我正在尝试遵循“ Quartz Scheduling框架工作”一书“在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(无此类文件或目录)”
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();
最佳答案
要在容器加载时初始化调度程序,可以执行此操作。
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>
当容器加载时,这基本上会创建调度程序。然后,您可以使用我以前的文章从
StdSchedulerFactory
检索StdSchedulerFactory
。让我知道是否有问题。