how to start Quartz Scheduler automatically in Java - java

I'm using Quartz Scheduler 2.2.1 in a webapp which is built on Tomcat webserver. I use a servlet to start Quartz . However, if the system reboots, I also have to restart Quartz manually by sending request to that servlet. Therefore, the problem is how to start Quartz automatically !
One more thing that is I want to use Quartz to perform a task at 00:00:00 everyday, so what is the best design for the trigger in this case ?

Define a ServletContextListener and implement a contextInitialized() method that starts quartz. This listener gets triggered if the tomcat is restarted or your servlet is redeployed

You should use a custom ServletContextListener and configure it in your web.xml for automatically starting your cron job.
http://docs.oracle.com/cd/B14099_19/web.1012/b14017/filters.htm#i1000654
If not satisfied by the Oracle link, do a little Google and you will get plenty of examples
To start a cron job every day at 12:00 am the below cron pattern should work:
0 0 0 * * ?

Related

What is the working of PersistJobDataAfterExecution annotation in Quartz and how it can be used in Java?

I have created one quartz job trigger for the spring boot application. Now in case of application restart, I want the quartz trigger to continue its execution from the same point when the application was restarted. I was looking for some solution over the internet and found PersistJobDataAfterExecution annotation in quartz. I want to know how this annotation works and how it can be used in Java?

How to check whether #Schedule annotation (Spring 3.1) cron job is running.

In my application I have one cron job which connects to a FTP server and transfer files, a very simple functionality and it is configured using spring #Schedule annotation with cron expression as a parameter.
It was running fine for few months and then suddenly it stopped, got the connectException.
May be the FTP server was down or something happened which causes the cron thread to stop.
I looked (google) for the reasons but didnt get any ( Nothing much in the logs also - Just the exception name ).It may be a one time thing :)
my question is that can I put some check or watcher on the #Schedule cron job to know whether it is running or not ?
Sorry for my bad explanation/english
Thanks
my question is that can I put some check or watcher on the #Schedule
cron job to know whether it is running or not ?
Basically, you can't.
When you use #Scheduled, Spring uses a ScheduledAnnotationBeanPostProcessor to register the tasks you specify (annotated methods). It registers them with a ScheduledTaskRegistrar. The ScheduledAnnotationBeanPostProcessor is an ApplicationListener<ContextRefreshEvent>. When it receives the ContextRefreshEvent from the ApplicationContext, it schedules the tasks registered in the ScheduledTaskRegistrar.
During this step, these tasks are scheduled with a TaskScheduler which typically wraps a ScheduledExecutorService. If an exception is uncaught in a submitted task, then the task is removed from the ScheduledExecutorService queue.
The TaskScheduler class does not provide a public API to retrieve the scheduled tasks, ie. the ScheduledFuture objects. So you can't use it to find out if your tasks are running or not.
And you probably shouldn't. Develop your tasks, your #Scheduled methods, to be able to withstand an exception being thrown. Some times, obviously, that's not possible. With a network error, for example, you would probably have to restart your application. Without knowing anything else about your application, I would say more logging is your best bet.

How does the JDBCJobStore work .?

So I started to tinker around with JDBCJobStore in Quartz. Firstly, I could not find a single good resource on how to configure it from scratch. After looking for it for a while and singling out a good resource for beginners, I downloaded the sample application at Job scheduling with Quartz. I have a few doubts regarding it.
How does JDBCJobStore capture jobs.? I mean in order for the job to get stored in the database does the job have to run manually once.? Or will JDBCJobStore automatically detect the jobs and their details..?
How does JDBCJobStore schedule the jobs.? Does it hit the database at a fixed interval like a heartbeat to check if there are any scheduled jobs.? Or does it keep the triggers in the memory while the application is running.?
In order to run the jobs will I have to manually specify the details of the job like like name and group and fetch the trigger accordingly.? Is there any alternative to this.?
On each application restart how can I tell the scheduler to start automatically..? Can it be specified somehow.?
If you are using servlet/app server you can start it during startup:
http://quartz-scheduler.org/documentation/quartz-2.2.x/cookbook/ServletInitScheduler
If you are running standalone you have to initialize it manually i think.
You can read more about JobStores here:
http://quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-09
And about jobs and triggers:
http://quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-02
http://quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-03
http://quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-04
I guess that quartz checks jobs based on time interval to proper work in clusters and distributed systems.

EJB 3.0 Timer Cluster Info

I was able to get some hands on EJB3.0 Timer Service.I was able to get the timeout working and I was able to invoke the timer using servlet Context listener.I have deployed a simple app which sends alerts at a specific interval.I am using WL 10.3.1(does not support EJB3.1,to use Scheduler).
I get alerts twice at the same time.(I have a cluster with 2 managed Servers).I looked at few examples of using a timer in WL cluster,for eg: http://shaoxiongyang.blogspot.com/2010/10/how-to-use-ejb-3-timer-in-weblogic-10.html .But I would like to avoid any configuration on the server.Is there any other way this can be controlled in a Cluster Env.I want to have one timer running at any time in a cluster Env.
Thanks...
Do the servlet context listeners unconditionally create the timer during contextInitialized? If so, that explains the problem since the servlet context listener will run in each JVM. You'll need to somehow check if the timer has already been created first. Either use getTimers or check/insert a row into your own database table.

Scheduling a Job in webserver -Java

I want to schedule a job in Jboss and websphere server.
I have a piece of code in java which should execute at a certain frequency independantly.And it should execute in a server continiously.
Is it possible to execute a code when server starts.
I'd recommend to use Quartz Scheduler if you require full portability of the code (WebSphere, JBoss, Tomcat, etc.)
You can initialize Quartz very easily by using build-in QuartzInitializerServlet or even better QuartzInitializerListener.
you could use Timer api.
add #Startup on your bean.
or add this to your web.xml (change properties for your convenience)
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>example.web.Servlet2Stateless</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
Use Quartz Scheduler as mentioned by #Tomasz Blachowicz. Use Databse approach to configure Jobs and triggers. And just add below three line in your Startup servlet. That's It!
StdSchedulerFactory factory = new StdSchedulerFactory(configFile);
// where configFile => quartz.properties file complete path.
Scheduler scheduler = factory.getScheduler();
scheduler.start();
Hope this will work for you. I am using this and its very easy to configure.
You could use Flux to schedule your java jobs inside a web container.

Categories