{"id":1750,"date":"2016-09-23T17:32:55","date_gmt":"2016-09-23T17:32:55","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1750"},"modified":"2016-09-23T17:32:55","modified_gmt":"2016-09-23T17:32:55","slug":"job-tracking","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/job-tracking\/","title":{"rendered":"Job Tracking"},"content":{"rendered":"<p>I have a scenario where a scheduled job being run by Quartz will update an arraylist of objects every hour.<\/p>\n<p>But I need this arraylist of objects to be visible to all sessions created by Tomcat. So what I&#8217;m thinking is that I write this object somewhere every hour from the Quartz job that runs so that each session can access it.<\/p>\n<p>Store the list in the ServletContext as an application-scoped attribute. Pulling the data from a database instead is probably less efficient, since you&#8217;re only updating the list every hour. Creating a ServletContextListener might be necessary in order to give the Quartz task a reference to the ServletContext object. The ServletContext can only be retrieved from JavaEE-related classes like Servlets and Listeners.In the ServletContextListener, when you create the job, you can pass the list into the job by adding it to a JobDataMap.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class MyServletContextListener implements ServletContextListener{\r\n  public void contextInitialized(ServletContextEvent event){\r\n    ArrayList list = new ArrayList();\r\n\r\n    \/\/add to ServletContext\r\n    event.getServletContext().setAttribute(&quot;list&quot;, list);\r\n\r\n    JobDataMap map = new JobDataMap();\r\n    map.put(&quot;list&quot;, list);\r\n    JobDetail job = new JobDetail(..., MyJob.class);\r\n    job.setJobDataMap(map);\r\n    \/\/execute job\r\n  }\r\n\r\n  public void contextDestroyed(ServletContextEvent event){}\r\n}\r\n\r\n\/\/Quartz job\r\npublic class MyJob implements Job{\r\n  public void execute(JobExecutionContext context){\r\n    ArrayList list = (ArrayList)context.getMergedJobDataMap().get(&quot;list&quot;);\r\n    \/\/...\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have a scenario where a scheduled job being run by Quartz will update an arraylist of objects every hour. But I need this arraylist of objects to be visible to all sessions created by Tomcat. So what I&#8217;m thinking is that I write this object somewhere every hour from the Quartz job that runs&hellip; <a href=\"https:\/\/codethataint.com\/blog\/job-tracking\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203],"tags":[],"class_list":["post-1750","post","type-post","status-publish","format-standard","hentry","category-case-studies"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1750"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1750\/revisions"}],"predecessor-version":[{"id":1751,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1750\/revisions\/1751"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}