Skip to main content

Thread scheduling

2 replies [Last post]
devuser007
Offline
Joined: 2009-01-17
Points: 0

Hi there,

I have a task here and needed some inputs from you guys.
This task needs to be done using jdk1.4 only in weblogic 9 server

I have written a small thread which runs as a start up class once i a start my weblogic server.

Example:

I am making this thread poll the database every 5 min to check is there are any invalid recrods and if they are found This thread deletes it and sends email to user.
This works fine.

Now I have to also add one more new task to this in the same class.
I have to also send emails every midnight about the records in the database.

Here is where i am facing problem.

suppose i start my weblogic server at 5.00 pm then my program needs to calculate the midnight time and trigger exactly at midnight and there after run every midnight. simultaneously the polling mechanism i wrote should run as i have implemented every 5 min.

How can i achieve this in the same class? Do I need to start a seprate thread?
I came across a article which says timer task and timer in jdk1.4 should not be used in a managed environment like weblogic container as it runs a thread which is out of the scope of weblogic.

How do i go about this?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
peter__lawrey
Offline
Joined: 2005-11-01
Points: 0

You can use a DelayQueue from the concurrency libraries to schedule these task in an existing thread.
There is a backport of these libraries for Java 1.4.

devuser007
Offline
Joined: 2009-01-17
Points: 0

I have used Quartz for this.I found this pretty handy.
For more info here is the url http://www.opensymphony.com/

Here is what i have come up with now.

I have 2 classes which do 2 different tasks And the scheduled them in one class.

This class is for task 1.

1.
2.
3. import java.util.Date;
4.
5. import org.apache.commons.logging.Log;
6. import org.apache.commons.logging.LogFactory;
7. import org.quartz.Job;
8. import org.quartz.JobExecutionContext;
9. import org.quartz.JobExecutionException;
10.
11.
12. public class SimpleJob implements Job {
13.
14. private static Log _log = LogFactory.getLog(SimpleJob.class);
15.
16. /**
17. * Quartz requires a public empty constructor so that the
18. * scheduler can instantiate the class whenever it needs.
19. */
20. public SimpleJob() {
21. }
22.
23. /**
24. *

25. * Called by the {@link org.quartz.Scheduler} when a
26. * {@link org.quartz.Trigger} fires that is associated with
27. * the Job.
28. *

29. *
30. * @throws JobExecutionException
31. * if there is an exception while executing the job.
32. */
33. public void execute(JobExecutionContext context)
34. throws JobExecutionException {
35.
36. // This job simply prints out its job name and the
37. // date and time that it is running
38. String jobName = context.getJobDetail().getFullName();
39. _log.info("SimpleJob says: " + jobName + " executing at " + new Date());
40.
41. System.out.println("Simple Job Class.!");
42. }
43.
44. }

This class is for task 2.

1.
2. import java.util.Date;
3.
4. import org.apache.commons.logging.Log;
5. import org.apache.commons.logging.LogFactory;
6. import org.quartz.Job;
7. import org.quartz.JobExecutionContext;
8. import org.quartz.JobExecutionException;
9.
10.
11. public class HelloJob implements Job {
12.
13. private static Log _log = LogFactory.getLog(SimpleJob.class);
14.
15. /**
16. * Quartz requires a public empty constructor so that the
17. * scheduler can instantiate the class whenever it needs.
18. */
19. public HelloJob() {
20. }
21.
22. /**
23. *

24. * Called by the {@link org.quartz.Scheduler} when a
25. * {@link org.quartz.Trigger} fires that is associated with
26. * the Job.
27. *

28. *
29. * @throws JobExecutionException
30. * if there is an exception while executing the job.
31. */
32. public void execute(JobExecutionContext context)
33. throws JobExecutionException {
34.
35. // This job simply prints out its job name and the
36. // date and time that it is running
37. String jobName = context.getJobDetail().getFullName();
38. _log.info("Hello Job says: " + jobName + " executing at " + new Date());
39.
40. System.out.println("Hello Job Class.!");
41. }
42.
43. }

This is my main scheduler.

1.
2.
3. import java.util.Date;
4.
5. import org.apache.commons.logging.Log;
6. import org.apache.commons.logging.LogFactory;
7. import org.quartz.CronTrigger;
8. import org.quartz.JobDetail;
9. import org.quartz.Scheduler;
10. import org.quartz.SchedulerFactory;
11. import org.quartz.SchedulerMetaData;
12. import org.quartz.impl.StdSchedulerFactory;
13.
14.
15. public class CronTriggerExample {
16.
17. public void run() throws Exception {
18. Log log = LogFactory.getLog(CronTriggerExample.class);
19.
20. log.info("------- Initializing -------------------");
21.
22. // First we must get a reference to a scheduler
23. SchedulerFactory sf = new StdSchedulerFactory();
24. Scheduler sched = sf.getScheduler();
25.
26.
27.
28. // job 1 will run every 20 seconds
29. JobDetail job = new JobDetail("job1", "group1", SimpleJob.class);
30. CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1","group1", "0/20 * * * * ?");
31. sched.addJob(job, true);
32. Date ft = sched.scheduleJob(trigger);
33. log.info(job.getFullName() + " has been scheduled to run at: " + ft
34. + " and repeat based on expression: "
35. + trigger.getCronExpression());
36.
37.
38.
39. // job 2 will run every day at 12.00am
40. JobDetail helloJob = new JobDetail("job2", "group1", HelloJob.class);
41. trigger = new CronTrigger("trigger2", "group1", "job2", "group1","0 0 12am * * ?");
42. sched.addJob(helloJob, true);
43.
44. ft = sched.scheduleJob(trigger);
45.
46. log.info(job.getFullName() + " has been scheduled to run at: " + ft
47. + " and repeat based on expression: "
48. + trigger.getCronExpression());
49.
50. log.info(helloJob.getFullName() + " has been scheduled to run at: " + ft
51. + " and repeat based on expression: "
52. + trigger.getCronExpression());
53.
54. log.info("------- Starting Scheduler ----------------");
55.
56. // All of the jobs have been added to the scheduler, but none of the
57. // jobs
58. // will run until the scheduler has been started
59. sched.start();
60.
61. log.info("------- Started Scheduler -----------------");
62.
63.
64. }
65.
66. public static void main(String[] args) throws Exception {
67.
68. CronTriggerExample example = new CronTriggerExample();
69. example.run();
70. }
71.
72. }