【Java开源代码栏目提醒】:网学会员鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“VolQueueSenderTest.java”一文,供大家参考学习
import javax.jms.*;
import javax.naming.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class VolQueueSenderTest extends JPanel
implements ActionListener
{
JTextField sendField;
JTextField queueField;
QueueSession session;
QueueSender sender;
QueueConnection conn;
QueueConnectionFactory factory;
Context context;
public static final String send_action="send";
public static final String queue_action="queue";
public VolQueueSenderTest(String user)
{
JLabel label;
JButton button;
JPanel tmp;
Font f = new Font("Serif",Font.PLAIN,16);
setFont(f);
try
{
context = new InitialContext();
factory
= (QueueConnectionFactory) context.lookup("QueueConnectionFactory");
conn
= factory.createQueueConnection(user,null);
session
= conn.createQueueSession(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));
queueField = new JTextField(20);
queueField.addActionListener(this);
queueField.setActionCommand(queue_action);
label = new JLabel("Queue:");
button = new JButton("Change");
button.addActionListener(this);
button.setActionCommand(queue_action);
tmp = new JPanel();
tmp.setLayout(new FlowLayout(FlowLayout.LEFT,6,0));
label.setFont(f);
queueField.setFont(f);
button.setFont(f);
tmp.add(label);
tmp.add(queueField);
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 setQueue(String name)
{
Queue queue=null;
try
{
conn.stop();
if(sender!=null) sender.close();
queue
= (Queue) context.lookup(name);
sender = session.createSender(queue);
conn.start();
if((queue == 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.send(msg
,DeliveryMode.NON_PERSISTENT
,Message.DEFAULT_PRIORITY
,Message.DEFAULT_TIME_TO_LIVE);
}
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(queue_action.equals(cmd))
{
setQueue(queueField.getText());
}
}
public void close()
{
try
{
sender.close();
session.close();
conn.close();
}
catch(Exception exp)
{
exp.printStackTrace();
}
}
public static void main(String s[])
{
JFrame frame;
VolQueueSenderTest panel;
String user="VolQueueSenderTest";
if(s.length>0) user = s[0];
frame = new JFrame(user);
panel = new VolQueueSenderTest(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
{
VolQueueSenderTest test;
public WindowCloser(VolQueueSenderTest test)
{
this.test = test;
}
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
test.close();
win.setVisible(false);
System.exit(0);
}
}