问题描述
我有一个 Web 应用程序,我正在尝试在春季以编程方式启动 Quartz 调度程序.我有一个服务类,我在其中创建了 SchedulerFactory 的一个实例,然后获取了一个调度程序.
I have a web application and I am trying to start Quartz scheduler programmatically in spring. I have a service class where I create an instance of SchedulerFactory and then get a scheduler.
代码如下.
@Service("auctionWinnerService")
public class NormalAuctionWinnerServiceImpl implements AuctionWinnerService {
public static final String NORMAL_AUCTION = "NORMAL AUCTION";
public static int NORMAL_AUCTION_COUNTER = 0;
private SchedulerFactory schedulerFactory;
private Scheduler scheduler;
public void declareWinner(int auctionId, Map<String, Object> parameterMap) {
System.out.println("INSIDE declareWinner of NormalAuctionWinner");
schedulerFactory = new StdSchedulerFactory();
try {
scheduler = schedulerFactory.getScheduler();
System.out.println("GOT SCHEDULER : "+scheduler);
} catch (SchedulerException e1) {
e1.printStackTrace();
}
JobDetail jd = new JobDetail();
jd.setName(NORMAL_AUCTION+" JOB "+NORMAL_AUCTION_COUNTER);
jd.setJobClass(NormalAuctionWinnerJob.class);
/** CREATE CRON TRIGGER INSTANCE **/
CronTrigger t = new CronTrigger();
t.setName(NORMAL_AUCTION + ++NORMAL_AUCTION_COUNTER);
t.setGroup("Normal Auction");
Date d = new Date();
Date d1 = new Date();
d1.setMinutes(d.getMinutes()+5);
t.setStartTime(d);
t.setEndTime(d1);
try {
t.setCronExpression("10 * * * * ? *");
scheduler.scheduleJob(jd, t);
} catch (SchedulerException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
schedulerFactory 和 scheduler 已实例化,但我的作业未运行.有人能指出我在这里遗漏了什么吗?
The schedulerFactory and scheduler are instantiated but my jobs do not run.Could someone point out what am I missing here?
此外,我只需要一个 Factory 实例和一个调度程序实例.我尝试制作静态,但没有用.在这个方向上的任何指示都会有所帮助.
Also I need only one instance of Factory and one scheduler instance. I tried making the static but it didn't work. Any pointers in this direction will be helpful.
谢谢
推荐答案
除非您对 Quartz 的专有功能有特定要求,否则我建议您摆脱它并使用 Spring 的内部调度能力.从 Spring 3 开始,这包括对 cron 类型表达式的支持,非常类似于 Quartz 的 cron 触发器.
Unless you have a specific requirement on Quartz's proprietary functionality, I recommend getting rid of it and using Spring's internal scheduling capability. As of Spring 3, this includes support for cron-type expressions, very similar to Quartz's cron trigger.
除了为您的应用程序及其配置带来简单性之外,它本质上比 Quartz 更可靠,并通过 TaskScheduler
接口为编程使用提供更简单的 API.
As well as bringing simplicity to your application and its config, it's inherently more reliable than Quartz, and provides an easier API for programmatic usage, via the TaskScheduler
interface.
这篇关于Quartz 与 Spring 的集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!