001/*
002 * Copyright 2006-2007 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.quartz;
017
018import java.util.Date;
019import java.util.Map;
020import java.util.Map.Entry;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.quartz.JobExecutionContext;
025import org.springframework.batch.core.JobExecutionException;
026import org.springframework.batch.core.JobParameters;
027import org.springframework.batch.core.JobParametersBuilder;
028import org.springframework.batch.core.configuration.JobLocator;
029import org.springframework.batch.core.launch.JobLauncher;
030import org.springframework.scheduling.quartz.QuartzJobBean;
031
032/**
033 * @author Dave Syer
034 * 
035 */
036public class JobLauncherDetails extends QuartzJobBean {
037
038        /**
039         * Special key in job data map for the name of a job to run.
040         */
041        static final String JOB_NAME = "jobName";
042
043        private static Log log = LogFactory.getLog(JobLauncherDetails.class);
044
045        private JobLocator jobLocator;
046
047        private JobLauncher jobLauncher;
048
049        /**
050         * Public setter for the {@link JobLocator}.
051         * @param jobLocator the {@link JobLocator} to set
052         */
053        public void setJobLocator(JobLocator jobLocator) {
054                this.jobLocator = jobLocator;
055        }
056
057        /**
058         * Public setter for the {@link JobLauncher}.
059         * @param jobLauncher the {@link JobLauncher} to set
060         */
061        public void setJobLauncher(JobLauncher jobLauncher) {
062                this.jobLauncher = jobLauncher;
063        }
064
065        @Override
066        protected void executeInternal(JobExecutionContext context) {
067                Map<String, Object> jobDataMap = context.getMergedJobDataMap();
068                String jobName = (String) jobDataMap.get(JOB_NAME);
069                log.info("Quartz trigger firing with Spring Batch jobName="+jobName);
070                JobParameters jobParameters = getJobParametersFromJobMap(jobDataMap);
071                try {
072                        jobLauncher.run(jobLocator.getJob(jobName), jobParameters);
073                }
074                catch (JobExecutionException e) {
075                        log.error("Could not execute job.", e);
076                }
077        }
078
079        /*
080         * Copy parameters that are of the correct type over to
081         * {@link JobParameters}, ignoring jobName.
082         * 
083         * @return a {@link JobParameters} instance
084         */
085        private JobParameters getJobParametersFromJobMap(Map<String, Object> jobDataMap) {
086
087                JobParametersBuilder builder = new JobParametersBuilder();
088
089                for (Entry<String, Object> entry : jobDataMap.entrySet()) {
090                        String key = entry.getKey();
091                        Object value = entry.getValue();
092                        if (value instanceof String && !key.equals(JOB_NAME)) {
093                                builder.addString(key, (String) value);
094                        }
095                        else if (value instanceof Float || value instanceof Double) {
096                                builder.addDouble(key, ((Number) value).doubleValue());
097                        }
098                        else if (value instanceof Integer || value instanceof Long) {
099                                builder.addLong(key, ((Number)value).longValue());
100                        }
101                        else if (value instanceof Date) {
102                                builder.addDate(key, (Date) value);
103                        }
104                        else {
105                                log.debug("JobDataMap contains values which are not job parameters (ignoring).");
106                        }
107                }
108
109                return builder.toJobParameters();
110
111        }
112
113}