当容器首次加载 Web 应用程序时初始化 QuartzScheduler

San*_*and 1 java struts2 quartz-scheduler

我正在尝试按照“ Quartz 调度框架”一书“在 Web 应用程序中初始化 Quartz ”示例中提到的步骤进行操作。这是该程序的链接https://gist.github.com/5777d9f27c700e716a5a。但是这个例子是在 Struts1 框架上的。

我们的是一个带有 Hibernate 3.5 ORM 的 struts2 框架。我应该如何在 Struts2 上配置确切的步骤。任何帮助,将不胜感激。

但是,如果我在 contextInitialized() 方法中编写代码,则会收到异常“java.lang.RuntimeException: java.io.FileNotFoundException: src/hibernate.cfg.xml (No such file or directory)”

Xml config = new Xml("src/hibernate.cfg.xml", "hibernate-configuration");
Properties prop = new Properties();
prop.setProperty("org.quartz.dataSource.tasksDataStore.driver", config.child("session-
                                      factory").children("property").get(1).content());
prop.setProperty("org.quartz.dataSource.tasksDataStore.URL", config.child("session-
                                      factory").children("property").get(2).content());
prop.setProperty("org.quartz.dataSource.tasksDataStore.user", config.child("session-
                                      factory").children("property").get(3).content());
prop.setProperty("org.quartz.dataSource.tasksDataStore.password", config.child("session-
                                      factory").children("property").get(4).content());
prop.setProperty("org.quartz.dataSource.tasksDataStore.maxConnections", "20");

SchedulerFactory sf = new StdSchedulerFactory(prop);
Scheduler sched = sf.getScheduler();
Run Code Online (Sandbox Code Playgroud)

Uch*_*nwu 5

要在容器加载时初始化调度程序,您可以这样做。

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzServletContextListener implements ServletContextListener
{
    public static final String QUARTZ_FACTORY_KEY = "org.quartz.impl.StdSchedulerFactory.KEY";
    private StdSchedulerFactory factory = null;

    /**
     * Called when the container is shutting down.
     */
    public void contextDestroyed(ServletContextEvent sce)
    {
        try
        {
            factory.getDefaultScheduler().shutdown();
        } catch (SchedulerException ex)
        {
        }

    }

    /**
     * Called when the container is first started.
     */
    public void contextInitialized(ServletContextEvent sce)
    {
        ServletContext ctx = sce.getServletContext();
        try
        {
            factory = new StdSchedulerFactory();

            // Start the scheduler now
            factory.getScheduler().start();
            ctx.setAttribute(QUARTZ_FACTORY_KEY, factory);

        } catch (Exception ex)
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的 web.xml 中,添加

<listener>
    <description>A Listener Class to initialize Quartz Scheduler</description>
    <listener-class>full_package_path.QuartzServletContextListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

这基本上在容器加载时创建调度程序。然后,您可以使用我之前的帖子StdSchedulerFactoryStdSchedulerFactory. 让我知道是否有问题。