Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e5bb95
commit 3a9d8d9
Showing
5 changed files
with
423 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
...es/src/main/java/org/springframework/batch/sample/remotechunking/WorkerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.batch.sample.remotechunking; | ||
|
||
import org.springframework.amqp.core.AmqpTemplate; | ||
import org.springframework.amqp.core.Binding; | ||
import org.springframework.amqp.core.BindingBuilder; | ||
import org.springframework.amqp.core.Queue; | ||
import org.springframework.amqp.core.TopicExchange; | ||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; | ||
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.integration.chunk.RemoteChunkingWorkerFlowBuilder; | ||
import org.springframework.batch.integration.config.annotation.EnableBatchIntegration; | ||
import org.springframework.batch.item.ItemProcessor; | ||
import org.springframework.batch.item.ItemWriter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.integration.amqp.dsl.Amqp; | ||
import org.springframework.integration.channel.DirectChannel; | ||
import org.springframework.integration.config.EnableIntegration; | ||
import org.springframework.integration.dsl.IntegrationFlow; | ||
import org.springframework.integration.dsl.IntegrationFlows; | ||
|
||
/** | ||
* This class is used to start a worker for | ||
* {@code org.springframework.batch.sample.RemoteChunkingJobFunctionalTests#testLaunchJob()}. | ||
*/ | ||
|
||
@Configuration | ||
@EnableBatchProcessing | ||
@EnableBatchIntegration | ||
@EnableIntegration | ||
@PropertySource("classpath:default.amqp.properties") | ||
public class WorkerConfiguration { | ||
|
||
@Value("${rabbitmq.host}") | ||
private String host; | ||
|
||
@Value("${rabbitmq.port}") | ||
private int port; | ||
|
||
public static void main(String[] args) { | ||
new AnnotationConfigApplicationContext(WorkerConfiguration.class); | ||
} | ||
|
||
@Autowired | ||
private RemoteChunkingWorkerFlowBuilder<Integer, Integer> workerFlowBuilder; | ||
|
||
@Bean | ||
public ConnectionFactory rabbitConnectionFactory() { | ||
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(); | ||
cachingConnectionFactory.setHost(host); | ||
cachingConnectionFactory.setPort(port); | ||
return cachingConnectionFactory; | ||
} | ||
|
||
@Bean | ||
public AmqpTemplate rabbitTemplate() { | ||
RabbitTemplate rabbitTemplate = new RabbitTemplate(); | ||
rabbitTemplate.setConnectionFactory(rabbitConnectionFactory()); | ||
return rabbitTemplate; | ||
} | ||
|
||
@Bean | ||
public Queue requestQueue() { | ||
return new Queue("requests", false); | ||
} | ||
|
||
@Bean | ||
public Queue repliesQueue() { | ||
return new Queue("replies", false); | ||
} | ||
|
||
@Bean | ||
public TopicExchange exchange() { | ||
return new TopicExchange("remote-chunking-exchange"); | ||
} | ||
|
||
@Bean | ||
Binding repliesBinding(TopicExchange exchange) { | ||
return BindingBuilder | ||
.bind(repliesQueue()) | ||
.to(exchange) | ||
.with("replies"); | ||
} | ||
|
||
@Bean | ||
Binding requestBinding(TopicExchange exchange) { | ||
return BindingBuilder | ||
.bind(requestQueue()) | ||
.to(exchange) | ||
.with("requests"); | ||
} | ||
|
||
@Bean | ||
public DirectChannel requests() { | ||
return new DirectChannel(); | ||
} | ||
|
||
@Bean | ||
public DirectChannel replies() { | ||
return new DirectChannel(); | ||
} | ||
|
||
@Bean | ||
public IntegrationFlow incomingRequests(ConnectionFactory rabbitConnectionFactory) { | ||
return IntegrationFlows | ||
.from(Amqp.inboundAdapter(rabbitConnectionFactory, "requests")) | ||
.channel(requests()) | ||
.get(); | ||
} | ||
|
||
@Bean | ||
public IntegrationFlow outgoingReplies(AmqpTemplate rabbitTemplate) { | ||
return IntegrationFlows | ||
.from("replies") | ||
.handle(Amqp.outboundAdapter(rabbitTemplate).routingKey("replies")) | ||
.get(); | ||
} | ||
|
||
@Bean | ||
public IntegrationFlow integrationFlow(DirectChannel requests, DirectChannel replies) { | ||
return workerFlowBuilder | ||
.itemProcessor(itemProcessor()) | ||
.itemWriter(itemWriter()) | ||
.inputChannel(requests) | ||
.outputChannel(replies) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public ItemWriter<Integer> itemWriter() { | ||
return items -> { | ||
for (Integer item : items) { | ||
System.out.println("writing item " + item); | ||
} | ||
}; | ||
} | ||
|
||
@Bean | ||
public ItemProcessor<Integer, Integer> itemProcessor() { | ||
return item -> { | ||
System.out.println("processing item " + item); | ||
return item; | ||
}; | ||
} | ||
|
||
} |
Oops, something went wrong.