문제 설명
데이터베이스 필드를 매일 확인하려면 어떻게 해야 합니까? 크론 작업? (How can I check database field every day?Cron job?)
I' m developing application in Java which is connected to database (Oracle or SQLite). In database I store contracts. The contract has field access_date
. And i want to check this field once every day and if one day access_date
will be equal to the current date then mark the contract as not valid. How can I implement this? In PHP as far as I know there are cron jobs for such tasks but what about java or java ee? How can I do this?Thanks in advance!
참조 솔루션
방법 1:
You can do this in two ways:
- You can use Cron to invoke a Java program at specific time (in your case, one every day).
- If your Java program is designed to run as service/daemon (staying in the background all the time), you can use a job scheduler such as Quartz Scheduler to invoke a certain check function at specific time.
- As an extension of 2., you can use ScheduledExecutorService.
방법 2:
Use either TimerTask or ScheduledTask. Here is a good tutorial for that:
http://www.ibm.com/developerworks/java/library/j‑schedule/index.html
방법 3:
Instead of saving an attribute for the contract not being valid, and having to check it against sysdate every day, I would use a predicate such as "access_date < trunc(sysdate)" to find expired contracts, and an expression such as:
case when access_date < trunc(sysdate) then 'N' else 'Y' end
... to display whether a contract is active or not.
(by MainstreamDeveloper00、goblinjuice、Juned Ahsan、David Aldridge)