【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“AlarmServer.java”一文,供大家参考学习!
package alarm;
import javax.jms.*;
import javax.naming.*;
import ptimer.*;
import java.text.*;
import java.util.*;
import java.io.*;
public class AlarmServer
implements PTimerWentOffListener, Runnable
{
public final static String ADMIN_QUEUE
="jms/alarmadminqueue";
public final static String ALARM_TOPIC
="jms/alarmalarmtopic";
public final static String ACTION="action";
public final static String SHUTDOWN="down";
public final static String SET="set";
public final static String DELETE="delete";
public final static String GET_ALARMS="get";
public final static String ALARM="alarm";
Queue adminQueue;//the queue for receiving admin messages
QueueReceiver adminReceiver;
QueueSession session;
QueueConnection conn;
Topic alarmTopic;//the topic for sending alarm messages
TopicPublisher alarmPublisher;
TopicSession tsession;
TopicConnection tconn;
Hashtable timers;//holds the timer specs for each alarm
Hashtable alarm2timer;//maps the alarms to their timer
DateFormat dateFormat;//converts string to Date
Context context;
public AlarmServer(String user,String pw)
{
TopicConnectionFactory tfactory;
QueueConnectionFactory factory;
dateFormat = new SimpleDateFormat();
timers = new Hashtable();
alarm2timer = new Hashtable();
try
{
context = new InitialContext();
loadAlarms();
factory
= (QueueConnectionFactory) context.lookup("QueueConnectionFactory");
conn
= factory.createQueueConnection(user,pw);
session
= conn.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
//Create the queues, or
//handle failure if they exist
try
{
adminQueue =session.createQueue(ADMIN_QUEUE);
}
catch(Exception exp)
{
}
adminQueue = (Queue)
context.lookup(ADMIN_QUEUE);
adminReceiver = session.createReceiver(adminQueue);
//Create the topic related objects
tfactory
= (TopicConnectionFactory)
context.lookup("TopicConnectionFactory");
tconn
= tfactory.createTopicConnection(user,pw);
tsession
= tconn.createTopicSession(false
,Session.AUTO_ACKNOWLEDGE);
try
{
alarmTopic = tsession.createTopic(ALARM_TOPIC);
}
catch(Exception exp)
{
}
alarmTopic = (Topic)
context.lookup(ALARM_TOPIC);
alarmPublisher = tsession.createPublisher(alarmTopic);
alarmPublisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
conn.start();
tconn.start();
}
catch(Exception exp)
{
exp.printStackTrace();
System.exit(0);
}
}
protected void loadAlarms()
{
FileInputStream fileIn;//Stream for reading
BufferedInputStream bufIn;//Stream for reading
ObjectInputStream objIn;//Stream for reading
String fileName;//File to read
Vector readFrom;//Vector read from file
Alarm alarm;//temp variable
int i,max;//temp variable
long now= System.currentTimeMillis();
long alarmTime;//temp variable
PTimer timer = PTimer.getTimer();//shared PTimer
PTimerSpec spec;//temp variable
try
{
fileName
= "c:\\temp\\alarm.ser";
fileIn = new FileInputStream(fileName);
bufIn = new BufferedInputStream(fileIn);
objIn = new ObjectInputStream(bufIn);
readFrom = (Vector) objIn.readObject();
objIn.close();
max = readFrom.size();
for(i=0;i<max;i++)
{
alarm = (Alarm) readFrom.elementAt(i);
alarmTime
= dateFormat.parse(alarm.getTime()).getTime();
if(alarmTime > now)
{
spec = new PTimerSpec();
spec.setAbsoluteTime(alarmTime);
spec.addPTimerWentOffListener(this);
timers.put(spec,alarm);
alarm2timer.put(alarm,spec);
timer.schedule(spec);
}
}
System.out.println("Loaded "+max+" alarms.");
}
catch(Exception exp)
{
System.out.println("No Alarms to Load");
}
}
protected void saveAlarms()
{
FileOutputStream fileOut;
BufferedOutputStream bufOut;
ObjectOutputStream objOut;
String fileName;
Vector toWrite = new Vector();
Enumeration cursor;
try
{
cursor = alarm2timer.keys();
while(cursor.hasMoreElements())
{
toWrite.addElement(cursor.nextElement());
}
fileName
= "c:\\temp\\alarm.ser";
fileOut = new FileOutputStream(fileName);
bufOut = new BufferedOutputStream(fileOut);
objOut = new ObjectOutputStream(bufOut);
objOut.writeObject(toWrite);
objOut.close();
}
catch(Exception exp)
{
exp.printStackTrace();
}
}
public void run()
{
Message msg=null;
while(true)
{
try
{
MapMessage map;
String action;
msg = adminReceiver.receive();
map = (MapMessage) msg;
action = map.getString(ACTION);
if(SET.equals(action))
{