001/* 002 * Copyright 2002-2013 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 */ 016 017package org.springframework.batch.integration.launch; 018 019import org.springframework.batch.core.JobExecution; 020import org.springframework.batch.core.JobExecutionException; 021import org.springframework.batch.core.launch.JobLauncher; 022import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; 023import org.springframework.messaging.Message; 024import org.springframework.messaging.MessageHandlingException; 025import org.springframework.util.Assert; 026 027/** 028 * The {@link JobLaunchingGateway} is used to launch Batch Jobs. Internally it 029 * delegates to a {@link JobLaunchingMessageHandler}. 030 * 031 * @author Gunnar Hillert 032 * 033 * @since 1.3 034 */ 035public class JobLaunchingGateway extends AbstractReplyProducingMessageHandler { 036 037 private final JobLaunchingMessageHandler jobLaunchingMessageHandler; 038 039 /** 040 * Constructor taking a {@link JobLauncher} as parameter. 041 * 042 * @param jobLauncher Must not be null. 043 * 044 */ 045 public JobLaunchingGateway(JobLauncher jobLauncher) { 046 Assert.notNull(jobLauncher, "jobLauncher must not be null."); 047 this.jobLaunchingMessageHandler = new JobLaunchingMessageHandler(jobLauncher); 048 } 049 050 /** 051 * Launches a Batch Job using the provided request {@link Message}. The payload 052 * of the {@link Message} <em>must</em> be an instance of {@link JobLaunchRequest}. 053 * 054 * @param requestMessage must not be null. 055 * @return Generally a {@link JobExecution} will always be returned. An 056 * exception ({@link MessageHandlingException}) will only be thrown if there 057 * is a failure to start the job. The cause of the exception will be a 058 * {@link JobExecutionException}. 059 * 060 * @throws MessageHandlingException when a job cannot be launched 061 */ 062 @Override 063 protected Object handleRequestMessage(Message<?> requestMessage) { 064 065 Assert.notNull(requestMessage, "The provided requestMessage must not be null."); 066 067 final Object payload = requestMessage.getPayload(); 068 069 Assert.isInstanceOf(JobLaunchRequest.class, payload, "The payload must be of type JobLaunchRequest."); 070 071 final JobLaunchRequest jobLaunchRequest = (JobLaunchRequest) payload; 072 073 final JobExecution jobExecution; 074 075 try { 076 jobExecution = this.jobLaunchingMessageHandler.launch(jobLaunchRequest); 077 } catch (JobExecutionException e) { 078 throw new MessageHandlingException(requestMessage, e); 079 } 080 081 return jobExecution; 082 083 } 084 085}