"JAVA.JMS" point to point communication--queue__java

Source: Internet
Author: User

Single point of transmission--the corresponding data channel for messages is queue queues, message producer Queuesender and message consumers QueueReceiver

JAVAX.JMS public 
Interface Queuesender extends MessageProducer public interface QueueReceiver extends
Messageconsumer


The interface associated with the queue is also:

JAVAX.JMS public
Interface Queueconnectionfactory extends ConnectionFactory public
interface Queueconnection Extends Connection public
interface Queuesession extends session

(Session and connection need to be closed after use.)

you need to open Connection,connection.start () before the message is received.

The model for point-to-point communication is as follows:



The programming model for point-to-point JMS applications is as follows:



Example:

We manually go to send message to queue through the active MQ Admin portlet, creating two consumer in the code to receive the message asynchronously.

The consumer code is as follows:

Package com.gof.jms.test;
Import javax.jms.Connection;
Import Javax.jms.ConnectionFactory;
Import Javax.jms.DeliveryMode;
Import javax.jms.Destination;
Import javax.jms.JMSException;
Import Javax.jms.Message;
Import Javax.jms.MessageConsumer;
Import Javax.jms.MessageListener;
Import Javax.jms.MessageProducer;
Import javax.jms.Session;

Import Javax.jms.TextMessage;

Import Org.apache.activemq.ActiveMQConnectionFactory;
	public class Queuemessageapp {public static final String user = ' System ';
	public static final String password = "Manager";
	public static final String URL = "tcp://localhost:61616";
	public static final String queuename = "Test_queue";
	public static Final Boolean transacted = false;
	
    public static Final Boolean persistent = false;
    	public static void Main (string[] args) {Connection Connection = null;
    	
    	Session session = NULL; try{//Create the connection connectionfactory connectionfactory = new Activemqconnectionfactory (user, password, URL);
            Connection = Connectionfactory.createconnection ();
            
            Connection.start ();
            Create the session session = Connection.createsession (transacted, Session.auto_acknowledge);
            
            Destination Destination = Session.createqueue (queuename);
            
            In this example, we'll send the message through ACTIVEMQ admin portlet manually.
            Create the consumer1 messageconsumer consumer1 = session.createconsumer (destination); Consumer1.setmessagelistener (New MessageListener () {public void OnMessage (Message message) {//TODO Auto-genera
					Ted Method Stub TextMessage recvmessage = (textmessage) message;
					try{System.out.println ("Receive message by the 1st Consumer:" + recvmessage.gettext ());
					}catch (jmsexception e) {e.printstacktrace ();
            
            }
				}
			}); Create the Consumer2 MessaGeconsumer consumer2 = Session.createconsumer (destination); Consumer2.setmessagelistener (New MessageListener () {public void OnMessage (Message message) {//TODO Auto-genera
					Ted Method Stub TextMessage recvmessage = (textmessage) message;
					try{System.out.println ("Receive message by the 2nd Consumer:" + recvmessage.gettext ());
					}catch (jmsexception e) {e.printstacktrace ();
            
            }
				}
			});
            To avoid the connection closed before the message listener received.
            
    	Thread.Sleep (5000000);
    	}catch (Exception e) {e.printstacktrace ();
    		    }finally{try{//close sessions and connection if (session!= null) {session.close ();
    		    } if (connection!= null) {connection.close ();
    		}}catch (Exception e) {e.printstacktrace ();
 }
    	}
    }
}


Start Active MQ First, then start the consumer program above.

Manually send queue messages to target queue in active MQ Admin porlet.



You can get the following program output:



It can be seen that each message is consumed only once, and if multiple consumers are listening to a queue at the same time, it is not possible to determine which consumer will eventually be consumed by a message .




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.