|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.batch.sample.remotechunking; |
| 18 | + |
| 19 | +import org.springframework.amqp.core.AmqpTemplate; |
| 20 | +import org.springframework.amqp.core.Binding; |
| 21 | +import org.springframework.amqp.core.BindingBuilder; |
| 22 | +import org.springframework.amqp.core.Queue; |
| 23 | +import org.springframework.amqp.core.TopicExchange; |
| 24 | +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; |
| 25 | +import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
| 26 | +import org.springframework.amqp.rabbit.core.RabbitTemplate; |
| 27 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 28 | +import org.springframework.batch.integration.chunk.RemoteChunkingWorkerFlowBuilder; |
| 29 | +import org.springframework.batch.integration.config.annotation.EnableBatchIntegration; |
| 30 | +import org.springframework.batch.item.ItemProcessor; |
| 31 | +import org.springframework.batch.item.ItemWriter; |
| 32 | +import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.beans.factory.annotation.Value; |
| 34 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 35 | +import org.springframework.context.annotation.Bean; |
| 36 | +import org.springframework.context.annotation.Configuration; |
| 37 | +import org.springframework.context.annotation.PropertySource; |
| 38 | +import org.springframework.integration.amqp.dsl.Amqp; |
| 39 | +import org.springframework.integration.channel.DirectChannel; |
| 40 | +import org.springframework.integration.config.EnableIntegration; |
| 41 | +import org.springframework.integration.dsl.IntegrationFlow; |
| 42 | +import org.springframework.integration.dsl.IntegrationFlows; |
| 43 | + |
| 44 | +/** |
| 45 | + * This class is used to start a worker for |
| 46 | + * {@code org.springframework.batch.sample.RemoteChunkingJobFunctionalTests#testLaunchJob()}. |
| 47 | + */ |
| 48 | + |
| 49 | +@Configuration |
| 50 | +@EnableBatchProcessing |
| 51 | +@EnableBatchIntegration |
| 52 | +@EnableIntegration |
| 53 | +@PropertySource("classpath:default.amqp.properties") |
| 54 | +public class WorkerConfiguration { |
| 55 | + |
| 56 | + @Value("${rabbitmq.host}") |
| 57 | + private String host; |
| 58 | + |
| 59 | + @Value("${rabbitmq.port}") |
| 60 | + private int port; |
| 61 | + |
| 62 | + public static void main(String[] args) { |
| 63 | + new AnnotationConfigApplicationContext(WorkerConfiguration.class); |
| 64 | + } |
| 65 | + |
| 66 | + @Autowired |
| 67 | + private RemoteChunkingWorkerFlowBuilder<Integer, Integer> workerFlowBuilder; |
| 68 | + |
| 69 | + @Bean |
| 70 | + public ConnectionFactory rabbitConnectionFactory() { |
| 71 | + CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(); |
| 72 | + cachingConnectionFactory.setHost(host); |
| 73 | + cachingConnectionFactory.setPort(port); |
| 74 | + return cachingConnectionFactory; |
| 75 | + } |
| 76 | + |
| 77 | + @Bean |
| 78 | + public AmqpTemplate rabbitTemplate() { |
| 79 | + RabbitTemplate rabbitTemplate = new RabbitTemplate(); |
| 80 | + rabbitTemplate.setConnectionFactory(rabbitConnectionFactory()); |
| 81 | + return rabbitTemplate; |
| 82 | + } |
| 83 | + |
| 84 | + @Bean |
| 85 | + public Queue requestQueue() { |
| 86 | + return new Queue("requests", false); |
| 87 | + } |
| 88 | + |
| 89 | + @Bean |
| 90 | + public Queue repliesQueue() { |
| 91 | + return new Queue("replies", false); |
| 92 | + } |
| 93 | + |
| 94 | + @Bean |
| 95 | + public TopicExchange exchange() { |
| 96 | + return new TopicExchange("remote-chunking-exchange"); |
| 97 | + } |
| 98 | + |
| 99 | + @Bean |
| 100 | + Binding repliesBinding(TopicExchange exchange) { |
| 101 | + return BindingBuilder |
| 102 | + .bind(repliesQueue()) |
| 103 | + .to(exchange) |
| 104 | + .with("replies"); |
| 105 | + } |
| 106 | + |
| 107 | + @Bean |
| 108 | + Binding requestBinding(TopicExchange exchange) { |
| 109 | + return BindingBuilder |
| 110 | + .bind(requestQueue()) |
| 111 | + .to(exchange) |
| 112 | + .with("requests"); |
| 113 | + } |
| 114 | + |
| 115 | + @Bean |
| 116 | + public DirectChannel requests() { |
| 117 | + return new DirectChannel(); |
| 118 | + } |
| 119 | + |
| 120 | + @Bean |
| 121 | + public DirectChannel replies() { |
| 122 | + return new DirectChannel(); |
| 123 | + } |
| 124 | + |
| 125 | + @Bean |
| 126 | + public IntegrationFlow incomingRequests(ConnectionFactory rabbitConnectionFactory) { |
| 127 | + return IntegrationFlows |
| 128 | + .from(Amqp.inboundAdapter(rabbitConnectionFactory, "requests")) |
| 129 | + .channel(requests()) |
| 130 | + .get(); |
| 131 | + } |
| 132 | + |
| 133 | + @Bean |
| 134 | + public IntegrationFlow outgoingReplies(AmqpTemplate rabbitTemplate) { |
| 135 | + return IntegrationFlows |
| 136 | + .from("replies") |
| 137 | + .handle(Amqp.outboundAdapter(rabbitTemplate).routingKey("replies")) |
| 138 | + .get(); |
| 139 | + } |
| 140 | + |
| 141 | + @Bean |
| 142 | + public IntegrationFlow integrationFlow(DirectChannel requests, DirectChannel replies) { |
| 143 | + return workerFlowBuilder |
| 144 | + .itemProcessor(itemProcessor()) |
| 145 | + .itemWriter(itemWriter()) |
| 146 | + .inputChannel(requests) |
| 147 | + .outputChannel(replies) |
| 148 | + .build(); |
| 149 | + } |
| 150 | + |
| 151 | + @Bean |
| 152 | + public ItemWriter<Integer> itemWriter() { |
| 153 | + return items -> { |
| 154 | + for (Integer item : items) { |
| 155 | + System.out.println("writing item " + item); |
| 156 | + } |
| 157 | + }; |
| 158 | + } |
| 159 | + |
| 160 | + @Bean |
| 161 | + public ItemProcessor<Integer, Integer> itemProcessor() { |
| 162 | + return item -> { |
| 163 | + System.out.println("processing item " + item); |
| 164 | + return item; |
| 165 | + }; |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments