【Java开源代码栏目提醒】:网学会员为需要Java开源代码的朋友们搜集整理了AlarmDaemon.java相关资料,希望对各位网友有所帮助!
package alarm;
import javax.jms.*;
import javax.naming.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class AlarmDaemon extends JPanel
implements ActionListener, MessageListener
{
TopicSubscriber subscriber;
TopicSession tsession;
TopicConnection tconn;
Context context;
public static final String shutdown_action="shut";
public AlarmDaemon(String user,String pw)
{
JLabel label;
JButton button;
JPanel tmp;
Font f = new Font("Serif",Font.PLAIN,16);
Font bigFont = new Font("Serif",Font.BOLD,48);
TopicConnectionFactory tfactory;
Topic topic;
setFont(f);
try
{
context = new InitialContext();
tfactory
= (TopicConnectionFactory) context.lookup("TopicConnectionFactory");
tconn
= tfactory.createTopicConnection(user,pw);
tsession
= tconn.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
topic
= (Topic) context.lookup(AlarmServer.ALARM_TOPIC);
subscriber = tsession.createSubscriber(topic);
subscriber.setMessageListener(this);
tconn.start();
}
catch(Exception exp)
{
exp.printStackTrace();
System.exit(0);
}
setLayout(new BorderLayout(6,6));
label = new JLabel("Alarm Daemon",JLabel.CENTER);
label.setFont(bigFont);
add(label,"Center");
tmp = new JPanel();
tmp.setLayout(new FlowLayout(FlowLayout.CENTER));
button = new JButton("Shutdown");
button.addActionListener(this);
button.setActionCommand(shutdown_action);
tmp.add(button);
add(tmp,"South");
}
public void onMessage(Message msg)
{
try
{
MapMessage map = (MapMessage)msg;
Alarm alarm = new Alarm(map);
long interval=300;
int beeps=5;
int i;
Toolkit kit = Toolkit.getDefaultToolkit();
if(alarm != null)
{
for(i=0;i<beeps;i++)
{
kit.beep();
Thread.sleep(interval);
}
JOptionPane.showMessageDialog(null
,alarm.getMessage()
,"Alarm"
,JOptionPane.INFORMATION_MESSAGE);
kit.beep();
}
}
catch(Exception exp)
{
}
}
public void actionPerformed(ActionEvent evt)
{
String cmd = evt.getActionCommand();
if(shutdown_action.equals(cmd))
{
doShutdown();
}
}
protected void doShutdown()
{
try
{
close();
setVisible(false);
System.exit(0);
}
catch(Exception exp)
{
System.exit(0);
}
}
public void close()
{
try
{
subscriber.close();
tsession.close();
tconn.close();
}
catch(Exception exp)
{
exp.printStackTrace();
}
}
public static void main(String s[])
{
JFrame frame;
AlarmDaemon panel;
frame = new JFrame("Alarm Daemon");
panel = new AlarmDaemon("AlarmDaemon",null);
//So we can catch errors in a log file
System.setErr(System.out);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setForeground(Color.black);
frame.setBackground(Color.lightGray);
frame.getContentPane().add(panel,"Center");
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new DaemonWindowCloser(panel));
}
}
class DaemonWindowCloser extends WindowAdapter
{
AlarmDaemon test;
public DaemonWindowCloser(AlarmDaemon test)
{
this.test = test;
}
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
test.close();
win.setVisible(false);
System.exit(0);
}
}