001/*
002 * Copyright 2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.sample.remotepartitioning.aggregating;
017
018import org.apache.activemq.ActiveMQConnectionFactory;
019
020import org.springframework.batch.core.Step;
021import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
022import org.springframework.batch.core.configuration.annotation.StepScope;
023import org.springframework.batch.core.step.tasklet.Tasklet;
024import org.springframework.batch.integration.config.annotation.EnableBatchIntegration;
025import org.springframework.batch.integration.partition.RemotePartitioningWorkerStepBuilderFactory;
026import org.springframework.batch.repeat.RepeatStatus;
027import org.springframework.batch.sample.remotepartitioning.BrokerConfiguration;
028import org.springframework.batch.sample.remotepartitioning.DataSourceConfiguration;
029import org.springframework.beans.factory.annotation.Value;
030import org.springframework.context.annotation.Bean;
031import org.springframework.context.annotation.Configuration;
032import org.springframework.context.annotation.Import;
033import org.springframework.integration.channel.DirectChannel;
034import org.springframework.integration.dsl.IntegrationFlow;
035import org.springframework.integration.dsl.IntegrationFlows;
036import org.springframework.integration.jms.dsl.Jms;
037
038/**
039 * This configuration class is for the worker side of the remote partitioning sample.
040 * Each worker will process a partition sent by the master step.
041 *
042 * @author Mahmoud Ben Hassine
043 */
044@Configuration
045@EnableBatchProcessing
046@EnableBatchIntegration
047@Import(value = {DataSourceConfiguration.class, BrokerConfiguration.class})
048public class WorkerConfiguration {
049
050        private final RemotePartitioningWorkerStepBuilderFactory workerStepBuilderFactory;
051
052
053        public WorkerConfiguration(RemotePartitioningWorkerStepBuilderFactory workerStepBuilderFactory) {
054                this.workerStepBuilderFactory = workerStepBuilderFactory;
055        }
056
057        /*
058         * Configure inbound flow (requests coming from the master)
059         */
060        @Bean
061        public DirectChannel requests() {
062                return new DirectChannel();
063        }
064
065        @Bean
066        public IntegrationFlow inboundFlow(ActiveMQConnectionFactory connectionFactory) {
067                return IntegrationFlows
068                                .from(Jms.messageDrivenChannelAdapter(connectionFactory).destination("requests"))
069                                .channel(requests())
070                                .get();
071        }
072
073        /*
074         * Configure outbound flow (replies going to the master)
075         */
076        @Bean
077        public DirectChannel replies() {
078                return new DirectChannel();
079        }
080
081        @Bean
082        public IntegrationFlow outboundFlow(ActiveMQConnectionFactory connectionFactory) {
083                return IntegrationFlows
084                                .from(replies())
085                                .handle(Jms.outboundAdapter(connectionFactory).destination("replies"))
086                                .get();
087        }
088
089        /*
090         * Configure the worker step
091         */
092        @Bean
093        public Step workerStep() {
094                return this.workerStepBuilderFactory.get("workerStep")
095                                .inputChannel(requests())
096                                .outputChannel(replies())
097                                .tasklet(tasklet(null))
098                                .build();
099        }
100
101        @Bean
102        @StepScope
103        public Tasklet tasklet(@Value("#{stepExecutionContext['partition']}") String partition) {
104                return (contribution, chunkContext) -> {
105                        System.out.println("processing " + partition);
106                        return RepeatStatus.FINISHED;
107                };
108        }
109
110}