I have a webapp running on weblogic that runs a Scheduler on a ServletContextListener.
The problem is the scheduler runs indefinitely, so even if i stop the webapp or redeploy the scheduler keeps running.
I should be able to stop the scheduler on contextDestroyed, but I don't have the instance. I've seen a couple of websites recommending this aproach to the problem, but they all have shedulers running a defined number of times.
Quartz comes with a servlet specifically for starting & stopping the scheduler on application startup and shutdown simply add the following to your web.xml:
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
If you want to shutdown without waiting for the executing jobs to finish use:
scheduler.shutdown(false);
Check this page for more info.
Upon application shutdown you must call
scheduler.shutdown();
Sometimes you have to do a Thread.sleep(1000); to let it shut down properly aswell.
Do this in a ContextLoad listener or other shutdown hook that you have.
To get the instance depends on how you have set up quartz, but the default scheduler can be obtained like this:
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
See http://www.quartz-scheduler.org/docs/1.x/quick_start_guide.html for more information
Related
I have used quartz scheduler in my spring boot application . I am using java programmatic scheduling not a spring based annotated one. Here's the snippet
Scheduler scheduler =
StdSchedulerFactory.getDefaultScheduler();
JobDetail jobDetail = newJob(ExPJob.class) .build();
CronScheduleBuilder cronSchedule =
cronSchedule (mycronExpression)
.withMisfireHandlingInstructionDoNothing();
I am shutting down this as bellow
#PreDestroy
Public void shutdownSch(){
Scheduler scheduler =
StdSchedulerFactory.getDefaultScheduler();
scheduler.shutdown();
}
Whats my problem ?
-- if I do a re/deployment for my app.war, In Weblogic thread pool does not get killed, and after scheduler starts,
It creates another thread pool it seems.
For Example : if I schedule a cron Job at 4 PM lot of jobs are firing at the same time. I have tried to find the PID in Linux box I'm not getting any regd this zombie threads.
What are the solutions tried ? --
I am able to kill jobs successfully via jobkeys
scheduler.deletejob(jobKey) .
So why issue still persists ?
-- I have done a series of deployments without the above mentioned job killing code before.
So, I suspect those jobs/threads are already running inside JVM. I tried a weblogic restart via console through MWare folks. But still no luck. Kindly pour your thoughts.
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 * * ?
Is there any way to know when a war is being hot deployed so I can shutdown the old executor that is running scheduled tasks? I'm using jboss 6 and I have a scheduled thread pool that is updating data periodically in the background. When I hot deploy that thread pool is not shutdown and new starts up so I have multiple scheduled thread pools.
Thanks for any input.
Yes, but it depends on where you start your Executor and which technology you use:
in ServletContextListener: start Executor in contextInitialized() and shut it down in contextDestroyed().
in servlet, start Executor in init(), shut it down with destroy()
in EJB/Spring bean: start in method annotated with #PostConstruct, shutdown in #PreDestroy.
Sure. It depends on how do you run this thread. If for example you are running it from sevlet use servlet's destroy() method to stop it. If you are using Spring use lifecicle of its Application context.
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.
I have a web application that makes use of a Spring TaskExecutor. The task executor is started when the web application is loaded for the first time and the thread runs the entire life of the web application. Ever since I added this thread to the application, Oracle Application Server will no longer shutdown gracefully. My guess is that OAS is not shutting down because I am not gracefully shutting down the task executor thread anywhere in the code. Where would the best place be to shutdown the task executor in a web application? I am using Java 5 with Spring 2.5.6.
Here is the task executor that I am using:
<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>
SimpleAsyncTaskExecutor has no meaningful state, it does not require shutting down gracefully. Also, I'm not sure what you mean by "the thread runs the entire life of the web application" - which thread are you referring to?
However, the threads that it spawns may be preventing your server from shutting down. SimpleAsyncTaskExecutor uses a ThreadFactory to create its threads, and by default threads that are created are not daemon threads, and so any spawned threads which are still executing when the shutdown is initiated will continue, and the server will only terminate when all of those spawned threads are finished.
As a solution, I suggest using a ThreadPoolTaskExecutor in preference to the SimpleAsyncTaskExecutor. This has a boolean property which controls what happens when a shutdown occurs, and it can force the termination of the thread pool if required.
If you still want to use SimpleAsyncTaskExecutor, then you could inject it with a custom ThreadFactory which creates deamon threads. This will allow the server to shutdown regardless of the state of those spawned threads. This might be dangerous, though, if those threads could leave resources in an unstable state.
Spring provides a bean-friendly implementation of ThreadFactory (CustomizableThreadFactory) which you can inject into the SimpleAsyncTaskExecutor, setting the daemon property to false.
Typically web aplications can react to container shutdown via a ServletContextListener. Implement one and use it to shut down your Executor. Ideally you should couple that with a proper shutdown of the whole Spring lifecycle. I vaguely remember that Spring had something ready-made for that purpose, so it's probably a good idea to check there as well.
You can use the #PreDestroy annotation:
#PreDestroy
protected void shutDown() {
taskExecutor.shutdown();
}