【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-TransQueueSenderTest.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
import javax.jms.*;
import javax.naming.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TransQueueSenderTest 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 commit_action="commit";
public static final String rollback_action="rollback";
public static final String queue_action="queue";
public TransQueueSenderTest(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);
//Create a transacted session
session
= conn.createQueueSession(true,Session.AUTO_ACKNOWLEDGE);
if(session == null) throw new Exception();
}
catch(Exception exp)
{
exp.printStackTrace();
System.exit(0);
}
setLayout(new GridLayout(3,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);
tmp = new JPanel();
tmp.setLayout(new FlowLayout(FlowLayout.CENTER,6,0));
button = new JButton("Commit");
button.addActionListener(this);
button.setActionCommand(commit_action);
button.setFont(f);
tmp.add(button);
button = new JButton("Rollback");
button.addActionListener(this);
button.setActionCommand(rollback_action);
button.setFont(f);
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);
}
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());
}
else if(commit_action.equals(cmd))
{
try
{
session.commit();
}
catch(Exception exp)
{
System.out.println(exp);
}
}
else if(rollback_action.equals(cmd))
{
try
{
session.rollback();
}
catch(Exception exp)
{
System.out.println(exp);
}
}
}
public void close()
{
try
{
sender.close();
session.close();
conn.close();
}
catch(Exception exp)
{
exp.printStackTrace();
}
}
public static void main(String s[])
{
JFrame frame;
TransQueueSenderTest panel;
String user="TransQueueSenderTest";
if(s.length>0) user = s[0];
frame = new JFrame(user);
panel = new TransQueueSenderTest(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));
}
}