Add builders for master/worker beans in remote chunking setup
This commit adds a new annotation `@EnableBatchIntegration` that makes it possible to autowire two beans in the application context: * RemoteChunkingMasterStepBuilderFactory: used to create a master step * RemoteChunkingWorkerBuilder: used to create a worker integration flow The goal of these new APIs is to simplify the setup of remote chunking job by automatically configuring infrastructure beans. Resolves BATCH-2686
This commit is contained in:
committed by
Michael Minella
parent
977741c294
commit
ee56a2320d
@@ -0,0 +1,403 @@
|
||||
/*
|
||||
* 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.integration.chunk;
|
||||
|
||||
import org.springframework.batch.core.ChunkListener;
|
||||
import org.springframework.batch.core.ItemReadListener;
|
||||
import org.springframework.batch.core.ItemWriteListener;
|
||||
import org.springframework.batch.core.SkipListener;
|
||||
import org.springframework.batch.core.StepExecutionListener;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.step.builder.FaultTolerantStepBuilder;
|
||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||
import org.springframework.batch.core.step.item.KeyGenerator;
|
||||
import org.springframework.batch.core.step.skip.SkipPolicy;
|
||||
import org.springframework.batch.core.step.tasklet.TaskletStep;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.repeat.CompletionPolicy;
|
||||
import org.springframework.batch.repeat.RepeatOperations;
|
||||
import org.springframework.batch.repeat.exception.ExceptionHandler;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.core.MessagingTemplate;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.retry.RetryPolicy;
|
||||
import org.springframework.retry.backoff.BackOffPolicy;
|
||||
import org.springframework.retry.policy.RetryContextCache;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.interceptor.TransactionAttribute;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Builder for a master step in a remote chunking setup. This builder creates and
|
||||
* sets a {@link ChunkMessageChannelItemWriter} on the master step.
|
||||
*
|
||||
* If no <code>messagingTemplate</code> is provided through
|
||||
* {@link RemoteChunkingMasterStepBuilder#messagingTemplate(MessagingTemplate)},
|
||||
* this builder will create one. The <code>outputChannel</code> set with
|
||||
* {@link RemoteChunkingMasterStepBuilder#outputChannel(DirectChannel)} will be
|
||||
* used as a default channel of the messaging template and will override any default
|
||||
* channel already set on the messaging template.
|
||||
*
|
||||
* @param <I> type of input items
|
||||
* @param <O> type of output items
|
||||
*
|
||||
* @since 4.1
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
public class RemoteChunkingMasterStepBuilder<I, O> extends FaultTolerantStepBuilder<I, O> {
|
||||
|
||||
private MessagingTemplate messagingTemplate;
|
||||
private PollableChannel inputChannel;
|
||||
private DirectChannel outputChannel;
|
||||
|
||||
private final int DEFAULT_MAX_WAIT_TIMEOUTS = 40;
|
||||
private static final long DEFAULT_THROTTLE_LIMIT = 6;
|
||||
private int maxWaitTimeouts = DEFAULT_MAX_WAIT_TIMEOUTS;
|
||||
private long throttleLimit = DEFAULT_THROTTLE_LIMIT;
|
||||
|
||||
/**
|
||||
* Create a new {@link RemoteChunkingMasterStepBuilder}.
|
||||
*
|
||||
* @param stepName name of the master step
|
||||
*/
|
||||
public RemoteChunkingMasterStepBuilder(String stepName) {
|
||||
super(new StepBuilder(stepName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the input channel on which replies from workers will be received.
|
||||
* The provided input channel will be set as a reply channel on the
|
||||
* {@link ChunkMessageChannelItemWriter} created by this builder.
|
||||
*
|
||||
* @param inputChannel the input channel
|
||||
* @return this builder instance for fluent chaining
|
||||
*
|
||||
* @see ChunkMessageChannelItemWriter#setReplyChannel
|
||||
*/
|
||||
public RemoteChunkingMasterStepBuilder<I, O> inputChannel(PollableChannel inputChannel) {
|
||||
Assert.notNull(inputChannel, "inputChannel must not be null");
|
||||
this.inputChannel = inputChannel;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the output channel on which requests to workers will be sent.
|
||||
* The output channel will be set as a default channel on the provided
|
||||
* {@link MessagingTemplate} trough {@link RemoteChunkingMasterStepBuilder#messagingTemplate(MessagingTemplate)}
|
||||
* (or the one created by this builder if no messaging template is provided).
|
||||
*
|
||||
* @param outputChannel the output channel.
|
||||
* @return this builder instance for fluent chaining
|
||||
*
|
||||
* @see RemoteChunkingMasterStepBuilder#messagingTemplate(MessagingTemplate)
|
||||
*/
|
||||
public RemoteChunkingMasterStepBuilder<I, O> outputChannel(DirectChannel outputChannel) {
|
||||
Assert.notNull(outputChannel, "outputChannel must not be null");
|
||||
this.outputChannel = outputChannel;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link MessagingTemplate} to use to send data to workers.
|
||||
*
|
||||
* <p><strong>The default destination of the messaging template will be
|
||||
* overridden by the output channel provided through
|
||||
* {@link RemoteChunkingMasterStepBuilder#outputChannel(DirectChannel)}</strong>.</p>
|
||||
*
|
||||
* @param messagingTemplate the messaging template to use
|
||||
* @return this builder instance for fluent chaining
|
||||
*/
|
||||
public RemoteChunkingMasterStepBuilder<I, O> messagingTemplate(MessagingTemplate messagingTemplate) {
|
||||
Assert.notNull(messagingTemplate, "messagingTemplate must not be null");
|
||||
this.messagingTemplate = messagingTemplate;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The maximum number of times to wait at the end of a step for a non-null result from the remote workers. This is a
|
||||
* multiplier on the receive timeout set separately on the gateway. The ideal value is a compromise between allowing
|
||||
* slow workers time to finish, and responsiveness if there is a dead worker. Defaults to 40.
|
||||
*
|
||||
* @param maxWaitTimeouts the maximum number of wait timeouts
|
||||
* @see ChunkMessageChannelItemWriter#setMaxWaitTimeouts(int)
|
||||
*/
|
||||
public RemoteChunkingMasterStepBuilder<I, O> maxWaitTimeouts(int maxWaitTimeouts) {
|
||||
Assert.isTrue(maxWaitTimeouts > 0, "maxWaitTimeouts must be greater than zero");
|
||||
this.maxWaitTimeouts = maxWaitTimeouts;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the throttle limit. This limits the number of pending requests for chunk processing to avoid
|
||||
* overwhelming the receivers.
|
||||
*
|
||||
* @param throttleLimit the throttle limit to set
|
||||
* @see ChunkMessageChannelItemWriter#setThrottleLimit(long)
|
||||
*/
|
||||
public RemoteChunkingMasterStepBuilder<I, O> throttleLimit(long throttleLimit) {
|
||||
Assert.isTrue(throttleLimit > 0, "throttleLimit must be greater than zero");
|
||||
this.throttleLimit = throttleLimit;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a master {@link TaskletStep}.
|
||||
*
|
||||
* @return the configured master step
|
||||
* @see RemoteChunkHandlerFactoryBean
|
||||
*/
|
||||
public TaskletStep build() {
|
||||
Assert.notNull(this.inputChannel, "An InputChannel must be provided");
|
||||
Assert.notNull(this.outputChannel, "An OutputChannel must be provided");
|
||||
|
||||
// configure messaging template
|
||||
if (this.messagingTemplate == null) {
|
||||
this.messagingTemplate = new MessagingTemplate();
|
||||
}
|
||||
this.messagingTemplate.setDefaultChannel(this.outputChannel);
|
||||
|
||||
// configure item writer
|
||||
ChunkMessageChannelItemWriter<O> chunkMessageChannelItemWriter = new ChunkMessageChannelItemWriter<>();
|
||||
chunkMessageChannelItemWriter.setMessagingOperations(this.messagingTemplate);
|
||||
chunkMessageChannelItemWriter.setMaxWaitTimeouts(this.maxWaitTimeouts);
|
||||
chunkMessageChannelItemWriter.setThrottleLimit(this.throttleLimit);
|
||||
chunkMessageChannelItemWriter.setReplyChannel(this.inputChannel);
|
||||
super.writer(chunkMessageChannelItemWriter);
|
||||
|
||||
return super.build();
|
||||
}
|
||||
|
||||
/*
|
||||
* The following methods override those from parent builders and return
|
||||
* the current builder type.
|
||||
* FIXME: Change parent builders to be generic and return current builder
|
||||
* type in each method.
|
||||
*/
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> reader(ItemReader<? extends I> reader) {
|
||||
super.reader(reader);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> repository(JobRepository jobRepository) {
|
||||
super.repository(jobRepository);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> transactionManager(PlatformTransactionManager transactionManager) {
|
||||
super.transactionManager(transactionManager);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> listener(Object listener) {
|
||||
super.listener(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> listener(SkipListener<? super I, ? super O> listener) {
|
||||
super.listener(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> listener(ChunkListener listener) {
|
||||
super.listener(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> transactionAttribute(TransactionAttribute transactionAttribute) {
|
||||
super.transactionAttribute(transactionAttribute);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> listener(org.springframework.retry.RetryListener listener) {
|
||||
super.listener(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> keyGenerator(KeyGenerator keyGenerator) {
|
||||
super.keyGenerator(keyGenerator);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> retryLimit(int retryLimit) {
|
||||
super.retryLimit(retryLimit);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> retryPolicy(RetryPolicy retryPolicy) {
|
||||
super.retryPolicy(retryPolicy);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> backOffPolicy(BackOffPolicy backOffPolicy) {
|
||||
super.backOffPolicy(backOffPolicy);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> retryContextCache(RetryContextCache retryContextCache) {
|
||||
super.retryContextCache(retryContextCache);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> skipLimit(int skipLimit) {
|
||||
super.skipLimit(skipLimit);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> noSkip(Class<? extends Throwable> type) {
|
||||
super.noSkip(type);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> skip(Class<? extends Throwable> type) {
|
||||
super.skip(type);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> skipPolicy(SkipPolicy skipPolicy) {
|
||||
super.skipPolicy(skipPolicy);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> noRollback(Class<? extends Throwable> type) {
|
||||
super.noRollback(type);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> noRetry(Class<? extends Throwable> type) {
|
||||
super.noRetry(type);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> retry(Class<? extends Throwable> type) {
|
||||
super.retry(type);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> stream(ItemStream stream) {
|
||||
super.stream(stream);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> chunk(int chunkSize) {
|
||||
super.chunk(chunkSize);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> chunk(CompletionPolicy completionPolicy) {
|
||||
super.chunk(completionPolicy);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will throw a {@link UnsupportedOperationException} since
|
||||
* the item writer of the master step in a remote chunking setup will be
|
||||
* automatically set to an instance of {@link ChunkMessageChannelItemWriter}.
|
||||
*
|
||||
* When building a master step for remote chunking, no item writer must be
|
||||
* provided.
|
||||
*
|
||||
* @throws UnsupportedOperationException if an item writer is provided
|
||||
* @see ChunkMessageChannelItemWriter
|
||||
* @see RemoteChunkHandlerFactoryBean#setChunkWriter(ItemWriter)
|
||||
*/
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> writer(ItemWriter<? super O> writer) throws UnsupportedOperationException {
|
||||
throw new UnsupportedOperationException("When configuring a master step " +
|
||||
"for remote chunking, the item writer will be automatically set " +
|
||||
"to an instance of ChunkMessageChannelItemWriter. The item writer " +
|
||||
"must not be provided in this case.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> readerIsTransactionalQueue() {
|
||||
super.readerIsTransactionalQueue();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> listener(ItemReadListener<? super I> listener) {
|
||||
super.listener(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> listener(ItemWriteListener<? super O> listener) {
|
||||
super.listener(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> chunkOperations(RepeatOperations repeatTemplate) {
|
||||
super.chunkOperations(repeatTemplate);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> exceptionHandler(ExceptionHandler exceptionHandler) {
|
||||
super.exceptionHandler(exceptionHandler);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> stepOperations(RepeatOperations repeatTemplate) {
|
||||
super.stepOperations(repeatTemplate);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> startLimit(int startLimit) {
|
||||
super.startLimit(startLimit);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> listener(StepExecutionListener listener) {
|
||||
super.listener(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemoteChunkingMasterStepBuilder<I, O> allowStartIfComplete(boolean allowStartIfComplete) {
|
||||
super.allowStartIfComplete(allowStartIfComplete);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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.integration.chunk;
|
||||
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
/**
|
||||
* Convenient factory for a {@link RemoteChunkingMasterStepBuilder} which sets
|
||||
* the {@link JobRepository} and {@link PlatformTransactionManager} automatically.
|
||||
*
|
||||
* @since 4.1
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
public class RemoteChunkingMasterStepBuilderFactory {
|
||||
|
||||
private JobRepository jobRepository;
|
||||
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
/**
|
||||
* Create a new {@link RemoteChunkingMasterStepBuilderFactory}.
|
||||
*
|
||||
* @param jobRepository the job repository to use
|
||||
* @param transactionManager the transaction manager to use
|
||||
*/
|
||||
public RemoteChunkingMasterStepBuilderFactory(
|
||||
JobRepository jobRepository,
|
||||
PlatformTransactionManager transactionManager) {
|
||||
|
||||
this.jobRepository = jobRepository;
|
||||
this.transactionManager = transactionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link RemoteChunkingMasterStepBuilder} and initializes its job
|
||||
* repository and transaction manager.
|
||||
*
|
||||
* @param name the name of the step
|
||||
* @return a {@link RemoteChunkingMasterStepBuilder}
|
||||
*/
|
||||
public <I, O> RemoteChunkingMasterStepBuilder<I, O> get(String name) {
|
||||
return new RemoteChunkingMasterStepBuilder<I, O>(name)
|
||||
.repository(this.jobRepository)
|
||||
.transactionManager(this.transactionManager);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* 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.integration.chunk;
|
||||
|
||||
import org.springframework.batch.core.step.item.SimpleChunkProcessor;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.support.PassThroughItemProcessor;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.dsl.IntegrationFlow;
|
||||
import org.springframework.integration.dsl.IntegrationFlows;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Builder for a worker in a remote chunking setup. This builder:
|
||||
*
|
||||
* <ul>
|
||||
* <li>creates a {@link ChunkProcessorChunkHandler} with the provided
|
||||
* item processor and writer. If no item processor is provided, a
|
||||
* {@link PassThroughItemProcessor} will be used</li>
|
||||
* <li>creates an {@link IntegrationFlow} with the
|
||||
* {@link ChunkProcessorChunkHandler} as a service activator which listens
|
||||
* to incoming requests on <code>inputChannel</code> and sends replies
|
||||
* on <code>outputChannel</code></li>
|
||||
* </ul>
|
||||
*
|
||||
* @param <I> type of input items
|
||||
* @param <O> type of output items
|
||||
*
|
||||
* @since 4.1
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
public class RemoteChunkingWorkerBuilder<I, O> {
|
||||
|
||||
private static final String SERVICE_ACTIVATOR_METHOD_NAME = "handleChunk";
|
||||
|
||||
private ItemProcessor<I, O> itemProcessor;
|
||||
private ItemWriter<O> itemWriter;
|
||||
private DirectChannel inputChannel;
|
||||
private DirectChannel outputChannel;
|
||||
|
||||
/**
|
||||
* Set the {@link ItemProcessor} to use to process items sent by the master
|
||||
* step.
|
||||
*
|
||||
* @param itemProcessor to use
|
||||
* @return this builder instance for fluent chaining
|
||||
*/
|
||||
public RemoteChunkingWorkerBuilder<I, O> itemProcessor(ItemProcessor<I, O> itemProcessor) {
|
||||
Assert.notNull(itemProcessor, "itemProcessor must not be null");
|
||||
this.itemProcessor = itemProcessor;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link ItemWriter} to use to write items sent by the master step.
|
||||
*
|
||||
* @param itemWriter to use
|
||||
* @return this builder instance for fluent chaining
|
||||
*/
|
||||
public RemoteChunkingWorkerBuilder<I, O> itemWriter(ItemWriter<O> itemWriter) {
|
||||
Assert.notNull(itemWriter, "itemWriter must not be null");
|
||||
this.itemWriter = itemWriter;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the input channel on which items sent by the master are received.
|
||||
*
|
||||
* @param inputChannel the input channel
|
||||
* @return this builder instance for fluent chaining
|
||||
*/
|
||||
public RemoteChunkingWorkerBuilder<I, O> inputChannel(DirectChannel inputChannel) {
|
||||
Assert.notNull(inputChannel, "inputChannel must not be null");
|
||||
this.inputChannel = inputChannel;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the output channel on which replies will be sent to the master step.
|
||||
*
|
||||
* @param outputChannel the output channel
|
||||
* @return this builder instance for fluent chaining
|
||||
*/
|
||||
public RemoteChunkingWorkerBuilder<I, O> outputChannel(DirectChannel outputChannel) {
|
||||
Assert.notNull(outputChannel, "outputChannel must not be null");
|
||||
this.outputChannel = outputChannel;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an {@link IntegrationFlow} with a {@link ChunkProcessorChunkHandler}
|
||||
* configured as a service activator listening to the input channel and replying
|
||||
* on the output channel.
|
||||
*
|
||||
* @return the integration flow
|
||||
*/
|
||||
public IntegrationFlow build() {
|
||||
Assert.notNull(this.itemWriter, "An ItemWriter must be provided");
|
||||
Assert.notNull(this.inputChannel, "An InputChannel must be provided");
|
||||
Assert.notNull(this.outputChannel, "An OutputChannel must be provided");
|
||||
|
||||
if(this.itemProcessor == null) {
|
||||
this.itemProcessor = new PassThroughItemProcessor();
|
||||
}
|
||||
SimpleChunkProcessor<I, O> chunkProcessor = new SimpleChunkProcessor<>(this.itemProcessor, this.itemWriter);
|
||||
|
||||
ChunkProcessorChunkHandler<I> chunkProcessorChunkHandler = new ChunkProcessorChunkHandler<>();
|
||||
chunkProcessorChunkHandler.setChunkProcessor(chunkProcessor);
|
||||
|
||||
return IntegrationFlows
|
||||
.from(this.inputChannel)
|
||||
.handle(chunkProcessorChunkHandler, SERVICE_ACTIVATOR_METHOD_NAME)
|
||||
.channel(this.outputChannel)
|
||||
.get();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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.integration.config.annotation;
|
||||
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.integration.chunk.RemoteChunkingMasterStepBuilderFactory;
|
||||
import org.springframework.batch.integration.chunk.RemoteChunkingWorkerBuilder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
/**
|
||||
* Base configuration class for Spring Batch Integration factory beans.
|
||||
*
|
||||
* @since 4.1
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
@Configuration
|
||||
public class BatchIntegrationConfiguration {
|
||||
|
||||
private JobRepository jobRepository;
|
||||
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@Autowired
|
||||
public BatchIntegrationConfiguration(
|
||||
JobRepository jobRepository,
|
||||
PlatformTransactionManager transactionManager) {
|
||||
|
||||
this.jobRepository = jobRepository;
|
||||
this.transactionManager = transactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RemoteChunkingMasterStepBuilderFactory remoteChunkingMasterStepBuilderFactory() {
|
||||
return new RemoteChunkingMasterStepBuilderFactory(this.jobRepository,
|
||||
this.transactionManager);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public <I,O> RemoteChunkingWorkerBuilder<I, O> remoteChunkingWorkerBuilder() {
|
||||
return new RemoteChunkingWorkerBuilder<>();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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.integration.config.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.batch.integration.chunk.RemoteChunkingWorkerBuilder;
|
||||
import org.springframework.batch.integration.chunk.RemoteChunkingMasterStepBuilderFactory;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* Enable Spring Batch Integration features and provide a base configuration for
|
||||
* setting up remote chunking infrastructure beans.
|
||||
*
|
||||
* By adding this annotation on a {@link org.springframework.context.annotation.Configuration}
|
||||
* class, it will be possible to autowire the following beans:
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@link RemoteChunkingMasterStepBuilderFactory}:
|
||||
* used to create a master step by automatically setting up the job repository
|
||||
* and transaction manager.</li>
|
||||
* <li>{@link RemoteChunkingWorkerBuilder}: used to create the integration
|
||||
* flow on the worker side.</li>
|
||||
* </ul>
|
||||
*
|
||||
* For example:
|
||||
*
|
||||
* <pre class="code">
|
||||
* @Configuration
|
||||
* @EnableBatchIntegration
|
||||
* public class RemoteChunkingAppConfig {
|
||||
*
|
||||
* @Autowired
|
||||
* private RemoteChunkingMasterStepBuilderFactory masterStepBuilderFactory;
|
||||
*
|
||||
* @Autowired
|
||||
* private RemoteChunkingWorkerBuilder workerBuilder;
|
||||
*
|
||||
* @Bean
|
||||
* public TaskletStep masterStep() {
|
||||
* return this.masterStepBuilderFactory
|
||||
* .get("masterStep")
|
||||
* .chunk(100)
|
||||
* .reader(itemReader())
|
||||
* .outputChannel(outgoingRequestsToWorkers())
|
||||
* .inputChannel(incomingRepliesFromWorkers())
|
||||
* .build();
|
||||
* }
|
||||
*
|
||||
* @Bean
|
||||
* public IntegrationFlow worker() {
|
||||
* return this.workerBuilder
|
||||
* .itemProcessor(itemProcessor())
|
||||
* .itemWriter(itemWriter())
|
||||
* .inputChannel(incomingRequestsFromMaster())
|
||||
* .outputChannel(outgoingRepliesToMaster())
|
||||
* .build();
|
||||
* }
|
||||
*
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @since 4.1
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Import(BatchIntegrationConfiguration.class)
|
||||
public @interface EnableBatchIntegration {
|
||||
}
|
||||
Reference in New Issue
Block a user