Refactor job launching message handler to use special request object.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package org.springframework.batch.integration.launch;
|
||||
package org.springframework.batch.integration.file;
|
||||
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.integration.message.Message;
|
||||
@@ -19,7 +19,6 @@ import java.io.IOException;
|
||||
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.integration.launch.MessageToJobParametersStrategy;
|
||||
import org.springframework.batch.item.ItemStreamException;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.message.Message;
|
||||
@@ -39,7 +38,7 @@ public class ResourcePayloadAsJobParameterStrategy implements MessageToJobParame
|
||||
* Convert a message payload which is a {@link Resource} to its URL
|
||||
* representation and load that into a job parameter.
|
||||
*
|
||||
* @see org.springframework.batch.integration.launch.MessageToJobParametersStrategy#getJobParameters(org.springframework.integration.message.Message)
|
||||
* @see org.springframework.batch.integration.file.MessageToJobParametersStrategy#getJobParameters(org.springframework.integration.message.Message)
|
||||
*/
|
||||
public JobParameters getJobParameters(Message<?> message) {
|
||||
JobParametersBuilder builder = new JobParametersBuilder();
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2006-2007 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.launch;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
|
||||
/**
|
||||
* Encapsulation of a {@link Job} and its {@link JobParameters} forming a
|
||||
* request for a job to be launched.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class JobExecutionRequest {
|
||||
|
||||
private final Job job;
|
||||
private final JobParameters jobParameters;
|
||||
|
||||
/**
|
||||
* @param job
|
||||
* @param jobParameters
|
||||
*/
|
||||
public JobExecutionRequest(Job job, JobParameters jobParameters) {
|
||||
super();
|
||||
this.job = job;
|
||||
this.jobParameters = jobParameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link Job} to be executed
|
||||
*/
|
||||
public Job getJob() {
|
||||
return this.job;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link JobParameters} for this request
|
||||
*/
|
||||
public JobParameters getJobParameters() {
|
||||
return this.jobParameters;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,10 +4,9 @@ import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobExecutionException;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.UnexpectedJobExecutionException;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.integration.annotation.Handler;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageHandlingException;
|
||||
|
||||
/**
|
||||
* Message handler which uses strategies to convert a Message into a job and a
|
||||
@@ -18,37 +17,28 @@ import org.springframework.integration.message.MessageHandlingException;
|
||||
*/
|
||||
public class JobLaunchingMessageHandler {
|
||||
|
||||
private MessageToJobStrategy messageToJobStrategy;
|
||||
private final JobLauncher jobLauncher;
|
||||
|
||||
private MessageToJobParametersStrategy messageToJobParametersStrategy = new MessagePropertiesToJobParametersStrategy();
|
||||
|
||||
private JobLauncher jobLauncher;
|
||||
|
||||
public JobLaunchingMessageHandler(JobLauncher jobLauncher, MessageToJobStrategy messageToJobStrategy) {
|
||||
/**
|
||||
* @param jobLauncher
|
||||
*/
|
||||
public JobLaunchingMessageHandler(JobLauncher jobLauncher) {
|
||||
super();
|
||||
this.jobLauncher = jobLauncher;
|
||||
this.messageToJobStrategy = messageToJobStrategy;
|
||||
}
|
||||
|
||||
@Handler
|
||||
public JobExecution handle(Message<?> message) {
|
||||
Job job = messageToJobStrategy.getJob(message);
|
||||
JobParameters jobParameters = messageToJobParametersStrategy.getJobParameters(message);
|
||||
public JobExecution launch(JobExecutionRequest request) {
|
||||
Job job = request.getJob();
|
||||
JobParameters jobParameters = request.getJobParameters();
|
||||
|
||||
try {
|
||||
JobExecution execution = jobLauncher.run(job, jobParameters);
|
||||
if (message.getHeader().getReturnAddress() != null) {
|
||||
return execution;
|
||||
}
|
||||
return null;
|
||||
return execution;
|
||||
}
|
||||
catch (JobExecutionException e) {
|
||||
throw new MessageHandlingException(message, "Exception executing job ");
|
||||
throw new UnexpectedJobExecutionException("Exception executing job: ["+request+"]", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setMessageToJobParametersStrategy(MessageToJobParametersStrategy messageToJobParametersStrategy) {
|
||||
this.messageToJobParametersStrategy = messageToJobParametersStrategy;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
package org.springframework.batch.integration.launch;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecutionException;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.channel.interceptor.ChannelInterceptorAdapter;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageHandlingException;
|
||||
|
||||
/**
|
||||
* Channel interceptor which launches the configured job after the message has
|
||||
* been received
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class JobLaunchingPostReceiveChannelInterceptor extends ChannelInterceptorAdapter {
|
||||
|
||||
private JobLauncher jobLauncher;
|
||||
|
||||
private Job job;
|
||||
|
||||
private MessageToJobParametersStrategy messageToJobParametersStrategy = new MessagePropertiesToJobParametersStrategy();
|
||||
|
||||
/**
|
||||
*
|
||||
* @param job The job to launch
|
||||
* @param jobLauncher
|
||||
*/
|
||||
public JobLaunchingPostReceiveChannelInterceptor(Job job, JobLauncher jobLauncher) {
|
||||
super();
|
||||
this.job = job;
|
||||
this.jobLauncher = jobLauncher;
|
||||
}
|
||||
|
||||
public MessageToJobParametersStrategy getMessageToJobParametersStrategy() {
|
||||
return messageToJobParametersStrategy;
|
||||
}
|
||||
|
||||
public void setMessageToJobParametersStrategy(MessageToJobParametersStrategy messageToJobParametersStrategy) {
|
||||
this.messageToJobParametersStrategy = messageToJobParametersStrategy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postReceive(Message<?> message, MessageChannel channel) {
|
||||
JobParameters parameters = messageToJobParametersStrategy.getJobParameters(message);
|
||||
try {
|
||||
jobLauncher.run(job, parameters);
|
||||
}
|
||||
catch (JobExecutionException e) {
|
||||
throw new MessageHandlingException(message, "Excpetion executing job ", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package org.springframework.batch.integration.launch;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* Builds an instance of JobParameters from the properties in the message header
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class MessagePropertiesToJobParametersStrategy implements MessageToJobParametersStrategy {
|
||||
|
||||
public JobParameters getJobParameters(Message<?> message) {
|
||||
JobParametersBuilder parametersBuilder = new JobParametersBuilder();
|
||||
Set<String> propertyNames = message.getHeader().getPropertyNames();
|
||||
for (String key : propertyNames) {
|
||||
parametersBuilder.addString(key, message.getHeader().getProperty(key));
|
||||
}
|
||||
return parametersBuilder.toJobParameters();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package org.springframework.batch.integration.launch;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
|
||||
/**
|
||||
* Interface for strategy implementations which convert from a Message to a Spring batch Job
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public interface MessageToJobStrategy{
|
||||
|
||||
public Job getJob(Message<?> message);
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package org.springframework.batch.integration.launch;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.configuration.JobLocator;
|
||||
import org.springframework.batch.core.repository.NoSuchJobException;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageHandlingException;
|
||||
|
||||
/**
|
||||
* Takes the string payload of a message and delegates to a JobLocator
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class StringPayloadAsJobNameStrategy implements MessageToJobStrategy{
|
||||
|
||||
private JobLocator jobLocator;
|
||||
|
||||
public StringPayloadAsJobNameStrategy(JobLocator jobLocator){
|
||||
this.jobLocator = jobLocator;
|
||||
}
|
||||
|
||||
public Job getJob(Message<?> message) {
|
||||
String name = (String)message.getPayload();
|
||||
try {
|
||||
return jobLocator.getJob(name);
|
||||
}
|
||||
catch (NoSuchJobException e) {
|
||||
throw new MessageHandlingException(message, "Could not find job with name " + name, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user