作为客户端,推荐使用哪种方式来处理调度程序?
我正在使用 Quartz Scheduler v1.8.3 并集成到 Apache Tomcat v6.0.10 中,因此调度程序是 从 servlet 容器内初始化。作业也可以从 Apache Tomcat 内添加/调度到调度程序。
我正在设计一个 JSP Web 前端页面 (quartzAdmin.jsp),我只想在其中查看调度程序信息,例如 元数据 值、所有作业详细信息及其触发器、当前正在执行的作业等。
我的问题是,为了获得调度程序的句柄,推荐使用以下两种选项之一:
选项 1:通过直接调用 SchedulerFactory.getScheduler() 获取句柄
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
选项 2:通过实现单例模式获取句柄 strong>
public class Quartz {
private Quartz() {
}
private static Scheduler scheduler = null;
public static Scheduler getScheduler() {
if(scheduler == null) {
SchedulerFactory sf = new StdSchedulerFactory();
scheduler = sf.getScheduler();
}
return scheduler;
}
}
然后在我的quartzAdmin.jsp 中,只需调用Quartz.getScheduler() 即可返回调度程序的句柄。
I'm using Quartz Scheduler v1.8.3 and is integrated into Apache Tomcat v6.0.10, and hence scheduler is initialized from within servlet container. Jobs are also added/scheduled to scheduler from within Apache Tomcat.
I'm designing a JSP web front end page (quartzAdmin.jsp) from where I only want to see scheduler information like meta-data values, all job details along with its triggers, currently executing jobs, etc.
My question is, in order to get a handle to the scheduler, which one of the below 2 option is recommended:
Option 1: Getting handle by directly calling schedulerFactory.getScheduler()
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
Option 2: Getting handle by implementing singleton pattern
public class Quartz {
private Quartz() {
}
private static Scheduler scheduler = null;
public static Scheduler getScheduler() {
if(scheduler == null) {
SchedulerFactory sf = new StdSchedulerFactory();
scheduler = sf.getScheduler();
}
return scheduler;
}
}
Then in my quartzAdmin.jsp, just a call Quartz.getScheduler() would return a handle to the scheduler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这两个选项的作用都是创建一个新的调度程序,而不是获取您在 web.xml 中配置的调度程序。
目前我正在这样做(类似于选项1):
I think what both options do is create a new Scheduler, not getting the one you have configured in
web.xml
.Currently I am doing this (similar to Option 1):
您在这里描述的是单例模式,并且您的单例初始化不是线程安全的。
我建议不要在这里使用单例,而是向
ServletContext
添加对Quartz
的引用,可能会在ServletContextListener
中初始化它。web.xml
MyServletContextListener.java
Quartz.java
What you are describing here is a singleton pattern, and your singleton initialization is not thread-safe.
I would recommend not using singleton here, but instead add a reference to
Quartz
toServletContext
, possibly initializing it inServletContextListener
.web.xml
MyServletContextListener.java
Quartz.java