Search code examples
javaspringquartz-scheduler

pass JobDataMap in the JobDetailFactoryBean not working


I'm using Quartz 2.2.1. My code is like follows.

<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;
    }
}

but I always get param null and jobdatamap 0. I checked several examples in web and those are same like mine.

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

what is I am missing?


Solution

  • I have use the SchedulerFactoryBean to pass JobDataMap and it is working for me.

    <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>