|
| 1 | +/* |
| 2 | + * Copyright 2025-present 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 | + * https://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 | +package org.springframework.batch.integration.remote; |
| 17 | + |
| 18 | +import org.springframework.batch.core.job.JobExecution; |
| 19 | +import org.springframework.batch.core.step.StepExecution; |
| 20 | +import org.springframework.batch.core.repository.JobRepository; |
| 21 | +import org.springframework.batch.core.step.AbstractStep; |
| 22 | +import org.springframework.batch.infrastructure.poller.DirectPoller; |
| 23 | +import org.springframework.batch.infrastructure.poller.Poller; |
| 24 | +import org.springframework.batch.integration.partition.StepExecutionRequest; |
| 25 | +import org.springframework.integration.core.MessagingTemplate; |
| 26 | +import org.springframework.messaging.MessageChannel; |
| 27 | + |
| 28 | +import java.util.concurrent.Callable; |
| 29 | +import java.util.concurrent.Future; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | + |
| 32 | +import org.apache.commons.logging.Log; |
| 33 | +import org.apache.commons.logging.LogFactory; |
| 34 | + |
| 35 | +/** |
| 36 | + * A {@link org.springframework.batch.core.step.Step} implementation that delegates the |
| 37 | + * execution to a remote worker step through messaging. |
| 38 | + * <p> |
| 39 | + * The remote worker step must be listening to the same message channel to receive step |
| 40 | + * execution requests. |
| 41 | + * <p> |
| 42 | + * The step execution is created locally and sent to the remote worker step which will |
| 43 | + * update its status and context in the job repository. The {@link RemoteStep} will poll |
| 44 | + * the job repository to check for step completion. |
| 45 | + * |
| 46 | + * @author Mahmoud Ben Hassine |
| 47 | + * @since 6.0.0 |
| 48 | + */ |
| 49 | +public class RemoteStep extends AbstractStep { |
| 50 | + |
| 51 | + private static final Log logger = LogFactory.getLog(RemoteStep.class.getName()); |
| 52 | + |
| 53 | + private final String remoteStepName; |
| 54 | + |
| 55 | + private final MessagingTemplate messagingTemplate; |
| 56 | + |
| 57 | + private MessageChannel messageChannel; |
| 58 | + |
| 59 | + private long pollInterval = 10000; |
| 60 | + |
| 61 | + private long timeout = -1; |
| 62 | + |
| 63 | + /** |
| 64 | + * Create a new {@link RemoteStep} instance. |
| 65 | + * @param name the name of this step |
| 66 | + * @param remoteStepName the name of the remote worker step to execute |
| 67 | + * @param jobRepository the job repository to use |
| 68 | + * @param messagingTemplate the messaging template to use to send step execution |
| 69 | + * requests |
| 70 | + */ |
| 71 | + public RemoteStep(String name, String remoteStepName, JobRepository jobRepository, |
| 72 | + MessagingTemplate messagingTemplate) { |
| 73 | + super(jobRepository); |
| 74 | + setName(name); |
| 75 | + this.remoteStepName = remoteStepName; |
| 76 | + this.messagingTemplate = messagingTemplate; |
| 77 | + this.messageChannel = this.messagingTemplate.getDefaultDestination(); |
| 78 | + } |
| 79 | + |
| 80 | + public void setTimeout(long timeout) { |
| 81 | + this.timeout = timeout; |
| 82 | + } |
| 83 | + |
| 84 | + public void setPollInterval(long pollInterval) { |
| 85 | + this.pollInterval = pollInterval; |
| 86 | + } |
| 87 | + |
| 88 | + public void setMessageChannel(MessageChannel messageChannel) { |
| 89 | + this.messageChannel = messageChannel; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + protected void doExecute(StepExecution stepExecution) throws Exception { |
| 94 | + // create a step execution for the remote worker step |
| 95 | + JobExecution jobExecution = stepExecution.getJobExecution(); |
| 96 | + StepExecution workerStepExecution = getJobRepository().createStepExecution(this.remoteStepName, jobExecution); |
| 97 | + |
| 98 | + // pass the same context to the remote worker step |
| 99 | + workerStepExecution.setExecutionContext(stepExecution.getExecutionContext()); |
| 100 | + getJobRepository().update(workerStepExecution); |
| 101 | + getJobRepository().updateExecutionContext(workerStepExecution); |
| 102 | + |
| 103 | + // send step execution request and wait for the remote step to finish |
| 104 | + StepExecutionRequest stepExecutionRequest = new StepExecutionRequest(this.remoteStepName, jobExecution.getId(), |
| 105 | + workerStepExecution.getId()); |
| 106 | + this.messagingTemplate.convertAndSend(this.messageChannel, stepExecutionRequest); |
| 107 | + StepExecution updatedWorkerExecution = pollRemoteStep(workerStepExecution); |
| 108 | + |
| 109 | + // upgrade status and context based on the remote step |
| 110 | + stepExecution.setExecutionContext(updatedWorkerExecution.getExecutionContext()); |
| 111 | + stepExecution.upgradeStatus(updatedWorkerExecution.getStatus()); |
| 112 | + stepExecution.setExitStatus(updatedWorkerExecution.getExitStatus()); |
| 113 | + } |
| 114 | + |
| 115 | + private StepExecution pollRemoteStep(StepExecution workerStepExecution) throws Exception { |
| 116 | + Poller<StepExecution> poller = new DirectPoller<>(this.pollInterval); |
| 117 | + Callable<StepExecution> callable = () -> { |
| 118 | + StepExecution updatedExecution = getJobRepository().getStepExecution(workerStepExecution.getId()); |
| 119 | + if (updatedExecution != null && updatedExecution.getStatus().isRunning()) { |
| 120 | + logger.info("Waiting for remote step to finish"); |
| 121 | + return null; |
| 122 | + } |
| 123 | + else { |
| 124 | + return updatedExecution; |
| 125 | + } |
| 126 | + }; |
| 127 | + Future<StepExecution> executionFuture = poller.poll(callable); |
| 128 | + if (this.timeout >= 0) { |
| 129 | + return executionFuture.get(this.timeout, TimeUnit.MILLISECONDS); |
| 130 | + } |
| 131 | + else { |
| 132 | + return executionFuture.get(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments