@@ -706,10 +706,10 @@ configuration similar to the following:
706
706
.Java Configuration
707
707
[source, java, role="javaContent"]
708
708
----
709
- public Job chunkJob(ItemReader itemReader ) {
709
+ public Job chunkJob() {
710
710
return jobBuilderFactory.get("personJob")
711
711
.start(stepBuilderFactory.get("step1")
712
- .chunk(200)
712
+ .<Person, Person> chunk(200)
713
713
.reader(itemReader())
714
714
.writer(itemWriter())
715
715
.build())
@@ -722,7 +722,7 @@ to use for reading data on the master. The `ItemWriter` reference
722
722
points to a special `ItemWriter`
723
723
(called `ChunkMessageChannelItemWriter`),
724
724
as described above. The processor (if any) is left off the
725
- master configuration, as it is configured on the slave . The
725
+ master configuration, as it is configured on the worker . The
726
726
following configuration provides a basic master setup. You
727
727
should check any additional component properties, such as
728
728
throttle limits and so on, when implementing your use case.
@@ -734,7 +734,7 @@ throttle limits and so on, when implementing your use case.
734
734
<property name="brokerURL" value="tcp://localhost:61616"/>
735
735
</bean>
736
736
737
- <int-jms:outbound-channel-adapter id="requests " destination-name="requests"/>
737
+ <int-jms:outbound-channel-adapter id="jmsRequests " destination-name="requests"/>
738
738
739
739
<bean id="messagingTemplate"
740
740
class="org.springframework.integration.core.MessagingTemplate">
@@ -749,12 +749,6 @@ throttle limits and so on, when implementing your use case.
749
749
<property name="replyChannel" ref="replies"/>
750
750
</bean>
751
751
752
- <bean id="chunkHandler"
753
- class="org.springframework.batch.integration.chunk.RemoteChunkHandlerFactoryBean">
754
- <property name="chunkWriter" ref="itemWriter"/>
755
- <property name="step" ref="step1"/>
756
- </bean>
757
-
758
752
<int:channel id="replies">
759
753
<int:queue/>
760
754
</int:channel>
@@ -774,58 +768,55 @@ public org.apache.activemq.ActiveMQConnectionFactory connectionFactory() {
774
768
return factory;
775
769
}
776
770
771
+ /*
772
+ * Configure outbound flow (requests going to workers)
773
+ */
774
+
777
775
@Bean
778
776
public DirectChannel requests() {
779
777
return new DirectChannel();
780
778
}
781
779
782
780
@Bean
783
- public IntegrationFlow jmsOutboundFlow( ) {
784
- return IntegrationFlows.from("requests")
785
- .handle(Jms.outboundGateway(connectionFactory ())
786
- .requestDestination ("requests"))
781
+ public IntegrationFlow outboundFlow(ActiveMQConnectionFactory connectionFactory ) {
782
+ return IntegrationFlows
783
+ .from(requests ())
784
+ .handle(Jms.outboundAdapter(connectionFactory).destination ("requests"))
787
785
.get();
788
786
}
789
787
790
- @Bean
791
- public MessagingTemplate messagingTemplate() {
792
- MessagingTemplate template = new MessagingTemplate();
793
- template.setDefaultChannel(requests());
794
- template.setReceiveTimeout(2000);
795
- return template;
796
- }
797
-
798
- @Bean
799
- @StepScope
800
- public ChunkMessageChannelItemWriter itemWriter() {
801
- ChunkMessageChannelItemWriter chunkMessageChannelItemWriter = new ChunkMessageChannelItemWriter();
802
- chunkMessageChannelItemWriter.setMessagingOperations(messagingTemplate());
803
- chunkMessageChannelItemWriter.setReplyChannel(replies());
804
- return chunkMessageChannelItemWriter;
805
- }
806
-
807
- @Bean
808
- public RemoteChunkHandlerFactoryBean chunkHandler() {
809
- RemoteChunkHandlerFactoryBean remoteChunkHandlerFactoryBean = new RemoteChunkHandlerFactoryBean();
810
- remoteChunkHandlerFactoryBean.setChunkWriter(itemWriter());
811
- remoteChunkHandlerFactoryBean.setStep(step1());
812
- return remoteChunkHandlerFactoryBean;
813
- }
788
+ /*
789
+ * Configure inbound flow (replies coming from workers)
790
+ */
814
791
815
792
@Bean
816
793
public QueueChannel replies() {
817
794
return new QueueChannel();
818
795
}
819
796
820
797
@Bean
821
- public IntegrationFlow jmsReplies( ) {
798
+ public IntegrationFlow inboundFlow(ActiveMQConnectionFactory connectionFactory ) {
822
799
return IntegrationFlows
823
- .from(Jms.messageDrivenChannelAdapter(connectionFactory())
824
- .configureListenerContainer(c -> c.subscriptionDurable(false))
825
- .destination("replies"))
800
+ .from(Jms.messageDrivenChannelAdapter(connectionFactory).destination("replies"))
826
801
.channel(replies())
827
802
.get();
828
803
}
804
+
805
+ /*
806
+ * Configure the ChunkMessageChannelItemWriter
807
+ */
808
+
809
+ @Bean
810
+ public ItemWriter<Integer> itemWriter() {
811
+ MessagingTemplate messagingTemplate = new MessagingTemplate();
812
+ messagingTemplate.setDefaultChannel(requests());
813
+ messagingTemplate.setReceiveTimeout(2000);
814
+ ChunkMessageChannelItemWriter<Integer> chunkMessageChannelItemWriter
815
+ = new ChunkMessageChannelItemWriter<>();
816
+ chunkMessageChannelItemWriter.setMessagingOperations(messagingTemplate);
817
+ chunkMessageChannelItemWriter.setReplyChannel(replies());
818
+ return chunkMessageChannelItemWriter;
819
+ }
829
820
----
830
821
831
822
The preceding configuration provides us with a number of beans. We
@@ -836,7 +827,7 @@ referenced by our job step, uses the
836
827
`ChunkMessageChannelItemWriter` for writing chunks over the
837
828
configured middleware.
838
829
839
- Now we can move on to the slave configuration, as shown in the following example:
830
+ Now we can move on to the worker configuration, as shown in the following example:
840
831
841
832
842
833
.XML Configuration
@@ -849,7 +840,7 @@ Now we can move on to the slave configuration, as shown in the following example
849
840
<int:channel id="requests"/>
850
841
<int:channel id="replies"/>
851
842
852
- <int-jms:message-driven-channel-adapter id="jmsIn "
843
+ <int-jms:message-driven-channel-adapter id="incomingRequests "
853
844
destination-name="requests"
854
845
channel="requests"/>
855
846
@@ -889,65 +880,72 @@ public org.apache.activemq.ActiveMQConnectionFactory connectionFactory() {
889
880
return factory;
890
881
}
891
882
883
+ /*
884
+ * Configure inbound flow (requests coming from the master)
885
+ */
886
+
892
887
@Bean
893
888
public DirectChannel requests() {
894
889
return new DirectChannel();
895
890
}
896
- @Bean
897
- public DirectChannel replies() {
898
- return new DirectChannel();
899
- }
900
891
901
892
@Bean
902
- public IntegrationFlow jmsIn( ) {
893
+ public IntegrationFlow inboundFlow(ActiveMQConnectionFactory connectionFactory ) {
903
894
return IntegrationFlows
904
- .from(Jms.messageDrivenChannelAdapter(connectionFactory())
905
- .configureListenerContainer(c -> c.subscriptionDurable(false))
906
- .destination("requests"))
895
+ .from(Jms.messageDrivenChannelAdapter(connectionFactory).destination("requests"))
907
896
.channel(requests())
908
897
.get();
909
898
}
910
899
900
+ /*
901
+ * Configure outbound flow (replies going to the master)
902
+ */
903
+
911
904
@Bean
912
- public IntegrationFlow outgoingReplies() {
913
- return IntegrationFlows.from("replies")
914
- .handle(Jms.outboundGateway(connectionFactory())
915
- .requestDestination("replies"))
916
- .get();
905
+ public DirectChannel replies() {
906
+ return new DirectChannel();
917
907
}
918
908
919
909
@Bean
920
- @ServiceActivator(inputChannel = "requests")
921
- public AggregatorFactoryBean serviceActivator() throws Exception{
922
- AggregatorFactoryBean aggregatorFactoryBean = new AggregatorFactoryBean();
923
- aggregatorFactoryBean.setProcessorBean(chunkProcessorChunkHandler());
924
- aggregatorFactoryBean.setOutputChannel(replies());
925
- ...
926
- return aggregatorFactoryBean;
910
+ public IntegrationFlow outboundFlow(ActiveMQConnectionFactory connectionFactory) {
911
+ return IntegrationFlows
912
+ .from(replies())
913
+ .handle(Jms.outboundAdapter(connectionFactory).destination("replies"))
914
+ .get();
927
915
}
928
916
917
+ /*
918
+ * Configure the ChunkProcessorChunkHandler
919
+ */
920
+
929
921
@Bean
930
- public ChunkProcessorChunkHandler chunkProcessorChunkHandler() {
931
- ChunkProcessorChunkHandler chunkProcessorChunkHandler = new ChunkProcessorChunkHandler();
932
- chunkProcessorChunkHandler.setChunkProcessor(new SimpleChunkProcessor(personItemProcessor(), personItemWriter()));
922
+ @ServiceActivator(inputChannel = "requests", outputChannel = "replies")
923
+ public ChunkProcessorChunkHandler<Integer> chunkProcessorChunkHandler() {
924
+ ChunkProcessor<Integer> chunkProcessor
925
+ = new SimpleChunkProcessor<>(itemProcessor(), itemWriter());
926
+ ChunkProcessorChunkHandler<Integer> chunkProcessorChunkHandler
927
+ = new ChunkProcessorChunkHandler<>();
928
+ chunkProcessorChunkHandler.setChunkProcessor(chunkProcessor);
933
929
return chunkProcessorChunkHandler;
934
930
}
935
931
----
936
932
937
933
Most of these configuration items should look familiar from the
938
- master configuration. Slaves do not need access to
934
+ master configuration. Workers do not need access to
939
935
the Spring Batch `JobRepository` nor
940
936
to the actual job configuration file. The main bean of interest
941
937
is the `chunkProcessorChunkHandler`. The
942
938
`chunkProcessor` property of `ChunkProcessorChunkHandler` takes a
943
939
configured `SimpleChunkProcessor`, which is where you would provide a reference to your
944
940
`ItemWriter` (and, optionally, your
945
- `ItemProcessor`) that will run on the slave
941
+ `ItemProcessor`) that will run on the worker
946
942
when it receives chunks from the master.
947
943
948
944
For more information, see the section of the "Scalability" chapter on
949
945
link:$$http://docs.spring.io/spring-batch/reference/html/scalability.html#remoteChunking$$[Remote Chunking].
950
946
947
+ You can find a complete example of a remote chunking job
948
+ link:$$https://github.com/spring-projects/spring-batch/tree/master/spring-batch-samples#remote-chunking-sample$$[here].
951
949
952
950
[[remote-partitioning]]
953
951
@@ -960,8 +958,8 @@ image::{batch-asciidoc}images/remote-partitioning.png[Remote Partitioning, scale
960
958
Remote Partitioning, on the other hand, is useful when it
961
959
is not the processing of items but rather the associated I/O that
962
960
causes the bottleneck. Using Remote Partitioning, work can
963
- be farmed out to slaves that execute complete Spring Batch
964
- steps. Thus, each slave has its own `ItemReader`, `ItemProcessor`, and
961
+ be farmed out to workers that execute complete Spring Batch
962
+ steps. Thus, each worker has its own `ItemReader`, `ItemProcessor`, and
965
963
`ItemWriter`. For this purpose, Spring Batch
966
964
Integration provides the `MessageChannelPartitionHandler`.
967
965
0 commit comments