简体   繁体   中英

How to add a quartz JobListener in XML configuration (spring-framework)

I have my quartz scheduler,triggers & job configured in XML similar to this- https://docs.spring.io/spring-framework/docs/1.2.x/reference/scheduling.html .

I want to add a jobListener to this scheduler which would fire another job on completion of job1 ( in jobWasExecuted method of listener), but I am unable to find any XML based usage of jobListener.

This is what I have tried so far, but bean is not getting initialized after adding listeners-

Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'listeners' of bean class [org.springframework.scheduling.quartz.SchedulerFactoryBean]: Bean property 'listeners' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

<bean id="myScheduler"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="myTrigger" />
        </list>
    </property>
    <property name="listeners">
        <list>
            <ref bean="completionListener" />
        </list>
    </property>
    <property name="quartzProperties">
        <props>
            <prop key="org.quartz.scheduler.skipUpdateCheck">true</prop>
        </props>
    </property>
</bean>

<bean name="myTrigger"
    class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="myJobDetail" />
    <property name="cronExpression" value="0 3 0/1 * * ?" />
</bean>

<bean id="myJobDetail"
    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="ABC" />
    <property name="targetMethod" value="refresh" />
    <property name="concurrent" value="false" />
</bean>

<bean id="completionListener" class="com.dummy.MyListener"/>

This is MyListener class -

public class MyListener implements JobListener {
    @Override
    public String getName() {
        return "myListener";
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext jobExecutionContext) {

    }

    @Override
    public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) {

    }

    @Override
    public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException e) {
        log.info("First Job was executed");
        Scheduler scheduler = jobExecutionContext.getScheduler();
        try {
            // Create the chained JobDetail
            JobKey jobKey = new JobKey("dummyJobName", "group1");
            JobDetail jobDetail = JobBuilder.newJob(DummyJob.class)
                    .withIdentity(jobKey).build();

            // Create a one-time trigger that fires immediately
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("triggerNextJob", "group1")
                    .startNow()
                    .build();

            // Schedule the next job to fire immediately
            scheduler.scheduleJob(jobDetail, trigger);

            log.info(DummyJob.class.getName() +
                    " has been scheduled executed");

        } catch (Exception ex) {
            log.error("Couldn't chain next Job", ex);
            return;
        }
    }
}

The problem is in

<bean id="completionListener" class="com.dummy.MyListener"/>

You need to give property name like below

<bean id="completionListener" class="com.dummy.MyListener">
     <property name="myListener" ref="myListener"></property>
</bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM