/*
* 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 com.greedchina.gcrawler.batch;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.configuration.JobLocator;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* @author Dave Syer
*
*/
public class JobLauncherDetails extends QuartzJobBean {
/**
* Special key in job data map for the name of a job to run.
*/
static final String JOB_NAME = "jobName";
private static Log log = LogFactory.getLog(JobLauncherDetails.class);
private JobLocator jobLocator;
private JobLauncher jobLauncher;
/**
* Public setter for the {@link JobLocator}.
* @param jobLocator the {@link JobLocator} to set
*/
public void setJobLocator(JobLocator jobLocator) {
this.jobLocator = jobLocator;
}
/**
* Public setter for the {@link JobLauncher}.
* @param jobLauncher the {@link JobLauncher} to set
*/
public void setJobLauncher(JobLauncher jobLauncher) {
this.jobLauncher = jobLauncher;
}
protected void executeInternal(JobExecutionContext context) {
Map<String, Object> jobDataMap = context.getMergedJobDataMap();
String jobName = (String) jobDataMap.get(JOB_NAME);
log.info("Quartz trigger firing with Spring Batch jobName="+jobName);
JobParameters jobParameters = getJobParametersFromJobMap(jobDataMap);
try {
jobLauncher.run(jobLocator.getJob(jobName), jobParameters);
}
catch (JobExecutionException e) {
log.error("Could not execute job.", e);
}
}
/*
* Copy parameters that are of the correct type over to
* {@link JobParameters}, ignoring jobName.
*
* @return a {@link JobParameters} instance
*/
private JobParameters getJobParametersFromJobMap(Map<String, Object> jobDataMap) {
JobParametersBuilder builder = new JobParametersBuilder();
builder.addLong("time", System.currentTimeMillis());
for (Entry<String, Object> entry : jobDataMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof String && !key.equals(JOB_NAME)) {
builder.addString(key, (String) value);
}
else if (value instanceof Float || value instanceof Double) {
builder.addDouble(key, ((Number) value).doubleValue());
}
else if (value instanceof Integer || value instanceof Long) {
builder.addLong(key, ((Number)value).longValue());
}
else if (value instanceof Date) {
builder.addDate(key, (Date) value);
}
else {
log.debug("JobDataMap contains values which are not job parameters (ignoring).");
}
}
return builder.toJobParameters();
}
}
动漫网页爬虫.zip
版权申诉
148 浏览量
2024-03-08
11:43:00
上传
评论
收藏 34KB ZIP 举报
![avatar](https://profile-avatar.csdnimg.cn/2b17cb8b32224168bb8ed166a94dd8f6_weixin_56154577.jpg!1)
JJJ69
- 粉丝: 6266
- 资源: 5775
最新资源
- 基于Java web的学生管理系统(源码+数据库+报告)高分项目
- 基于Java web的学生管理系统(源码+数据库+报告)期末大作业&课程设计
- FM1702SL芯片13.56MHZ NFC读卡器开发板PROTELPCB图+FM1702SL中文说明书+FM1715编程指南
- 期末大作业交通数据分析与应用期末作业程序源码+实验报告.zip
- 期末大作业基于Java web的图书销售管理系统(源码+数据库)高分项目
- python-leetcode面试题解之第274题H指数.zip
- python-leetcode面试题解之第270题最接近二叉搜索树值.zip
- python-leetcode面试题解之第267题回文排列II.zip
- python-leetcode面试题解之第264题丑数II.zip
- python-leetcode面试题解之第263题丑数.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)