在JobDetailFactoryBean中传递JobDataMap不起作用

时间:2018-01-18 13:12:56

标签: java spring quartz-scheduler

我正在使用Quartz 2.2.1。我的代码如下。

<bean name="myJob"  class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    <property name="jobClass" value="com.my.MyJob" />
    <property name="durability" value="true" />
    <property name="jobDataAsMap">
        <map>
            <entry key="param" value="myParam" />
        </map>
    </property>
</bean>
<bean id="myTrigger"  class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="myJob" />
    <property name="cronExpression" value="0 0/1 * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="jobDetails">
        <list>
            <ref bean="myJob" />
        </list>
    </property>
    <property name="triggers">
        <list>
            <ref bean="myTrigger" />
        </list>
    </property>
    <property name="quartzProperties" ref="quartzProperties"/>
</bean>

java class

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class MyJob extends QuartzJobBean {
    private String param;

    private final org.slf4j.Logger log = LoggerFactory.getLogger(getClass());

    @Override
    protected void executeInternal(JobExecutionContext jobContext) throws JobExecutionException {
        JobDataMap jobDataMap = jobContext.getJobDetail().getJobDataMap();
        log.debug("size of jobdatamap {}", jobDataMap.keySet().size());
        log.debug("quartz param {}", param);

        // some code
    }

    public void setParam(String param) {
        this.param = param;
    }

    public String getParam() {
        return param;
    }
}

但我总是得到param nulljobdatamap 0。我在网上查了几个例子,和我的一样。

org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.makeSchedulerThreadDaemon = true

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 1
org.quartz.threadPool.makeThreadsDaemons = true

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
   org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.dataSource = myDB
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = false

org.quartz.dataSource.myDB.jndiURL = /jdbc/myDB

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我使用SchedulerFactoryBean来传递JobDataMap,这对我有用。

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="jobDetails">
        <list>
            <ref bean="myJob" />
        </list>
    </property>
    <property name="triggers">
        <list>
            <ref bean="myTrigger" />
        </list>
    </property>
    <property name="quartzProperties" ref="quartzProperties"/>
    <property name="schedulerContextAsMap">
        <map>
            <entry key="param" value="myParam" />
        </map>
    </property>
</bean>