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