How can I program a "Time Listener"?

B

badgerduke

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
 
C

Chris Smith

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
 
W

Wibble

Chris said:
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
 
C

Chris Smith

Wibble said:

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top