0

currently, I have a web application based on Java 7, tomcat 7 and Spring 4 that invokes a thread on tomcat startup. This thread is always alive and the java code is:

public class Scheduler {
   Queue<Long> queue = new ArrayBlockingQueue<Long>();
   private static class ThreadExecutor implements Runnable
   {
      .......
      @Override
      public void run() 
      {
          while(true)
          {
              Long ID = queue.get();
              if(ID != null)
              {
                   Object o = webFacade.get(ID);
                   //Exec....
              }
              else
              {
                   try 
                    {
                        Thread.sleep(30000);
                    } 
                    catch (InterruptedException e) 
                    {
                        e.printStackTrace();
                    }
              }
          }
      }
   }
}

An external event fills the queue with the Object's ID. With one tomcat this thread works well, but now I nedd to add onother tomcat, so I want to introduce Quartz in clustered mode. I've configured Quartz in my project and it seems to work, but now how can I "translate" this class using Quartz?I want that only one thread is active because it is very expensive for my Database.

Thanks in advance

user3363936
  • 15
  • 1
  • 6

1 Answers1

0

In general Quartz while being run in a cluster mode guarantees that the job will be triggered (and handled) on one server only.

So Job will be the task that you execute (in other words, what should be executed).

Now Quartz also introduces the concept of Trigger which basically defines when the job will be triggered.

From your code snippet, you run the job every 30000 ms = 30 seconds. So you'll trigger your stuff every 30 seconds (SimpleTrigger will do the job).

So, the 'while' loop goes away, it will be handled by quartz automatically. In job you'll only work with a queue. Its unclear who fills this queue, but it looks like a different question.

It's hard to say exactly how you translate the queue, but in general job should

  1. Get from queue
  2. Call webFacade just like now

That's it. Last but not the least, Spring has a beautiful integration with Quartz. See Chapter 33.6.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • I like your solution. I have onother question: if I connect to production database with my local machine I don't want that my pc starts to execute jobs...is there a way to "disable" triggering?I tried to set `org.quartz.threadPool.threadCount` with a lower value, but it seems not working. Thanks – user3363936 May 18 '16 at 14:47
  • I dont know whether its possible in quartz. I've never tried that. Maybe the best solution will be to not deploy the quartz integration in spring. For that spring has profiles and new @Conditional annotation if you're running spring 4. – Mark Bramnik May 18 '16 at 15:08
  • I implemented the solution you suggested me. Thanks it works. But now I have one more question: I have 3 jobs: all should be triggered independently, but I want that same job wait to be ended before beeing triggered again...I tried to play with org.quartz.threadPool.threadCount but I'm confused about this...can you help me please? – user3363936 May 19 '16 at 14:02
  • threadCount should mean that only one thread can be used for running the quartz. I think you better should use "concurrent=false" parameter. Its stated here: http://stackoverflow.com/questions/1636556/ensure-that-spring-quartz-job-execution-doesnt-overlap – Mark Bramnik May 20 '16 at 15:31