Quartz基本使用(二)

本文介绍了如何使用Quartz框架创建JobDetail和Trigger实例,并详细解释了如何通过usingJobData方法来传递不同类型的参数,包括如何在执行方法中获取这些参数。

一、创建JobDetail及Trigger实例

//通过usingJobData方法进行参数传递
//设置不同的参数类型进行参数传递

JobDetail jobDetail = (JobDetail) JobBuilder.newJob(HelloJob.class)
                .withIdentity("myJob","groupOne").usingJobData("message","this is a JobDetail").usingJobData("FloatValue",30.1415F).build();

//通过usingJobData方法进行参数传递
//设置不同的参数类型进行参数传递
SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("simpleTrigger","group1").
                withSchedule(SimpleScheduleBuilder.simpleSchedule().                        withIntervalInSeconds(2).repeatForever()).startNow().usingJobData("messages","this is a trigger").usingJobData("DoubleValue",2.4D).build();
    

二、接收参数

public void execute(JobExecutionContext context) throws JobExecutionException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("start print time: "+sdf.format(new Date()));
        System.out.println("Hello world");

       JobDetail jobDetail =  context.getJobDetail();
        //获取任务组
        System.out.println("group "+jobDetail.getKey().getGroup());
        //获取任务名称
        System.out.println("name "+jobDetail.getKey().getName());
        //任务参数获取   
        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        System.out.println("message "+jobDataMap.getString("message"));
        System.out.println("FloatValue"+ jobDataMap.getFloat("FloatValue"));
       Trigger trigger =  context.getTrigger();
        //获取调度组
        System.out.println("group "+trigger.getKey().getGroup());
        //获取调度名称
        System.out.println("name "+trigger.getKey().getName());
        //调度参数获取   
        JobDataMap tigDataMap = context.getTrigger().getJobDataMap();
        System.out.println("messages "+tigDataMap.getString("messages"));
        System.out.println("DoubleValue"+ tigDataMap.getDouble("DoubleValue"));
    }

若传输的参数名称出现重复的话(key重复),则会覆盖JobDetail中的key。获取Trigger中的key对应的值

三、还可通过set方法进行参数获取

private String message;

private String messages;

private Float FloatValue;

private Double DoubleValue;


   public void setMessage(String message){
    	 this.message= message;
     }
   public void setMessages(String messages){
    	 this.messages= messages;
     }
   public void setFloatValue(Float FloatValue){
    	 this.FloatValue= FloatValue;
     }
   public void setDoubleValue(Double DoubleValue){
    	 this.DoubleValue= DoubleValue;
     }
 public void execute(JobExecutionContext context) throws JobExecutionException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("start print time: "+sdf.format(new Date()));
        System.out.println("Hello world");       
        
        System.out.println("message "+message);
        System.out.println("FloatValue"+ FloatValue));
     
      
        System.out.println("messages "+messages);
        System.out.println("DoubleValue"+ DoubleValue);
    }

 

转载于:https://my.oschina.net/u/2251646/blog/1502840

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值