Results 1 to 2 of 2

Thread: j-scheduler

  1. #1
    fabs is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default j-scheduler

    Hi all,

    I bumped into this neat scheduler program below is the code

    Java Code:
    package org.tiling.scheduling.examples;
    
    import java.text.SimpleDateFormat;
    
    import java.util.Date;
    
    import org.tiling.scheduling.Scheduler;
    import org.tiling.scheduling.SchedulerTask;
    import org.tiling.scheduling.examples.iterators.DailyIterator;
    
    public class AlarmClock {
    
        private final Scheduler scheduler = new Scheduler();
        private final SimpleDateFormat dateFormat =
            new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS");
        private final int hourOfDay, minute, second;
    
        public AlarmClock(int hourOfDay, int minute, int second) {
            this.hourOfDay = hourOfDay;
            this.minute = minute;
            this.second = second;
        }
    
        public void start() {
            scheduler.schedule(new SchedulerTask() {
                public void run() {
                    soundAlarm();
                }
                private void soundAlarm() {
                    System.out.println("Wake up! " +
                        "It's " + dateFormat.format(new Date()));
                    // Start a new thread to sound an alarm...
                }
            }, new DailyIterator(hourOfDay, minute, second));
        }
    
        public static void main(String[] args) {
            AlarmClock alarmClock = new AlarmClock(7, 0, 0);
            alarmClock.start();
        }
    }
    
    package org.tiling.scheduling.examples.iterators;
    
    import org.tiling.scheduling.ScheduleIterator;
    
    import java.util.Calendar;
    import java.util.Date;
    
    /**
     * A DailyIterator class returns a sequence of dates on subsequent days
     * representing the same time each day.
     */
    public class DailyIterator implements ScheduleIterator {
        private final int hourOfDay, minute, second;
        private final Calendar calendar = Calendar.getInstance();
    
        public DailyIterator(int hourOfDay, int minute, int second) {
            this(hourOfDay, minute, second, new Date());
        }
    
        public DailyIterator(int hourOfDay, int minute, int second, Date date) {
            this.hourOfDay = hourOfDay;
            this.minute = minute;
            this.second = second;
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            calendar.set(Calendar.MINUTE, minute);
            calendar.set(Calendar.SECOND, second);
            calendar.set(Calendar.MILLISECOND, 0);
            if (!calendar.getTime().before(date)) {
                calendar.add(Calendar.DATE, -1);
            }
        }
    
        public Date next() {
            calendar.add(Calendar.DATE, 1);
            return calendar.getTime();
        }
    
    }
    
    
    package org.tiling.scheduling;
    
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class Scheduler {
    
        class SchedulerTimerTask extends TimerTask {
            private SchedulerTask schedulerTask;
            private ScheduleIterator iterator;
            public SchedulerTimerTask(SchedulerTask schedulerTask,
                    ScheduleIterator iterator) {
                this.schedulerTask = schedulerTask;
                this.iterator = iterator;
            }
            public void run() {
                schedulerTask.run();
                reschedule(schedulerTask, iterator);
            }
        }
    
        private final Timer timer = new Timer();
    
        public Scheduler() {
        }
    
        public void cancel() {
            timer.cancel();
        }
    
        public void schedule(SchedulerTask schedulerTask,
                ScheduleIterator iterator) {
    
            Date time = iterator.next();
            if (time == null) {
                schedulerTask.cancel();
            } else {
                synchronized(schedulerTask.lock) {
                    if (schedulerTask.state != SchedulerTask.VIRGIN) {
                      throw new IllegalStateException("Task already 
                      scheduled " + "or cancelled");
                    }
                    schedulerTask.state = SchedulerTask.SCHEDULED;
                    schedulerTask.timerTask =
                        new SchedulerTimerTask(schedulerTask, iterator);
                    timer.schedule(schedulerTask.timerTask, time);
                }
            }
        }
    
        private void reschedule(SchedulerTask schedulerTask,
                ScheduleIterator iterator) {
    
            Date time = iterator.next();
            if (time == null) {
                schedulerTask.cancel();
            } else {
                synchronized(schedulerTask.lock) {
                    if (schedulerTask.state != SchedulerTask.CANCELLED) {
                        schedulerTask.timerTask =
                            new SchedulerTimerTask(schedulerTask, iterator);
                        timer.schedule(schedulerTask.timerTask, time);
                    }
                }
            }
        }
    
    }
    
    
                    
    package org.tiling.scheduling;
    
    import java.util.TimerTask;
    
    public abstract class SchedulerTask implements Runnable {
    
        final Object lock = new Object();
    
        int state = VIRGIN;
        static final int VIRGIN = 0;
        static final int SCHEDULED = 1;
        static final int CANCELLED = 2;
    
        TimerTask timerTask;
    
        protected SchedulerTask() {
        }
    
        public abstract void run();
    
        public boolean cancel() {
            synchronized(lock) {
                if (timerTask != null) {
                    timerTask.cancel();
                }
                boolean result = (state == SCHEDULED);
                state = CANCELLED;
                return result;
            }
        }
    
        public long scheduledExecutionTime() {
            synchronized(lock) {
             return timerTask == null ? 0 : timerTask.scheduledExecutionTime();
            }
        }
    
    }
    The code above is the alarm clock. Could anybody help me out to make it to only one class except for the DailyIerator. And apply runnable so that I can just call the run method and supply the parameters e.g: TaskScheduler.run(12,0,0);. I'm fairly new to java.

    pls pls help me out.

    Fabs
    Last edited by fabs; 04-14-2009 at 01:56 PM. Reason: added description

  2. #2
    manoj.maniraj is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Task Scheduler
    By fabs in forum Threads and Synchronization
    Replies: 3
    Last Post: 04-21-2009, 11:17 AM
  2. Replies: 0
    Last Post: 04-12-2009, 04:16 PM
  3. Executing java program using windows scheduler
    By kanitha in forum New To Java
    Replies: 0
    Last Post: 03-21-2009, 09:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •