quartz定时任务_job实现类中获取传参02

Posted imlvbu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了quartz定时任务_job实现类中获取传参02相关的知识,希望对你有一定的参考价值。

1.main方法:

public static void main(String[] args) throws SchedulerException {
// TODO Auto-generated method stub

// 创建一个JobDetail实例 将HelloJob类添加到JobDetail中
JobDetail jobDetail = JobBuilder.newJob(helloJob.class)
.withIdentity("myJob", "jobDetailGroup")
.usingJobData("message1","date1")
.usingJobData("float1", 3.14f)
.build();
System.out.println(jobDetail.getKey().getName());
System.out.println(jobDetail.getKey().getGroup());
System.out.println(jobDetail.getJobClass().getName());

//创建一个Trigger实例,定义该job立即执行,每隔两秒钟重复执行一次,一直不停
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("myTrigger","triggerGroup")
.usingJobData("triggerParam1","trigerParam1Value")
.startNow().withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(2)
.repeatForever())
.build();
//创建Schduler实例
SchedulerFactory sfFactory = new StdSchedulerFactory();
Scheduler scheduler = sfFactory.getScheduler();
scheduler.start();

//打印当前时间
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("current scheduler time is :"+sdf.format(date));
scheduler.scheduleJob(jobDetail, trigger);

}

 

2.job实现类:

//实现job接口execute方法具体内容
public void execute(JobExecutionContext arg0) throws JobExecutionException{
//打印当前执行时间 格式:2017-01-01 00:00:00
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("job execute time is :"+sdf.format(date));

//编写具体业务逻辑
System.out.println("helloJob类 实现了 Job接口的execute方法");

JobKey jobkey=arg0.getJobDetail().getKey();
System.out.println("Job name:"+jobkey.getName()+" and Job group:"+jobkey.getGroup());

TriggerKey trkey = arg0.getTrigger().getKey();
System.out.println("Trigger name:"+trkey.getName()+" and Trigger group:"+trkey.getGroup());

JobDataMap jobDM = arg0.getJobDetail().getJobDataMap();
JobDataMap triDM = arg0.getTrigger().getJobDataMap();

String jobMsg = jobDM.getString("message1");
Float jobFloatValue =jobDM.getFloat("float1");
String triMsg = triDM.getString("triggerParam1");

System.out.println("jobMsg:"+jobMsg);
System.out.println("jobFloatValue:"+jobFloatValue);
System.out.println("triMsg:"+triMsg);
}

 

3.运行结果:

current scheduler time is :2018-01-03 17:43:59
job execute time is :2018-01-03 17:43:59
helloJob类 实现了 Job接口的execute方法
Job name:myJob and Job group:jobDetailGroup
Trigger name:myTrigger and Trigger group:triggerGroup
jobMsg:date1
jobFloatValue:3.14
triMsg:trigerParam1Value
job execute time is :2018-01-03 17:44:01
helloJob类 实现了 Job接口的execute方法
Job name:myJob and Job group:jobDetailGroup
Trigger name:myTrigger and Trigger group:triggerGroup
jobMsg:date1
jobFloatValue:3.14
triMsg:trigerParam1Value

以上是关于quartz定时任务_job实现类中获取传参02的主要内容,如果未能解决你的问题,请参考以下文章

Quartz--01

Quartz总结:定时任务中使用业务类(XXService)

quartz-job实现实时或定时发送短信任务

Spring+quartz 实现定时任务job集群配置

怎样动态的配置job和cronExpression?应用quartz实现定时任务

spring-boot-route(二十一)quartz实现动态定时任务