Run a program or method at specific time in Java - java

I just want that my program or method should run at specific date and time.
i have heard about Timer and TimerTask in java API. But don't know exactly how to use it.

If you want to run a java program at a specific time you probably want to look at the OS tools (like cron or at).
If you want to run a method inside of an already running java application then the ScheduleExecutorService, while it may be overkill, is pretty easy to use.

If this is for your own benefit and not for a project I would suggest you look into
http://java.sun.com/javase/6/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html which is a replacement for java.util.Timer. If however you want a robust scheduler, I concur with the previous posters with regards to Quartz.

Maybe you should use a third-party library with a higher level API like Quartz and use the SimpleTrigger.

You can run your task inside a Glassfish Java EE server. It supports a Timer Service that fires background tasks at specified intervals. When you're running a cluster of Glassfish servers on different machines, they'll collaborate to fire the task exactly once.
A simpler approach is to rely on cron for Unix systems. At specified times you can run your java task via the java command.
I've also used pycron on Windows, which is a service that emulates cron.

Related

ManagedExecutorService, ManagedTask and ManagedTaskListener alternative for Java SE

I recently built a Java EE 7 web application using GlassFish / Payara.
The web application started long running tasks (> 2 hours) on the underlying operating system on demand. These tasks are basically other programs or scripts written in Python, Ruby or Java and executed using the command line (Apache Commons Exec). Since I wanted to inform the user about the current state of a task and give him the possibility to cancel a running task I used the following Java EE features and libraries.
The following list briefly describes why I decided to use a specific Java EE feature or an external library.
ManagedExecutorService
Injected using #Resource, allowed me to queue Runnables and execute them in an own thread
ManagedTaskListener Implementation
This was the reason why I actually decided to use a ManagedExecutorService and run threads within the application server
The interface gave me four very handy methods (taskSubmitted, taskAborted, taskDone and taskStarting)
This allowed me to update the long running task status (JPA) and inform the user if a state change occurred
Runnable, ManagedTask Implementation
Here I defined the actual long running task by overriding the run() method
To react on state changes I also registered the ManagedTaskListener implementation as previously described
Apache Commons Exec Util
To actually execute the long running tasks the Runnable and ManagedTask implementation simply used apache commons exec
To be able to cancel a long running process I set a ShutdownHookProcessDestroyer per DefaultExecutor
Using defaultExecutor.getWatchdog().destroyProcess(); this allowes me to kill a running task
I know it is not the best idea to misuse a ManagedExecutorService in combination with ManagedTasks to execute such long running tasks, but it worked pretty stable.
But know I would like to run these long running tasks on a different physical machine and execute them using just Java SE. This means I have no ManagedExecutorService and therefore no ManagedTaskListener interface.
Does anyone have experience with this situation? Maybe there is library which provides similar capabilities. I think Google Guava provides something which goes into a similar direction but does not provide the same capabilities.
I am also thankful for other solution approaches.
Thank you very much!
Use the Java SE executors (i.e. ScheduledExecutorService) to submit a Runnable/Callable that makes the necessary updates.
You will have to implement your own equivalent of ManagedTask/ManagedTaskListener, but that should be easily achievable through your Runnable/Callable implementations.
Side note:
When you are using a ManagedTask, this is a perfect scenario to set ManagedTask.LONGRUNNING_HINT=true on your ManagedTask. For example:
managedTask.getExecutionProperties().put(ManagedTask.LONGRUNNING_HINT, "true");

Can I use Quartz Scheduler to launch my application?

I have a Java desktop application that I'd like to completely exit, and schedule it to launch at a later date.
Is there any way of using Quartz Scheduler for this?
As far as I understand, Quartz requires a Java runtime to be running for it to activate.
I'd like the following functionality, and would not mind using another Java library to achieve this:
Get/Set schedules for execution of a Java application.
Be able to launch the application without having a Java runtime.
Maintain a cross-platform codebase.
Yes you can, you need to wedge it outside the code.

What server platform to choose for periodically running a standalone (Java) program?

