如何使用Quartz Scheduler和日志记录创建Web应用程序
我有時會在Quartz Scheduler論壇中為用戶提供幫助。 有時,有人會問他/她如何在Web應用程序中設置Quartz。 實際上,這是一件相當簡單的事情。 該庫已經帶有一個ServletContextListener ,您可以使用它啟動調度程序。 我將在這里向您展示一個簡單的webapp示例。
首先創建一個Maven pom.xml文件。
然后,您需要創建一個src/main/webapp/META-INF/web.xml文件。
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param><param-name>quartz:config-file</param-name><param-value>quartz.properties</param-value></context-param><context-param><param-name>quartz:shutdown-on-unload</param-name><param-value>true</param-value></context-param><context-param><param-name>quartz:wait-on-shutdown</param-name><param-value>true</param-value></context-param><context-param><param-name>quartz:start-on-load</param-name><param-value>true</param-value></context-param><listener><listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class></listener></web-app>最后,您需要Scheduler的src/main/resources/quartz.properties配置文件。
# Main Quartz configuration org.quartz.scheduler.skipUpdateCheck = true org.quartz.scheduler.instanceName = MyQuartzScheduler org.quartz.scheduler.jobFactory.class = org.quartz.simpl.SimpleJobFactory org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 5您可以使用Quartz配置許多其他功能 ,但是上面的內容應該像在內存調度程序中一樣使您入門。
現在,您應該可以編譯并運行它了。
bash> mvn compile bash> mvn org.apache.tomcat.maven:tomcat7-maven-plugin:2.1:run -Dmaven.tomcat.port=8081如何為Quartz Scheduler配置日志記錄
另一個常見問題是他們如何設置日志記錄并查看DEBUG級別的消息。 Quartz Scheduler使用SLF4J,因此您可以選擇許多記錄器選項 。 下面,我將向您展示如何設置Log4j。
首先,將其添加到您的pom.xml
<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.5</version></dependency>然后添加src/main/resources/log4j.properties文件以將消息顯示到STDOUT上。
log4j.rootLogger=INFO, stdout log4j.logger.org.quartz=DEBUG log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n在命令行上重新啟動Web應用程序,現在您應該看到所有來自Quartz庫的DEBUG級別的日志記錄消息。
在一切正常運行的情況下,您的下一個問題可能是詢問您如何從Web應用程序訪問調度程序? 好了,當調度程序由Servlet上下文偵聽器創建時,它使用org.quartz.impl.StdSchedulerFactory.KEY鍵存儲在Web應用程序的ServletContext空間中。 因此,您可以像下面這樣檢索它并在您自己的Servlet中使用它:
public class YourServlet extends HttpServlet {public init(ServletConfig cfg) {String key = "org.quartz.impl.StdSchedulerFactory.KEY";ServletContext servletContext = cfg.getServletContext();StdSchedulerFactory factory = (StdSchedulerFactory) servletContext.getAttribute(key);Scheduler quartzScheduler = factory.getScheduler("MyQuartzScheduler");// TODO use quartzScheduler here.} }現在,您正在構建下一個調度應用程序!
玩得開心!
翻譯自: https://www.javacodegeeks.com/2013/09/how-to-create-web-app-with-quartz-scheduler-and-logging.html
總結
以上是生活随笔為你收集整理的如何使用Quartz Scheduler和日志记录创建Web应用程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 孚怎么读 孚的意思
- 下一篇: 无法保存打印机设置0x000006d9怎