Run a program or method at specific time in Java

后端 未结 4 1734
忘掉有多难
忘掉有多难 2020-12-21 20:46

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.

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-21 21:31

    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.

提交回复
热议问题