How to "make something" in a specific time and day of the week in java?
I have an application that needs to play some specifics audios(mp3) periodically. Let me give one example: Every Monday at 8:00am -> hello.mp3. How can I do that in J开发者_运维技巧ava?? I´m trying using Calendar, JodaTime, but I cant do it.
Depends, if you are using Java EE, you can use EJB Timer. And there is always Quartz http://www.quartz-scheduler.org/
What you want is a scheduled task. Timer class can provide this for you. Here is an example to get you started example
What a Timer
can help you to achieve is so minimum, Quartz Scheduler is a well-known (and easy-to-use) scheduler that accepts cron-like expression.
If you use Spring in your application, it has a scheduling module that can keep your Quartz code even cleaner.
You should use java.util.Timer
to define schedules and java.util.TimerTask
to define job. You can also use well-known Quartz Scheduler.
If you use Spring, you can use its scheduling service (it can be configured to use either Quartz Scheduler or java.util.Timer
in its work).
If you run desktop App, the easiest way is to use java.util.Timer
. For more complicated schedules use Quartz Scheduler.
精彩评论