JMS learning experience (1)

Source: Internet
Author: User
Tags sub domain

Some Learning Experiences
P2P mode:

Concept: three roles: queue, queuesender, queuereceiver, queue, message sender, and message receiver. The sender adds a message to the queue. The receiver fetches the message from the queue. one message in the queue receives one fewer message. If multiple receivers connect to the queue, one message can only be taken by one receiver.
I,
The default message sending mode is deliverymode. Persistent.
Method, that is, the persistence method. The constant value is 2. In this way, messages will not be lost and will be stored on the server. After a message is sent, even if you disable the JMS server (for example,
After the service is restarted, the message still exists, and other messages that use the non_persistent method are lost. I think in most cases, we need
The persistent method!
Deliverymode. The constant value of non_persistent is 1.
Deliverymode. The persistent constant value is 2.
Int A = msgp0.getdeliverymode ();
// Msgp0.setdeliverymode (deliverymode. Persistent );

2. the consumer of a message can receive messages in two ways:

1. Consumer. Receive () or consumer. Receive (INT timeout );
2. Register a messagelistener.

The first method is synchronous. When the receive method is used, the Message Receiver waits until a message arrives or times out. You can call this method to receive only one message at a time.
The second method is used, that is, the asynchronous method, and the program will listen continuously. As long as there is a message, it will respond to the onmessage method.
The listener ends when the conn. Close () method is used.
Messageconsumer comsumer
=
Session. createconsumer (Queue );
Comsumer. setmessagelistener (
New
Messagelistener (){
@ Override

Public
 
Void
Onmessage (message m ){
Textmessage textmsg
=
(Textmessage) m;

Try
{
System. Out. println (textmsg. gettext ());
}
Catch
(Jmsexception e ){
E. printstacktrace ();
}
}

});
Test found
Conn. Close ()
The method is somewhat different for the two methods. For the listener method, the listener will terminate if the close method is executed. It is estimated that the listener is executed in the background thread mode. For the recive receiving method, as long
As long as the receive method is called once, the following statement will be executed only when a message arrives. This is a blocking method, even if the following conn. Close () is not executed.
Reference: http://www.cnblogs.com/iloveu/archive/2009/06/10/1500714.html

Pub/sub Mode
:
Concepts: topic, publisher, subscriber, topic, publisher, and subscriber. A topic is a one-to-many relationship with a subscriber. A topic can be subscribed to by multiple subscriber. When a publisher sends a message to a topic, all subscribers receive the message.
How can we understand the concept of subscription? personal understanding involves two situations:
I,
When creating a subscriber, use session. createdurablesubscriber (topic, clientid
) When the method is created as a persistent subscriber, the subscription relationship between the topic and clientid is saved on the server, that is, the subscription relationship is generated. In this way, the subscriber of the clientid ID will
Messages are not lost when they are offline. This subscription relationship can also be understood as being persisted on the JMS server. Using the JMX monitoring Console (My activemq), you can see that there are
A subscriber node has the client name that you specified when subscribing to a topic. AvailableUnsubscribe
Method
Cancel the subscription relationship.
II,
When creating a non-persistent subscriber, the subscriber relationship is established only when the client subscriber connects to the JMS server. When the subscriber is offline, the subscriber relationship is canceled and will not be saved on the server.
The reason why a persistent subscriber cannot receive messages offline is that. The default value is the broadcast mode. When no subscriber is connected to the server, messages sent will be lost and will not be saved on the server.

Understanding of JMS specifications:

JMS
The specification is the unified API specification of Java Message Service proposed by Sun. The benefits of the specification and standard are needless to say. Java can come to this day, the formulation of normative standards can be said to have a great deal of credit. Institute
All software providers that provide Java Message Service must implement this interface. When writing a JMS program, we can basically only program the interface, without considering the implementation of that manufacturer. This is our generation.
The Code has a large number of flexible lines and is not bound to a specific vendor. As long as your code is well written, you can directly deploy the code on the JMS server of different vendors without modifying the code.
For example, when writing an activemq-based JMS program, the Code is as follows:
Activemqconnectionfactory factory = new
Activemqconnectionfactory ("VM: // localhost"
);
...
Queue queue = new
Activemqqueue ("testqueue"
);
So that the activemq API is bound to the code. To ensure vendor independence, we should use JNDI to obtain factory objects and queue resources.
For example:
Connectionfactory cf = (connectionfactory) CTX. Lookup ("connectionfactory ");
...
Destination DEST = (Queue) CTX. Lookup ("myqueue ");
Note that the interface should also use the large interface of JMS as much as possible. Of course, the JNDI service is required here. activemq itself provides the JNDI service. As long as there is a JNDI. properties file in classpath, you can configure the resources in the configuration file.
For example:
Java. Naming. Factory. Initial = org. Apache. activemq. JNDI. activemqinitialcontextfactory
Connectionfactorynames = connectionfactory, queueconnectionfactory, topicconnectionfactry
Queue. myqueue = zmqueue
Topic. mytopic = zmtopic
.

Some Understanding of JMS specification 1.1:

JMS 1.0.2 defines two types of Message Passing domains (they are independent of each other): point-to-point and publish/subscribe. The latest version of JMS, that is, Version 1.1. JMS unifies these two domains. For example, with JMS 1.1, the client no longer has to specifically implement this or that domain.

JMS public

PTP domain

Pub/sub domain

Connectionfactory Queueconnectionfactory Topicconnectionfactory
Connection Queueconnection Topicconnection
Destination Queue Topic
Session Queuesession Topicsession
Messageproducer Queuesender Topicpublisher
Messageconsumer Queuereceiver Topicsubscriber

Reference: http://www.ibm.com/developerworks/cn/java/j-jms11/index.html

That is to say, only the interface of the JMS public domain can be used to write the JMS program. Therefore, we recommend that you use as many interfaces as possible to increase code flexibility.
For example, you can write a message program in pub/sub mode as follows:
Context CTX = new initialcontext ();
Connectionfactory factory = (connectionfactory) CTX
. Lookup ("connectionfactory ");

Connection connection = factory. createconnection ();
Connection. Start ();

// Create a topic
Destination topic = (destination) CTX. Lookup ("mytopic ");
Session session = connection. createsession (false,
Session. auto_acknowledge );

Messageconsumer comsumer3 = session. createconsumer (topic );
Message MSG = comsumer3.receive ();

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.