How can I program a "Time Listener"?

Discussion in 'Java' started by badgerduke@gmail.com, Jul 1, 2005.

  1. Guest

    I have an interest in creating some Java object which will "listen for
    a certain time" and then trigger. For example, I'd like the object to
    wake up 6:00AM and do something. I'd like the object to react to the
    arrival of a certian time_of_day on certain days. Does anyone know how
    to do this besides using Quartz. Thanks.


    Eric
    , Jul 1, 2005
    #1
    1. Advertising

  2. Chris Smith Guest

    <> wrote:
    > I have an interest in creating some Java object which will "listen for
    > a certain time" and then trigger. For example, I'd like the object to
    > wake up 6:00AM and do something. I'd like the object to react to the
    > arrival of a certian time_of_day on certain days. Does anyone know how
    > to do this besides using Quartz. Thanks.


    Here's some very untested code to do what you're looking for. It should
    give you a general idea.

    import java.util.Calendar;
    import java.util.Date;
    import java.util.SortedSet;
    import java.util.TreeSet;

    public class Clock
    {
    public interface ClockListener
    {
    public void alarmSounded(ClockEvent event);
    }

    public static class ClockEvent
    {
    private Clock clock;
    private Date timestamp;

    private ClockEvent(Clock clock, Date timestamp)
    {
    this.clock = clock;
    this.timestamp = timestamp;
    }

    public Clock getClock()
    {
    return clock;
    }
    public Date getTimestamp()
    {
    return timestamp;
    }
    }

    private class ClockTask implements Runnable
    {
    private boolean running = true;

    public void run()
    {
    while (running)
    {
    synchronized (this)
    {
    long interval;
    while (running
    && (interval = getNextAlarmInterval()) > 0)
    {
    try
    {
    wait(interval);
    }
    catch (InterruptedException e) { }
    }
    }

    if (running)
    {
    Alarm alarm = alarms.first();
    alarms.remove(alarm);

    Date time = alarm.nextTime.getTime();
    ClockEvent event = new ClockEvent(Clock.this, time);

    alarm.listener.alarmSounded(event);

    if (alarm.intervalMagnitude > 0)
    {
    alarm.nextTime.add(
    alarm.intervalUnit,
    alarm.intervalMagnitude);
    alarms.add(alarm);
    }
    }
    }
    }

    private long getNextAlarmInterval()
    {
    Alarm nextAlarm = alarms.first();
    long nextTime = nextAlarm.nextTime.getTimeInMillis();
    return nextTime - System.currentTimeMillis();
    }

    private synchronized void close()
    {
    running = false;
    notify();
    }
    }

    private static class Alarm implements Comparable<Alarm>
    {
    private ClockListener listener;
    private Calendar nextTime;

    private int intervalMagnitude;
    private int intervalUnit;

    private Alarm(
    ClockListener listener,
    Calendar time,
    int magnitude,
    int unit)
    {
    this.listener = listener;
    nextTime = time;
    intervalMagnitude = magnitude;
    intervalUnit = unit;
    }

    public int compareTo(Alarm o)
    {
    return nextTime.compareTo(o.nextTime);
    }
    }

    private SortedSet<Alarm> alarms = new TreeSet<Alarm>();
    private ClockTask task = new ClockTask();

    public Clock()
    {
    new Thread(task).start();
    }

    public void close()
    {
    task.close();
    }

    private void schedule(Alarm alarm)
    {
    synchronized (task)
    {
    alarms.add(alarm);
    task.notify();
    }
    }

    public void scheduleOnce(ClockListener listener, Date d)
    {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);

    Alarm alarm = new Alarm(listener, cal, 0, 0);
    schedule(alarm);
    }

    public void scheduleDaily(ClockListener listener, Date d)
    {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);

    Alarm alarm = new Alarm(listener, cal, 1, Calendar.DATE);
    schedule(alarm);
    }

    public void scheduleWeekly(ClockListener listener, Date d)
    {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);

    Alarm alarm = new Alarm(
    listener, cal, 1, Calendar.WEEK_OF_YEAR);
    schedule(alarm);
    }
    }

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jul 1, 2005
    #2
    1. Advertising

  3. Wibble Guest

    Chris Smith wrote:
    > <> wrote:
    >
    >>I have an interest in creating some Java object which will "listen for
    >>a certain time" and then trigger. For example, I'd like the object to
    >>wake up 6:00AM and do something. I'd like the object to react to the
    >>arrival of a certian time_of_day on certain days. Does anyone know how
    >>to do this besides using Quartz. Thanks.

    >
    >
    > Here's some very untested code to do what you're looking for. It should
    > give you a general idea.
    >
    > import java.util.Calendar;
    > import java.util.Date;
    > import java.util.SortedSet;
    > import java.util.TreeSet;
    >
    > public class Clock
    > {
    > public interface ClockListener
    > {
    > public void alarmSounded(ClockEvent event);
    > }
    >
    > public static class ClockEvent
    > {
    > private Clock clock;
    > private Date timestamp;
    >
    > private ClockEvent(Clock clock, Date timestamp)
    > {
    > this.clock = clock;
    > this.timestamp = timestamp;
    > }
    >
    > public Clock getClock()
    > {
    > return clock;
    > }
    > public Date getTimestamp()
    > {
    > return timestamp;
    > }
    > }
    >
    > private class ClockTask implements Runnable
    > {
    > private boolean running = true;
    >
    > public void run()
    > {
    > while (running)
    > {
    > synchronized (this)
    > {
    > long interval;
    > while (running
    > && (interval = getNextAlarmInterval()) > 0)
    > {
    > try
    > {
    > wait(interval);
    > }
    > catch (InterruptedException e) { }
    > }
    > }
    >
    > if (running)
    > {
    > Alarm alarm = alarms.first();
    > alarms.remove(alarm);
    >
    > Date time = alarm.nextTime.getTime();
    > ClockEvent event = new ClockEvent(Clock.this, time);
    >
    > alarm.listener.alarmSounded(event);
    >
    > if (alarm.intervalMagnitude > 0)
    > {
    > alarm.nextTime.add(
    > alarm.intervalUnit,
    > alarm.intervalMagnitude);
    > alarms.add(alarm);
    > }
    > }
    > }
    > }
    >
    > private long getNextAlarmInterval()
    > {
    > Alarm nextAlarm = alarms.first();
    > long nextTime = nextAlarm.nextTime.getTimeInMillis();
    > return nextTime - System.currentTimeMillis();
    > }
    >
    > private synchronized void close()
    > {
    > running = false;
    > notify();
    > }
    > }
    >
    > private static class Alarm implements Comparable<Alarm>
    > {
    > private ClockListener listener;
    > private Calendar nextTime;
    >
    > private int intervalMagnitude;
    > private int intervalUnit;
    >
    > private Alarm(
    > ClockListener listener,
    > Calendar time,
    > int magnitude,
    > int unit)
    > {
    > this.listener = listener;
    > nextTime = time;
    > intervalMagnitude = magnitude;
    > intervalUnit = unit;
    > }
    >
    > public int compareTo(Alarm o)
    > {
    > return nextTime.compareTo(o.nextTime);
    > }
    > }
    >
    > private SortedSet<Alarm> alarms = new TreeSet<Alarm>();
    > private ClockTask task = new ClockTask();
    >
    > public Clock()
    > {
    > new Thread(task).start();
    > }
    >
    > public void close()
    > {
    > task.close();
    > }
    >
    > private void schedule(Alarm alarm)
    > {
    > synchronized (task)
    > {
    > alarms.add(alarm);
    > task.notify();
    > }
    > }
    >
    > public void scheduleOnce(ClockListener listener, Date d)
    > {
    > Calendar cal = Calendar.getInstance();
    > cal.setTime(d);
    >
    > Alarm alarm = new Alarm(listener, cal, 0, 0);
    > schedule(alarm);
    > }
    >
    > public void scheduleDaily(ClockListener listener, Date d)
    > {
    > Calendar cal = Calendar.getInstance();
    > cal.setTime(d);
    >
    > Alarm alarm = new Alarm(listener, cal, 1, Calendar.DATE);
    > schedule(alarm);
    > }
    >
    > public void scheduleWeekly(ClockListener listener, Date d)
    > {
    > Calendar cal = Calendar.getInstance();
    > cal.setTime(d);
    >
    > Alarm alarm = new Alarm(
    > listener, cal, 1, Calendar.WEEK_OF_YEAR);
    > schedule(alarm);
    > }
    > }
    >

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Timer.html
    Wibble, Jul 2, 2005
    #3
  4. Chris Smith Guest

    Wibble <> wrote:
    > http://java.sun.com/j2se/1.4.2/docs/api/java/util/Timer.html
    >


    java.util.Timer won't do exactly what was asked. The OP asked for code
    that would do something at the same time every day. Given daylight
    savings time, you can't use a repeating task with Timer for this.

    You can use a non-repeating task, and just reschedule it each time it
    fires. In that case, just take the Calendar code from what I posted
    earlier, and ignore the Thread-related stuff.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jul 2, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Coca
    Replies:
    15
    Views:
    641
    Alan Balmer
    Jan 14, 2004
  2. Shawn
    Replies:
    2
    Views:
    1,473
    Shawn
    Dec 6, 2006
  3. r.e.s.
    Replies:
    3
    Views:
    306
    r.e.s.
    Jul 11, 2008
  4. Hal Vaughan
    Replies:
    7
    Views:
    121
    Joe Smith
    Sep 6, 2005
  5. Yesterday Paid
    Replies:
    1
    Views:
    265
    Dave Angel
    Jun 21, 2012
Loading...

Share This Page