作为客户端,推荐使用哪种方式来处理调度程序?

发布于 2024-09-18 09:55:12 字数 1183 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作业与我同在 2024-09-25 09:55:13

我认为这两个选项的作用都是创建一个新的调度程序,而不是获取您在 web.xml 中配置的调度程序。

目前我正在这样做(类似于选项1):

...configure a QuartzInitializerListener for my webapp...
// my servlet's init()
public void init(ServletConfig config) throws ServletException {
    ...
    SchedulerFactory factory =
            (SchedulerFactory) config.getServletContext().getAttribute(
                QuartzInitializerListener.QUARTZ_FACTORY_KEY
            );
    Scheduler factory.getScheduler();
    ...
}

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

...configure a QuartzInitializerListener for my webapp...
// my servlet's init()
public void init(ServletConfig config) throws ServletException {
    ...
    SchedulerFactory factory =
            (SchedulerFactory) config.getServletContext().getAttribute(
                QuartzInitializerListener.QUARTZ_FACTORY_KEY
            );
    Scheduler factory.getScheduler();
    ...
}
场罚期间 2024-09-25 09:55:13

您在这里描述的是单例模式,并且您的单例初始化不是线程安全的。

我建议不要在这里使用单例,而是向 ServletContext 添加对 Quartz 的引用,可能会在 ServletContextListener 中初始化它。

web.xml

<listener>
  <listener-class>my.package.MyServletContextListener</listener-class>
</listener>

MyServletContextListener.java

package my.package;

public class MyServletContextListener implements ServletContextListener
{
    @Override
    public final void contextInitialized (
            final ServletContextEvent event
        )
    {
        final ServletContext servlet_ctx = event.getServletContext( );

        Quartz quartz = new Quartz( );

        servlet_ctx.addAttribute( Quartz.ATTRIBUTE_NAME, quartz );
    }

    @Override
    public void contextDestroyed (
            final ServletContextEvent event
        )
    {
        final ServletContext servlet_ctx = event.getServletContext( );

        Quartz.fromContext( servlet_ctx ).shutdown( );

        servlet_ctx.removeAttribute( Quartz.ATTRIBUTE_NAME );
    }
}

Quartz.java

public final static String ATTRIBUTE_NAME = Quartz.class.getName( );

public Quartz( )
{
    ...
}

public static Quartz fromContext( final ServletContext ctx )
{
  return ctx.getAttribute( ATTRIBUTE_NAME );
}

public void shutdown( )
{
    ...
}

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 to ServletContext, possibly initializing it in ServletContextListener.

web.xml

<listener>
  <listener-class>my.package.MyServletContextListener</listener-class>
</listener>

MyServletContextListener.java

package my.package;

public class MyServletContextListener implements ServletContextListener
{
    @Override
    public final void contextInitialized (
            final ServletContextEvent event
        )
    {
        final ServletContext servlet_ctx = event.getServletContext( );

        Quartz quartz = new Quartz( );

        servlet_ctx.addAttribute( Quartz.ATTRIBUTE_NAME, quartz );
    }

    @Override
    public void contextDestroyed (
            final ServletContextEvent event
        )
    {
        final ServletContext servlet_ctx = event.getServletContext( );

        Quartz.fromContext( servlet_ctx ).shutdown( );

        servlet_ctx.removeAttribute( Quartz.ATTRIBUTE_NAME );
    }
}

Quartz.java

public final static String ATTRIBUTE_NAME = Quartz.class.getName( );

public Quartz( )
{
    ...
}

public static Quartz fromContext( final ServletContext ctx )
{
  return ctx.getAttribute( ATTRIBUTE_NAME );
}

public void shutdown( )
{
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文