Schedule a task - java

Iam using jboss5.1.x, EJB3.0
I need to schedule a task in my application.
which way would you recommend me to do it according to version I am using below?
I heard about SAR, but i am not sure if there is a way which is more appropriate or updated to do it nowdays
thanks,
ray.

I would recommend Quartz
Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.
Quick Start Guide

I also recommend using Quartz. In case you cannot use it, don't forget the EJB 3.0 standard has the concept of EJB Timer services. You can read more here.

Java EE API javax.ejb.TimedObject supports the feature.

In Java you have TimerTask , you can schedule a task to run at the particular time.But i would recommend Quartz.According to your scenario , you have built in EJB Timer class available , hope will solve the purpose.
Please read this article
http://mhashem.wordpress.com/2010/03/29/java-quartz-scheduler-ejb-3-0-timer-service-and-java-timer-task-when-to-use-each/

Related

How to create threads in Java EE environment?

I have a requirement where I have to persist some data in a table and the persisting may take sometime. Basically I want to persist a log. I don't want the execution to wait till the persisting finishes.
I know I have to use threads to accomplish this task and I know that it is discouraged to create threads in an enterprise application.
So I started reading about worker manager and understood and tried a sample program in websphere application server 8.5.
I used asynchbeans.jar from websphere and now I am bothered that I am writing vendor specific code.
Then I came across commonj work api which is described in oracle java documentation. Now I am thinking to use commonj api from fabric3.
My doubt is, is there a better way to accomplish the same task? An EJB way? Or work manager is good for my requirement?
You have some options:
Asynchronous beans. These are vendor-specific, as you mention.
commonj is just barely not vendor-specific. As far as I know, it was only implemented by IBM WebSphere Application Server and BEA WebLogic. The API was effectively superseded by Concurrency Utilities for Java EE, which is really the best choice.
EJB #Asynchronous methods. Requires using EJBs (unwanted complexity for some).
EJB timers. Requires using EJBs, requires serializable data.
JMS. Probably requires using MDBs to receive the message, requires serializable data.
Actually create threads. The EE specs do not recommend this, but as long as you don't attempt to use EE constructs (lookup("java:..."), JPA, UserTransaction, etc.), then you should be fine.
JavaEE7 has the managed executor, that you can try. You can spawn a task with it, and recieve managed callbacks in a handler. This is part of EE standard and should be platform agnostic.
See JDoc here:
http://docs.oracle.com/javaee/7/api/javax/enterprise/concurrent/ManagedExecutorService.html
If you need to be sure that all your log entries are safely written, then you probably should use JMS with persistent messages. Otherwise you could use #Asynchronous EJB methods.

Job scheduling - how to choose the best one for springMVC based application

I am planning to go for Job scheduling for my spring MVC application and while I was searching for the same I came across this. but really don't have idea whether there are many like Quartz or which is the best scheduling API for Spring based application.
I think it really depends upon your requirements. For example:
Do jobs need to survive a restart of your infrastructure?
How critical is the availability of the scheduling framework?
How complex is the type of job you're trying to execute?
Quartz is a dedicated Job scheduling framework and as you would expect comes with many 'enterprisey' features that allow you to build a very highly available, highly performant Job scheduling implementation. It is fairly easy to get started with as well.
Other alternatives could be something like Amazon SQS with again provides a very highly available job queue that operates as a service. However the clue is in the name in terms of 'simple'. You loose a lot of the features that something like Quartz would offer. Amazon do however provide a Java wrapper onto the SQS API so managing it as part of your build should be simple enough.
Alternatively the JDK comes with its own built in options. Take a look at the various implementations of the java.util.concurrent.ExecutorService interface. Again depending upon your requirements there may be something in there that fits the bill without having to depend upon external libraries or APIs.
There is also this list of open-source job scheduling frameworks that should help you to compare other offerings with Quartz.

Are there any other Java schedulers besides Quartz(FOSS) and Flux(Commercial)

I am interested in finding out about other job scheduling packages besides Quartz and Flux. Given the plethora of web frameworks i find it peculiar that there is really only one scheduler. Are there others that perhaps are not as well-known or popular?
SpringBatch: Not really a scheduling solution but rather a batch job coordinator etc.
How does Spring Batch differ from Quartz? Is there a place for them both in a solution?
Spring Batch and Quartz have different goals. Spring Batch provides functionality for
processing large volumes of data and Quartz provides functionality for scheduling tasks.
So Quartz could complement Spring Batch, but are not excluding technologies. A common
combination would be to use Quartz as a trigger for a Spring Batch job using a Cron
expression and the Spring Core convenience SchedulerFactoryBean.
There are others, just not as well known necessarily:
http://java-source.net/open-source/job-schedulers
Also, as I mentioned above, TimerTask can be handy for simple tasks.
But I have to admit Quartz did a great job - it's one of the reasons they were ultimately "bought". jquery is a similar type of well-known solution when you might think there'd be more than there actually are.
Quartz is nice but just an API. Flux is pretty feature rich but more about workflow than hard-code scheduling. Another job scheduling alternative is JobServer with its open source SDK, soafaces. soafaces is a way to build modular server-side java Tasklets. It has lots of scheduling rules and job reporting/monitoring UI stuff also.