I need to develop a game server that will run periodically (e.g., triggered by a CRON job every five minutes or hour as appropriate). Once started up, the server will access all of the current game state (fetched through REST from the game's data servers (Stackmob, Parse or similar), do the processing of player actions, and then POST the results back to the data server. In other words, it will be doing a lot of HTTP requests, but does not itself necessarily need to be a web service.
I've been considering multiple ways of developing this.
I do not feel for setting up a server myself, so I need to find a service to run this on that permits the workflow I would like.
The game engine is Java, so something that works neatly with that.
Will need to GET and POST data files, so access to static files would be needed.
Most of the services that exist which provide something similar to what I require are directed at web services - which generally means that one needs to jump through some hoops to get things to work.
Google App Engine, for instance, would require that I implement this using backends (since the game server could potentially run for more than 60 seconds), and isn't particularly happy with the idea of static files.
Amazon EC2 would seem easier to develop on (again by building a web service frontend, of course), but there seems to be relatively poor support for CRON.
Generally speaking, it feels like I want to shoot some sparrows with a slingshot, but all the services are offering me cannons. Are there any alternative platforms/frameworks beyond the big two mentioned above that would be suitable for something like this?
You could try Heroku. They support Java. If you created a project that used a single worker dyno then the hosting would be free (see link).
The process would be running continuously, so you might want use a Timer for periodic execution. You could also use Quartz, but it might be overkill.
Edit:
Here's some links that might help get started:
Running non-web Java processes on Heroku
Heroku Java quickstart - this is for a web app ('web dyno') rather than a 'worker dyno', but it may help.
java.herokuapp.com has links to some example projects (again web apps rather than workers)
How about using EC2, but rather than putting the scheduler in the instance (which won't work because the instance can go away at any time), putting it in AWS? Like this guy:
http://alestic.com/2011/11/ec2-schedule-instance
Alternatively, if you manage your EC2 instances through Ylastic, it looks even easier:
http://blog.ylastic.com/scheduling-tasks-on-the-aws-cloud
Although you'll have to pay for Ylastic as well as EC2, i imagine.
I found a nifty way of writing something like this in Groovy with Maven. You can write a multithreaded Groovy script to pull the stats, do the updates, etc. and then have maven's assembly plugin assemble the whole thing into a self-contained, executable jar file that can be called by a CRON job. One nice thing about Groovy is that its syntax allows you to do this:
def google = "http://google.com".toURL().text
which will turn the string into a URL and handle all of the details of turning the URL into a HTTPURLConnection and getting the raw text.
You could develop the app as a standalone Java program first and then worry about where to deploy it later.
To develop the app you could write a simple Java program that uses HtmlUnit to talk to the external web services. The job could be internally started via Quartz. If you really wanted to start the job externally via CRON, you could have CRON run the app, passing in args. The app would then run and exit.
Alternatively, you could have the app always running and have cron run a bash script that triggers the job in some way.
Essentially, all you need to deploy is a Unix machine so you could use AWS.

How to create batch file for java-quartz cron job

Is it possible to write a cron job using java quartz and triggering through windows batch file because after so much searching there is no standalone particularly for java-quartz
"So much searching"? This was the first link returned by Google.
If I understand your question right, you want to execute java process as a windows service. If that is correct, wrap your java service (could be quartz as well) using tanuki wrapper and can install it as windows service. Or as #duffymo suggesting you can also use cron trigger too.

Job Scheduling in Java

I want to run a Java program on a specific date.
I am developing a J2EE application that allows you to schedule Selenium test launch (JUnit) on a specified date..
Are there any solutions to do this? can you point me to technology that can help me to do this?
any help is appreciated:)
thanks for your help
You provided very little information. You can schedule launch in scheduler of your operating system (like cron in Linux), or you can run a task from within your Java process, if the process is constantly running. For this see Quartz Scheduler.
Not knowing enough details, I would recommend using Quartz. You can see an example of using it here.
You could use crond or Windows Task Manager.
If you have a Java process running from now to the time it needs to start, look at Quartz.
If you need to have a Java process started from nothing, you must ask your operating system to invoke it for you. For Linux check the "at" command.
Cron on Unix, and Cron for NT on WindowsNT platforms (XP-Windows 7, Windows Server 4.0+).
Why reinvent the wheel?
If you want to create and package modular java server-side tasks (that you can then schedule in any particular java scheduler of your choice) check out the open source project called soafaces. Let's you create modular java Tasklets and also give them web based GUI customizer (customizer part is optional and based on google gwt).
Scheduling can be implemented in many ways, it is also bit IO intensive, so if needed u might want to use non-java solutions
However you want to have java solutions may be below links should help you
Spring Way : https://spring.io/guides/gs/scheduling-tasks/ and https://dzone.com/articles/schedulers-in-java-and-spring
Non Spring solution: https://github.com/knowm/Sundial

Categories

Resources