Ten improvements to message-oriented middleware specification JMS 2.0
Author: chszs, reprinted with note. Blog homepage: http://blog.csdn.net/chszs
Message-oriented middleware specification JMS 2.0 has made many improvements in coding, which can help developers reduce the amount of code written. Next, let me explain it one by one.
1. Use jmscontext to replace the connection and session objects
Message-oriented middleware specification JMS 2.0 introduces a new object, jmscontext, which provides the same functions as the original connection and session objects.
In JMS 1.1, it is as follows:
Connection connection = connectionfactory. createconnection ();
Session session = connection. createsession (false, session. auto_acknowledge );
In JMS 2.0, it is as follows:
Jmscontext context = connectionfactory. createcontext (jmscontext. sessiona_transacted );
Ii. Using the try-with-resources block means you do not need to call the close statement.
If the close statement is not called in JMS 1.1, the memory resource may be exhausted.
In JMS 1.1, it is as follows:
Try {
Connection connection = connectionfactory. createconnection ();
Try {
Session session = connection. createsession (false, session. auto_acknowledge );
} Finally {
Connection. Close ();
}
} Catch (jmsexception ex ){
Ex. printstacktrace ();
}
In JMS 2.0, it is as follows:
Try (connection = connectionfactory. createconnection ();){
......
} Catch (jmsexception ex ){
Ex. printstacktrace ();
}
Note that the above Code can also be changed to jmscontext.
3. You do not need to pass two parameters when creating a session in javase.
In JMS 1.1, it is as follows:
Session session = connection. createsession (true, session. session_transacted );
In JMS 2.0, it is as follows:
Session session = connection. createsession (session. session_transacted );
4. Create a session in a javaee transaction without passing any parameters
When a session is created in a javaee transaction, the parameters passed by createsession are ignored. For details, see the EJB 3.1 specification.
In JMS 1.1, it is as follows:
// The transaction behavior is ignored for the two parameters passed by the container.
Session session = connection. createsession (false, session. client_acknowledge );
In JMS 2.0, it is as follows:
// The No-argument method is provided in JMS 2.
Session session = connection. createsession ();
5. Support for method chains for new jmsproducer objects
Message-oriented middleware specification JMS 2.0 introduces a new object, jmsproducer, which allows message headers, message attributes, delivery options, and so on to be specified through method chains in a single line of code.
In JMS 1.1, it is as follows:
Messageproducer = session. createproducer (domoqueue );
Messageproducer. setpriority (1 );
Textmessage = session. createtextmessage (body );
Textmessage. setstringproperty ("foo", "bar ");
Messageproducer. Send (textmessage );
In JMS 2.0, it is as follows:
Textmessage = context. createtextmessage (body );
Context. createproducer (). setpriority (1). setproperty ("foo", "bar"). Send (demoqueue, textmessage );
6. You do not need to save the jmsproducer variable.
The new jmspruducer object is a lightweight object, so you do not need to save it in the variable and instantiate it directly when necessary.
In JMS 1.1, it is as follows:
// Messageproducer has a high overhead and needs to be reused.
Messageproducer = session. createproducer (demoqueue );
Messageproducer. Send (message1 );
Messageproducer. Send (message2 );
In JMS 2.0, it is as follows:
// Jmsproducer is lightweight and does not need to be saved with variables
Context. createproducer (). Send (demoqueue, message1 );
Context. createproducer (). Send (demoqueue, message2 );
7. In javaee, injecting jmscontext means you do not need to create or disable it.
In JMS 1.1, it is as follows:
Try {
Connection connection = connectionfactory. createconnection ();
Try {
Session session = connection. createsession (false, session. auto_acknowledge );
Messageproducer = session. createproducer (demoqueue );
Textmessage = session. createtextmessage (body );
Messageproducer. Send (textmessage );
} Finally {
Connection. Close ();
}
} Catch (jmsexception ex ){
Ex. printstacktrace ();
}
In JMS 2.0, it is as follows:
Try {
Context. createproducer (). Send (inboundqueue, body );
} Catch (jmsruntimeexception ex ){
Ex. printstacktrace ();
}
8. When a message is sent, the message object does not need to be instantiated.
In JMS 1.1, it is as follows:
// Create a message object of the appropriate type and set its body
Messageproducer = session. createproducer (demoqueue );
Textmessage = session. createtextmessage ("Hello World ");
Messageproducer. Send (textmessage );
In JMS 2.0, it is as follows:
// Simple transfer of the message body to the send Method
Context. createproducer (). Send (demoqueue, "Hello World ");
9. synchronous receiving, which can directly receive message Payload
In JMS 1.1, it is as follows:
// When receiving the message synchronously, you must specify the message object, convert it to a proper subtype, and extract the body.
Messageconsumer = session. createconsumer (demoqueue );
Textmessage = (textmessage) messageconsumer. Receive (1000 );
If (textmessage = NULL)
Return "received null"
Else
Return "received" + textmessage. gettext ();
In JMS 2.0, it is as follows:
// JMS 2 allows you to directly receive the message body
Jmsconsumer consumer = context. createconsumer (demoqueue );
Return "received" + consumer. receivebody (string. Class, 1000 );
10. asynchronous receiving requires no conversion when extracting the message body
In JMS 1.1, it is as follows:
Public void onmessage (message m ){
Myobject myobj = (myobject) (objectmessage) M). GetObject ();
...
In JMS 2.0, it is as follows:
Public void onmessage (message m ){
Myobject myobj = M. getbody (myobject. Class );
...