Run a program or method at specific time in 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 d开发者_开发问答on'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
.
精彩评论