【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了TopicPublisherTest.java提供大家参考,希望对大家有所帮助!
import javax.jms.*;
import javax.naming.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TopicPublisherTest extends JPanel
implements ActionListener
{
JTextField sendField;
JTextField topicField;
TopicSession session;
TopicPublisher sender;
TopicConnection conn;
TopicConnectionFactory factory;
Context context;
public static final String send_action="send";
public static final String topic_action="topic";
public TopicPublisherTest(String user)
{
JLabel label;
JButton button;
JPanel tmp;
Font f = new Font("Serif",Font.PLAIN,16);
setFont(f);
try
{
context =new InitialContext();
factory
= (TopicConnectionFactory) context.lookup("TopicConnectionFactory");
conn
= factory.createTopicConnection(user,null);
session
= conn.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
if(session == null) throw new Exception();
}
catch(Exception exp)
{
exp.printStackTrace();
System.exit(0);
}
setLayout(new GridLayout(2,1,6,6));
topicField = new JTextField(20);
topicField.addActionListener(this);
topicField.setActionCommand(topic_action);
label = new JLabel("Topic:");
button = new JButton("Change");
button.addActionListener(this);
button.setActionCommand(topic_action);
tmp = new JPanel();
tmp.setLayout(new FlowLayout(FlowLayout.LEFT,6,0));
label.setFont(f);
topicField.setFont(f);
button.setFont(f);
tmp.add(label);
tmp.add(topicField);
tmp.add(button);
add(tmp);
sendField = new JTextField(20);
sendField.addActionListener(this);
sendField.setActionCommand(send_action);
label = new JLabel("Message:");
button = new JButton("Send");
button.addActionListener(this);
button.setActionCommand(send_action);
tmp = new JPanel();
tmp.setLayout(new FlowLayout(FlowLayout.LEFT,6,0));
label.setFont(f);
sendField.setFont(f);
button.setFont(f);
tmp.add(label);
tmp.add(sendField);
tmp.add(button);
add(tmp);
}
public void setTopic(String name)
{
Topic topic=null;
try
{
conn.stop();
if(sender!=null) sender.close();
topic
= (Topic) context.lookup(name);
sender = session.createPublisher(topic);
conn.start();
if((topic == null)
||(sender==null)) throw new Exception();
}
catch(Exception exp)
{
exp.printStackTrace();
System.exit(0);
}
}
public void sendMessage(String text)
{
TextMessage msg;
try
{
msg = session.createTextMessage();
msg.setText(text);
sender.publish(msg);
}
catch(Exception exp)
{
JOptionPane.showMessageDialog(this
,exp.toString()
,"Send Error"
,JOptionPane.ERROR_MESSAGE);
}
}
public void actionPerformed(ActionEvent evt)
{
String cmd = evt.getActionCommand();
if(send_action.equals(cmd))
{
sendMessage(sendField.getText());
}
else if(topic_action.equals(cmd))
{
setTopic(topicField.getText());
}
}
public void close()
{
try
{
sender.close();
session.close();
conn.close();
}
catch(Exception exp)
{
exp.printStackTrace();
}
}
public static void main(String s[])
{
JFrame frame;
TopicPublisherTest panel;
String user="TopicPublisherTest";
if(s.length>0) user = s[0];
frame = new JFrame(user);
panel = new TopicPublisherTest(user);
//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 WindowCloser(panel));
}
}
class WindowCloser extends WindowAdapter
{
TopicPublisherTest test;
public WindowCloser(TopicPublisherTest test)
{
this.test = test;
}
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
test.close();
win.setVisible(false);
System.exit(0);
}
}