Spring Quartz:作业未触发

Spring Quartz : Job does not fire

我正在使用 Quartz 2 和 Spring 3.0

我想使用 SchedulerFactoryBean 但我的作业没有被解雇。

下面是我的XML文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="quartzScheduler"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="autoStartup" value="true"/>
        <property name="schedulerName" value="PCLoaderScheduler"/>
    </bean>

</beans>

我的代码如下:

@Component
public class PCSchedulerManager {

    @Autowired
    private Scheduler scheduler;

    public void scheduleJob(final Map<String, Object> parameters, Class inputClass) throws PCSchedulerException {

        try {
            long currentTimeStamp = System.currentTimeMillis();

            JobDetail job = JobBuilder
                            .newJob(inputClass)
                            .withIdentity(inputClass.getName() + currentTimeStamp)
                            .build();
            job.getJobDataMap().putAll(parameters);

            Trigger trigger = TriggerBuilder
                                .newTrigger()
                                .withIdentity(inputClass.getName() + currentTimeStamp)
                                .build();

            //Schedule a job with JobDetail and Trigger
            scheduler.scheduleJob(job, trigger);

        } catch (SchedulerException e) {
            throw new PCSchedulerException(e);
        }
    }
}

请参考我正在尝试执行的作业

public class LoaderJob implements Job {

    public void execute(JobExecutionContext jec) throws JobExecutionException {
        System.out.println("Do your stuff here...");
    }

}

我知道调度程序在服务器启动时启动。但这不是 运行 我的工作。

此外,如果我使用下面的语句而不是自动装配 Spring Quartz 调度程序,那么作业将成功触发

scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();

请让我知道我做错了什么...

是否需要在作业中注入依赖项?然后执行QuartzJobBean

并覆盖 executeInternal() 方法。

另请检查作业执行时是否出现异常。

首先,继续将类似的内容添加到您的配置 xml 文件中。请注意如何更改 repeatInterval 和 startDelay 属性。或者,您也可以使用 cron 表达式。从 this link.

了解它们
   <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers">
         <list>             
            <ref bean="cronTrgTest" />
         </list>
      </property>
   </bean>

    <bean id="cronTrgTest" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="testJob" />
        <property name="repeatInterval" value="5000" />
        <property name="startDelay" value="1000" />
    </bean>   

   <bean id="testJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <property name="targetObject" ref="cronTest" />
      <property name="targetMethod" value="test" />
   </bean>   

   <bean id="cronTest" class="com.mustafaergin.ws.cron.CronTest">
   </bean>

然后实现目标POJO就大功告成了。

public class CronTest {

    public void test() {
        System.out.println("TEST");
    }

}

你可以找到我前阵子写的原文here

Quartz 2 和 Spring 3.0 不兼容。将 Spring 更新为 3.1。现在它工作正常