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

Java 当容器首次加载web应用程序时初始化QuartzScheduler,java,struts2,quartz-scheduler,Java,Struts2,Quartz Scheduler,我正在尝试按照“Quartz调度框架”一书中提到的步骤在Web应用程序中初始化Quartz。这是该计划的链接。但这个例子是在Struts1框架上 我们的是一个带有Hibernate 3.5 ORM的struts2框架。我应该如何配置Struts2上的确切步骤。任何帮助都将不胜感激 但是如果我在contextInitialized()方法中编写代码,我会得到异常“java.lang.RuntimeException:java.io.FileNotFoundException:src/hiberna

我正在尝试按照“Quartz调度框架”一书中提到的步骤在Web应用程序中初始化Quartz。这是该计划的链接。但这个例子是在Struts1框架上

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

但是如果我在contextInitialized()方法中编写代码,我会得到异常“java.lang.RuntimeException:java.io.FileNotFoundException:src/hibernate.cfg.xml(没有这样的文件或目录)”

  • 首先,您需要在web.xml中进行配置
  • 其次,Struts2操作需要实现接口。此接口有一个方法,用于设置
  • 然后,您可以继续接收StdSchedulerFactory
  • 公共类MyClass实现ServletContextAware
    {

    希望这更清楚

  • 首先,您需要在web.xml中进行配置
  • 其次,您的Struts2操作需要实现接口。此接口有一个设置
  • 然后,您可以继续接收StdSchedulerFactory
  • 公共类MyClass实现ServletContextAware
    {


    希望这更清楚。

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

    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)
            {
            }
        }
    }
    
    在web.xml中,添加

    <listener>
        <description>A Listener Class to initialize Quartz Scheduler</description>
        <listener-class>full_package_path.QuartzServletContextListener</listener-class>
    </listener>
    
    
    用于初始化Quartz调度程序的侦听器类
    完整\u包\u路径.QuartzServletContextListener
    
    这基本上是在容器加载时创建调度程序。然后,您可以使用我的上一篇文章从
    StdSchedulerFactory
    检索
    StdSchedulerFactory

    如果有问题,请告诉我。

    要在容器加载时初始化计划程序,可以这样做

    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)
            {
            }
        }
    }
    
    在web.xml中,添加

    <listener>
        <description>A Listener Class to initialize Quartz Scheduler</description>
        <listener-class>full_package_path.QuartzServletContextListener</listener-class>
    </listener>
    
    
    用于初始化Quartz调度程序的侦听器类
    完整\u包\u路径.QuartzServletContextListener
    
    这基本上是在容器加载时创建调度程序。然后,您可以使用我的上一篇文章从
    StdSchedulerFactory
    检索
    StdSchedulerFactory


    如果有问题,请告诉我。

    那么,当容器加载时,它将进入execute方法?那么“execute()是什么方法以字符串形式返回?这在容器加载时不会运行。不,在容器加载时不会调用execute方法。如果希望在容器加载时实例化
    StdSchedulerFactory
    ,可以实现并注册
    ServletContextListener
    并在
    contextInitialize>中初始化调度程序d
    method.so,当容器加载时,它将进入execute方法?那么“execute()是什么方法以字符串形式返回?这在容器加载时不会运行。不,在容器加载时不会调用execute方法。如果希望在容器加载时实例化
    StdSchedulerFactory
    ,可以实现并注册
    ServletContextListener
    并在
    contextInitialize>中初始化调度程序d
    方法。这是一个很好的示例,但您确实应该对捕获的异常执行一些操作。为了简洁起见,您可能忽略了这些异常,但许多遇到这些异常的人只会盲目地复制/粘贴它。感谢这个示例,我剩下的一个查询是初始化时的DB连接属性。到目前为止,我是g从hibernate.cfg.xml设置数据库连接属性并将其添加到SchedulerFactory sf=new StdSchedulerFactory(属性)。您可以在我的帖子中查看代码。您的src/hibernate.cfg.xml在应用程序中的位置在哪里。在src/hibernate.cfg.xml中构建它之前。但是在/WEB-INF/Classes/hibernate.cfg.xml中构建它之后。我尝试指定这两个路径。但它们都不起作用。如果实际实例化,
    xml config=new xml(“src/hibernate.cfg.xml”,“hibernate配置”);
    读取类路径,我建议您删除src/。执行此操作
    xml config=new xml(“hibernate.cfg.xml”,“hibernate配置”)这是一个很好的例子,但是你真的应该对你捕获的异常做些什么。为了简洁起见,你可能忽略了它们,但是很多遇到这种情况的人都会盲目地复制/粘贴它。感谢这个例子,我剩下的一个查询是初始化时的DB连接属性。到目前为止,我得到了t从hibernate.cfg.xml获取数据库连接属性,并将其添加到SchedulerFactory sf=new StdSchedulerFactory(属性)。您可以在我的帖子中查看代码。您的src/hibernate.cfg.xml在应用程序中的位置在哪里。在src/hibernate.cfg.xml中构建它之前。但是在/WEB-INF/Classes/hibernate.cfg.xml中构建它之后。我尝试指定了这两个路径。但是它们都不起作用。如果实际实例化,
    xml config=new xml(“src/hibernate.cfg.xml,“hibernate配置”);
    从类路径读取,我建议您删除src/。执行此操作
    xml config=new xml(“hibernate.cfg.xml”,“hibernate配置”);
    如果解决方案适合您,我建议您勾选答案以帮助其他用户。您的解决方案非常适合我..谢谢..:)我使用的类实现ServletContextListener&实例化Quartz调度程序类并启动调度程序。但是hibernate.cfg.xml(db连接属性是剩下的一个问题)如果解决方案对您有效,我建议您勾选答案以帮助其他用户。您的解决方案对我非常有效..谢谢..:)我使用的类实现ServletContextListener&实例化Quartz调度程序类并启动调度程序。但是hibernate.cfg.xml(db连接属性是剩下的一个问题)