Run a program or method at specific time in Java

天涯浪子 提交于 2019-11-29 16:26:38

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!