相信做軟件的朋友都有這樣的經(jīng)歷,我的軟件是不是少了點(diǎn)什么東西呢?比如定時(shí)任務(wù)啊,
就拿新聞發(fā)布系統(tǒng)來說,如果新聞的數(shù)據(jù)更新太快,勢必涉及一個(gè)問題,這些新聞不能由人工的去發(fā)布,應(yīng)該讓系統(tǒng)自己發(fā)布,這就需要用到定時(shí)定制任務(wù)了,以前定制任務(wù)無非就是設(shè)計(jì)一個(gè)Thread,并且設(shè)置運(yùn)行時(shí)間片,讓它到了那個(gè)時(shí)間執(zhí)行一次,就ok了,讓系統(tǒng)啟動的時(shí)候啟動它,想來也夠簡單的。不過有了 spring,我想這事情就更簡單了。
看看spring的配置文件,想來就只有這個(gè)配置文件了
上面三個(gè)配置文件中只有一個(gè)配置文件是涉及到您自己的class的,其他的都是spring的類。很簡單吧
我們只需要涉及一個(gè)class讓他繼承java.util.TimerTask;
下面讓我們來看看 spring的源代碼
The JDK Timer does not offer more sophisticated scheduling
Note that Timer uses a TimerTask instance that is shared
Note that the semantics of the period vary between fixed-rate
See Timer javadoc for details on those execution modes.
說實(shí)話這個(gè)類也沒什么,只是簡單的包裝了我們的timertask,里面也就只有幾個(gè)屬性,一個(gè)是時(shí)間片,一個(gè)是任務(wù)等。
真正運(yùn)行我們的任務(wù)的類是:
Allows for registration of ScheduledTimerTasks, automatically starting
Note that Timer uses a TimerTask instance that is shared between
Default is "false": The timer will automatically get cancelled on
afterPropertiesSet
.