How to run a specific method on a specific date and time?

别说谁变了你拦得住时间么 提交于 2019-12-23 04:45:11

问题


I need to force my java application to perform a couple of actions on a specific date and time. The scenario is as following:

1- user set a specific time and frequency (every day, every month)
2- system starts a trigger for the request

3- once that pre-defined frequency and time are reached 
 3.1 - system performs the required actions that are kept in a method

I have found this answer but could not make it work.

An example would be appreciated.


回答1:


There are multiple java scheduler frameworks available to do the tasks at your specified time, interval, periodicity. Apache quartz is one of the commonly used ones.

or simply make use of java ScheduledExecutorService




回答2:


Quartz library is very useful for this.

How to do using Quartz: refer this http://www.mkyong.com/tutorials/quartz-scheduler-tutorial/




回答3:


You can use Executor from jdk itself, here is kick off example:

 ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
 ScheduledFuture<?> handle scheduler.scheduleAtFixedRate(new Runnable() {
            public void run() { System.out.println("your code is here :)"); }
        }, 1, 100, TimeUnit.MINUT);

so this code starts running after 1 min and runs after each 100 min.

in order to cancel later you will do handle.cancel(true)

Read from here



来源:https://stackoverflow.com/questions/17848859/how-to-run-a-specific-method-on-a-specific-date-and-time

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