{"id":717,"date":"2013-02-01T22:45:08","date_gmt":"2013-02-01T22:45:08","guid":{"rendered":"http:\/\/www.softwareeverydayblog.com\/?p=717"},"modified":"2013-02-02T06:01:02","modified_gmt":"2013-02-02T06:01:02","slug":"quartz-within-tomcat-using-xml-job-descriptions","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=717","title":{"rendered":"Quartz within Tomcat using XML job descriptions"},"content":{"rendered":"<p>Objective is to make a counter using Quartz. Step by step we&#8217;ll do the following<\/p>\n<ol>\n<li>Create a custom context listener to initialize a counter,<\/li>\n<li>Create quartz properties file and xml jobs file,<\/li>\n<li>Schedule a job that would keep incrementing the counter,<\/li>\n<li>Create a servlet to view that counter.<\/li>\n<\/ol>\n<p>1. <strong>Create a custom context listener to initialize a counter<\/strong>:- If we need to listen to various events within the servlet container event listeners give us the flexibility to do that. We have Servlet context-level (application-level) events and Session-level events. For more, look at the <a href=\"http:\/\/docs.oracle.com\/cd\/B15904_01\/web.1012\/b14017\/filters.htm#i1000654\" title=\"Event Listeners\">documentation<\/a>.<\/p>\n<p>&#8220;After the application starts up and before it services the first request, the servlet container creates and registers an instance of each listener class that you have declared. For each event category, listeners are registered in the order in which they are declared. Then, as the application runs, event listeners for each category are invoked in the order of their registration. All listeners remain active until after the last request is serviced for the application.&#8221;<\/p>\n<p>We declare all the listeners we need in the xml file. QuartzInitializerListener will create the scheduler (MyScheduler) from the quartz.properties and set scheduler factory instance into the servletContext.<\/p>\n<pre lang=\"xml\" line=\"1\">\r\n<context-param>\r\n  <param-name>initcounter<\/param-name>\r\n  <param-value>100<\/param-value>\r\n<\/context-param>\r\n<listener>\r\n  <listener-class>\r\n\tcom.napp.listener.MyServletContextListener\r\n  <\/listener-class>\t\r\n  <listener-class>\r\n\torg.quartz.ee.servlet.QuartzInitializerListener\r\n  <\/listener-class>\r\n<\/listener>\r\n<\/pre>\n<pre lang=\"java\" line=\"1\">\r\npublic class MyServletContextListener implements ServletContextListener {\r\n  public static int counter = 0;\r\n  final static Logger logger = LoggerFactory.getLogger(MyServletContextListener.class);\r\n\r\n  public void contextDestroyed(ServletContextEvent arg0) {\r\n    \/\/ TODO Auto-generated method stub\r\n  }\r\n\r\n  public void contextInitialized(ServletContextEvent arg0) {\r\n    String initCounter = arg0.getServletContext().getInitParameter(\"initcounter\");\r\n    if ( initCounter!= null) {\r\n\tcounter = Integer.parseInt(initCounter);\r\n\tlogger.info(\"Set counter-\" + counter);\r\n    } else {\r\n\tlogger.info(\"Coulnt read init counter, default is 0\");\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>2. <strong>Create quartz properties file and xml jobs file<\/strong>:- Quartz.properties that contains information that the Scheduler Factory uses to create a scheduler.<\/p>\n<pre lang=\"text\" line=\"1\">\r\norg.quartz.scheduler.instanceName = MyScheduler\r\norg.quartz.threadPool.threadCount = 3\r\norg.quartz.jobStore.class = org.quartz.simpl.RAMJobStore\r\norg.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin\r\norg.quartz.plugin.jobInitializer.fileNames = jobs.xml\r\norg.quartz.plugin.jobInitializer.failOnFileNotFound = true\r\n#org.quartz.plugin.jobInitializer.scanInterval = 1\r\n#org.quartz.plugin.jobInitializer.wrapInUserTransaction = false\r\n<\/pre>\n<pre lang=\"xml\" line=\"1\">\r\n<job-scheduling-data\r\n  xmlns=\"http:\/\/www.quartz-scheduler.org\/xml\/JobSchedulingData\"\r\n  xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n  xsi:schemaLocation=\"http:\/\/www.quartz-scheduler.org\/xml\/JobSchedulingData http:\/\/www.quartz-scheduler.org\/xml\/job_scheduling_data_1_8.xsd\"\r\n  version=\"1.8\">\r\n  <schedule>\t\r\n    <job>\r\n\t<name>hello-world-job<\/name>\r\n\t<group>hello-world-group<\/group>\r\n\t<description>Print a welcome message<\/description>\r\n        <job-class>com.napp.job.HelloWorldScheduledJob<\/job-class>\r\n        <job-data-map>\r\n          <entry>\r\n\t    <key>param1<\/key>\r\n\t    <value>value1<\/value>\r\n          <\/entry>\r\n        <\/job-data-map>\r\n    <\/job>\r\n    <trigger>\t\r\n\t<cron>\r\n\t  <name>helloworld-trigger<\/name>\r\n\t  <job-name>hello-world-job<\/job-name>\r\n\t  <job-group>hello-world-group<\/job-group>\r\n\t  <!-- It will run every 5 seconds -->\r\n\t  <cron-expression>0\/5 * * * * ?<\/cron-expression>\r\n\t<\/cron>\t\r\n    <\/trigger>\r\n  <\/schedule>\r\n<\/job-scheduling-data>\r\n<\/pre>\n<p>3. <strong>Schedule a job that would keep incrementing the counter<\/strong>:- <\/p>\n<pre lang=\"java\" line=\"1\">\r\npublic class HelloWorldScheduledJob implements Job {\r\n  final static Logger logger = LoggerFactory.getLogger(HelloWorldScheduledJob.class);\r\n  public void execute(JobExecutionContext arg0) throws JobExecutionException {\r\n    logger.info(\"Incremening counter.\");\r\n    MyServletContextListener.counter = MyServletContextListener.counter+1;\r\n  }\r\n}\r\n<\/pre>\n<p>4. <strong>Create a servlet to view that counter<\/strong>:- <\/p>\n<pre lang=\"java\" line=\"1\">\r\npublic class SimpleServlet extends HttpServlet {\r\n  private static final long serialVersionUID = 1L;\r\n  public void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {\r\n    rsp.setContentType(\"text\/html\");\r\n    PrintWriter out = rsp.getWriter();\r\n    out.println(\"<html>\");\r\n    out.println(\"<head><title> Simple Servlet <\/title><\/head>\");\r\n    out.println(\"<body data-rsssl=1>\");\r\n    out.println(\"<p>Counter - \" + MyServletContextListener.counter + \"<\/p>\");\r\n    out.println(\"<\/body><\/html>\");\r\n    out.close();\r\n  }\r\n}\r\n<\/pre>\n<p>You can download the <a href=\"https:\/\/www.softwareeverydayblog.com\/wp-content\/uploads\/2013\/02\/sample-web-app.rar\">sample-web-app<\/a> from here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Objective is to make a counter using Quartz. Step by step we&#8217;ll do the following Create a custom context listener to initialize a counter, Create quartz properties file and xml jobs file, Schedule a job that would keep incrementing the counter, Create a servlet to view that counter. 1. Create a custom context listener to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-717","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=717"}],"version-history":[{"count":29,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/717\/revisions"}],"predecessor-version":[{"id":748,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/717\/revisions\/748"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}