Is it possible to run a cron job in a web application?

In a java web application (servlets/spring mvc), using tomcat, is it possible to run a cron job type service?
e.g. every 15 minutes, purge the log database.
Can you do this in a way that is container independent, or it has to be run using tomcat or some other container?
Please specify if the method is guaranteed to run at a specific time or one that runs every 15 minutes, but may be reset etc. if the application recycles (that's how it is in .net if you use timers)
As documented in Chapter 23. Scheduling and Thread Pooling, Spring has scheduling support through integration classes for the Timer and the Quartz Scheduler (http://www.quartz-scheduler.org/). For simple needs, I'd recommend to go with the JDK Timer.
Note that Java schedulers are usually used to trigger Java business oriented jobs. For sysadmin tasks (like the example you gave), you should really prefer cron and traditional admin tools (bash, etc).
If you're using Spring, you can use the built-in Quartz or Timer hooks. See http://static.springsource.org/spring/docs/2.5.x/reference/scheduling.html
It will be container-specific. You can do it in Java with Quartz or just using Java's scheduling concurrent utils (ScheduledExecutorService) or as an OS-level cron job.
Every 15 minutes seems extreme. Generally I'd also advise you only to truncate/delete log files that are no longer being written to (and they're generally rolled over overnight).
Jobs are batch oriented. Either by manual trigger or cron-style (as you seem to want).
Still I don't get your relation between webapp and cron-style job? The only webapp use-case I could think of is, that you want to have a HTTP endpoint to trigger a job (but this opposes your statement about being 'cron-style').
Generally use a dedicated framework, which solves the problem-area 'batch-jobs'. I can recommend quartz.

How to run a task daily from Java?

How can I run a task daily at a specified time (say 11:00 am) using java.util.Timer? I'm using JDK 1.4.2, I know it's old, but it's what the project requires.
Quartz is the most well known solution to schedule processes in Java environments, but you have a lot of options. Check this list:
Quartz is an open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application. Quartz can be used to create simple or complex schedules.
Jcrontab is designed to be extended and integrated with any project. Reads and stores the tasks to execute in a file, a database or an EJB and provides a web UI and a basic swing GUI.
Essiembre J2EE Scheduler is a simple task scheduling mechanism for J2EE applications. This library can be considered a wrapper around the Timer and TimerTask classes found in the standard Java API. Configuration for tasks to be executed and their frequency is XML-based.
cron4j is a scheduler for the Java 2 platform which is very similar to the UNIX cron daemon.
Oddjob's goal is to provide some order and visibility to all the batch files and cron jobs that tie an enterprise's critical business processes together.
Fulcrum Scheduler provides a scheduler service. It is based on the TurbineScheduler provided with Turbine, but all older stuff has been removed. Currently ONLY the non persistent Scheduler is done. It loads scheduled jobs from the component config xml file.
Gos4j -Goal Oriented Scheduling for Java- is a way of organising processing priorities based on goals.
Job Scheduler is a batch program operating as a demon, and can be controlled using a graphical user interface. The Job Scheduler uses an XML configuration for the scheduled programs, scripts and for the timing and frequency of task processing. An API is available that hands control of events and logging to your jobs.
JDRing is a lightweight Java scheduling library that is simple and small, but still supports ringing alarms at specified intervals, as one-time events, or on complex schedules with full cron-like control.
jBatchEngine is a batch job spooler written in Java. In constrast to time driven schedulers like Cron, jBatchEngine is event driven.
MyBatchFramework is an open-source lightweight framework designed to create easily robust and manageable batch programs into the Java language.
Super with SuperScheduler and SuperWatchdog is a Java job scheduler with rich GUI for all applications. It is platform neutral. Especially good to be a job scheduler for Linux and Solaris. It provides a super set of functionalities of the Scheduler of Microsoft Windows. It provides event-triggered scheduling. It can schedule tasks in a distributed environment. A task will be executed once and only once among all machines in the network. All tasks are holiday adjustable. Even every job is a STANDBY job, the history will be a good trace for important tasks. It supports Internationalization.
source: Open Source Job Schedulers in Java
Look into TimerTask and Timer - both are in that version of the JDK.
Timer :
public void schedule(TimerTask task, Date firstTime, long period)
public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
Set it to run the first Date you want then the number of milliseconds in one day as your delay.
Is it possible for you to use a library such as cron4j? It will make your job much easier!
Java Timers can run an arbitrary job at intervals, pre-fixed times, etc.etc.
Quartz library
If you really want to be bare-bones about it, wrap it in a shell script and put in in cron.
You have to use Quartz
I have never know who launches Quartz in first place though.
If you have an application server or similar artifact you can configure the Quartz job there and have it run your task at the given time.
Maybe, that recent post helps you:
Will this pause my Java thread for a minute?
My response for that question is use a java built in implementation based on java.util.Time and java.util.TimerTask classes:
Will this pause my Java thread for a minute?
Or, you can use the crontab service for *nix platforms (available for Windows platforms too). That's the simplest and light-weight style to run a standalone job periodically.
[]'s,
And Past

Categories