问答详情
源自:2-1 第一个Quartz程序

如何传递对象类型的数据

如何传递对象类型的数据

提问者:qq_阿猫_2 2018-09-25 19:00

个回答

  • maybe丶xg
    2018-10-16 15:39:41

    public TriggerBuilder<T> usingJobData(JobDataMap newJobDataMap)

    trigger的usingJobData有这个方法;


    User user = new User();
    user.setAge(12);
    user.setName("Jack");
    
    JobDataMap dataMap = new JobDataMap();
    dataMap.put("user",user);
    SimpleTrigger trigger = TriggerBuilder.newTrigger()
            .withIdentity("myTrigger", "group1")
            .usingJobData(dataMap)
            .startAt(date)
            .endAt(endDate)
            .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).withRepeatCount(3))
            .build();


    Trigger currentTrigger = jobExecutionContext.getTrigger();
    currentTrigger.getStartTime();
    currentTrigger.getEndTime();
    
    JobDataMap jobDataMap = currentTrigger.getJobDataMap();
    System.out.println("user is:" + jobDataMap.get("user"));
    User user = (User) jobDataMap.get("user");
    System.out.println(user.getAge());