⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jmstestclient.java

📁 p2p 是现在java社区最火的一个话题, 看看jxta能给P2p带来什么吧
💻 JAVA
字号:
import java.awt.*;
import javax.jms.*;
import javax.swing.*;
import javax.naming.*;
import java.awt.event.*;
import java.util.*;


public class JMSTestClient extends JFrame implements ActionListener
{
	private Context jndiContxt = null;
	private QueueConnectionFactory qConnectionFactory = null;

	private JButton sendButton = null;
	private JLabel repLabel = null;	
    private JLabel msgLabel = null;	
    private JTextField repBox = null;	
    private JTextField msgBox = null;	    
	private JTextArea outputArea = null;

	private String queueName, clientName;

	public JMSTestClient ( String queueName)
	{
		super ("[JMS Test Client]");
        this.queueName = queueName;
        this.clientName = "jms/" + queueName;
		prepareGUI ();
		initialize ();
	}

	/*
		The method prepares the GUI of the application and attaches the
		event listeners.
	*/
	private void prepareGUI ()
	{
		Container c = getContentPane ();
		c.setLayout ( new FlowLayout ( FlowLayout.LEFT ) );

        repLabel = new JLabel ( "Receipeint Name" );
        repBox = new JTextField ();
		msgLabel = new JLabel ( "Message" );
		msgBox = new JTextField ();
		repBox.setPreferredSize( new Dimension (110, 20));
		msgBox.setPreferredSize( new Dimension (165, 20));
		
		sendButton = new JButton ( "Send message" );		
		sendButton.addActionListener (this);
		outputArea = new JTextArea ( 7, 20 );

        c.add (repLabel);
        c.add (repBox);
        c.add (msgLabel);
        c.add (msgBox);
		c.add (new JScrollPane ( outputArea ) );
		c.add (sendButton );

		setSize ( 250, 230 );

		show ();
	}

	/*
		The method performs initialization for the application.
		Step 1. Create a JNDI initial context.
		Step 2. Look up connection factory.
		Step 3. Start the receiver.
	*/
	private void initialize ()
	{
		try {

			/***Step 1***/
			jndiContxt = new InitialContext ();
			
			/***Step 2***/
			qConnectionFactory = (QueueConnectionFactory)jndiContxt.lookup ("jms/QueueConnectionFactory");
		}
		catch (NamingException nEx) {
			nEx.printStackTrace ();
			System.exit (1);
		}

		/***Step 3***/
		startReceiver ();
	}//initialize()

	/*
		Overridden method of ActionListener interface, handles the button-click events.
	*/
	public void actionPerformed (ActionEvent e)
	{
		if (e.getSource() == sendButton)
			sendMsg ();
	}//actionPerformed()


	/*
		sendMsg() method sends a message to the target queue.

		Step 1. Create a queue connection.
		Step 2. Create a queue session.
		Step 3. Lookup the target queue.
		Step 4. Create the sender for sending messages.
		Step 5. Create the message and prepare it.
		Step 6. Send the message.
		Step 7. Send a non-text control message to mark the
				end of message stream.
	*/
	private void sendMsg ()
	{
		Queue queue = null;
		QueueSender queueSender = null;
		TextMessage txtMsg = null;
		QueueConnection sendConn = null;
		QueueSession sendSession = null;


		try {
			/***Step 1***/
			sendConn = qConnectionFactory.createQueueConnection ();

			/***Step 2***/
			sendSession = sendConn.createQueueSession (false, Session.AUTO_ACKNOWLEDGE);

			/***Step 3***/
			queue = (Queue)jndiContxt.lookup ("jms/"+repBox.getText());

			/***Step 4***/
			queueSender	= sendSession.createSender (queue);

			/***Step 5***/
			txtMsg = sendSession.createTextMessage ();
            txtMsg.setStringProperty("Sender", queueName);
			txtMsg.setText (msgBox.getText());

            outputArea.append ("\r\n"+msgBox.getText()+"sent. from ="+ queueName+"\n");
			/***Step 6***/
			queueSender.send (txtMsg);

			/***Step 7***/
			queueSender.send (sendSession.createMessage());
		}//try
		
		catch (NamingException nEx) {
			nEx.printStackTrace ();
		}
		catch (JMSException jmsEx) {
			jmsEx.printStackTrace ();
		}
		finally {
			if (sendConn != null) {
				try {
					sendConn.close ();
				}
				catch (JMSException jmsEx) {}
			}
		}//finally
		
	}//sendMsg


	/*
		startReceiver() method starts up the thread 
		which receives the message.

		Step 1. Create a connection.
		Step 2. Create a session.
		Step 3. Lookup the queue name.
		Step 4. Create the queue receiver to receive messages.
		Step 5. Start message delivery.
		Step 6. Wait for messages and display them.
	*/
	private void startReceiver ()
	{
		new Thread () {
			TextMessage msg;
			Queue recvQueue = null;
			QueueReceiver qReceiver	= null;
			QueueConnection recvConn = null;
			QueueSession recvSession = null;


			public void run ()
			{
				try {
					/***Step 1***/
					recvConn = qConnectionFactory.createQueueConnection ();

					/***Step 2***/
					recvSession = recvConn.createQueueSession (false, Session.AUTO_ACKNOWLEDGE);

					/***Step 3***/
					outputArea.append ("Looking up: " + clientName + " Queue\n");
					recvQueue = (Queue)jndiContxt.lookup (clientName);

					/***Step 4***/
					qReceiver = recvSession.createReceiver (recvQueue);

					/***Step 5***/
					outputArea.append ("Listening for incoming messages..\n");
					recvConn.start ();
				}//try
				
				catch (NamingException nEx) {
					nEx.printStackTrace ();
					return;
				}
				catch (JMSException jmsEx) {
					jmsEx.printStackTrace ();
					return;
				}

				/***Step 6***/
				while (true) 
				{
					try {
						Message m = qReceiver.receive ();
						if (m != null) {
							if (m instanceof TextMessage) {
								msg = (TextMessage)m;
								outputArea.append ("Message Recived: " + msg.getText() + "\n");
							}
						}
					}
					catch (JMSException jmsEx) {
						jmsEx.printStackTrace ();
					}
				}//while (true)
				
			}//run()
			
		}.start ();
		
	}//startReceiver()
	

	public static void main (String args[])
	{
		JMSTestClient app = new JMSTestClient (args[0]);

		app.addWindowListener (
			new WindowAdapter () {
				public void windowClosing (WindowEvent e)
				{
					System.exit (0);
				}
			});
			
	}//main()

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -