New in JMS 2.0
This post lists ALL of the new APIs (interfaces/classes/annotations etc.) introduced in JMS 2.0 (part of the Java EE 7 platform). These have been categorized as follows
- API simplification
- Ease of use
- Exception Handling
- Miscellaneous
Here is a quick summary along with some code snippets
API simplification
JMSContext
Simpler abstraction on top of Connection and Session objects which eliminates the need for interacting with these classes/interfaces in order to send/receive messages.
@Path("email") @Stateless public class EmailService { //pulls in default Conn Factory as per Java EE 7 @Resource ConnectionFactory cf; //application managed JMSContext ctx; @Resource("jms/emailQ") Destination emailQ; @POST public void send(String email) { Session session; try { ctx = cf.createContext(); ctx.createProducer().send(emailQ, email); System.out.println("Message Sent to queue - " + ((Queue) emailQ).getQueueName()); } catch (JMSException ex) { Logger.getLogger(EmailService.class.getName()).log(Level.SEVERE, null, ex); throw new JMSRuntimeException(ex.getMessage(), ex.getMessage(), ex); } finally { //clean up after use. Can also be done as inside a @PreDestroy callback method ctx.close(); System.out.println("JMSContext closed"); } } }
JMSConnectionFactory
Used during JMSContext injection to specify the JNDI name of the JMS ConnectionFactory
@Path("email") @Stateless public class TheBestEmailService { //container managed @Inject @JMSConnectionFactory("jms/myConnectionFactory") JMSContext ctx; @Resource("jms/emailQ") Destination emailQ; @POST public void send(String email) { Session session; try { ctx.createProducer().send(emailQ, email); System.out.println("Message Sent to queue - " + ((Queue) emailQ).getQueueName()); } catch (JMSException ex) { Logger.getLogger(TheBestEmailService.class.getName()).log(Level.SEVERE, null, ex); throw new JMSRuntimeException(ex.getMessage(), ex.getMessage(), ex); } } }
JMSProducer and JMSConsumer
As the name suggests, a JMSProducer and JMSConsumer encapsulate the process of sending JMS messages to and from destinations (topics and queues), respectively. Instances to these objects can be obtained from the JMSContext object and they are important from an API ease-of-use perspective. Here is a ‘fluent’ API example
context.createProducer().send(queue,message); context.createConsumer(topic).receiveBody(Mail.class);
XAJMSContext
Transactional equivalent of the vanilla JMSContext object. The implementation of this interface provides support for JTA within JMS
Ease of use
These annotations empower less reliance on manual/administrative configuration and drive automated deployment of Java EE applications. These are perfect examples of ‘configuration as code’ and invaluable in Cloud (PaaS) deployment scenarios
JMSConnectionFactoryDefinition and JMSConnectionFactoryDefinitions
Specify the JNDI name of one/multiple JMS ConnectionFactory object. This resource will be automatically provisioned at deployment time.
@JMSConnectionFactoryDefinition(name = "jndi/App1JMSConnFactory") @JMSConnectionFactoryDefinitions({@JMSConnectionFactoryDefinition(name = "jndi/App2JMSConnFactory"), @JMSConnectionFactoryDefinition(name = "jndi/App3JMSConnFactory")})
JMSDestinationDefinition and JMSDestinationDefinitions
Specify the JNDI name of one/more JMS Destinations (queues/topics). This resource will be automatically provisioned at deployment time.
@JMSDestinationDefinition(name = "jms/emailQueue", interfaceName = "javax.jms.Queue") @JMSDestinationDefinitions({@JMSDestinationDefinition(name = "portal/notificationQueue", interfaceName = "javax.jms.Queue"), @JMSDestinationDefinition(name = "app/stockPriceTopic", interfaceName = "javax.jms.Topic")})
Exception Handling
JMS 1.1 and earlier versions did not have a notion of unchecked exceptions. From JMS 2.0, JMSRuntimeException has been introduced to act as the base/parent from which all other unchecked exceptions have been extended. Here is a list all the new exceptions introduced in JMS 2.0 (these are mostly unchecked versions of their checked counterparts)
- JMSRuntimeException
- IllegalStateRuntimeException
- InvalidClientIDRuntimeException
- InvalidDestinationRuntimeException
- InvalidSelectorRuntimeException
- JMSSecurityRuntimeException
- MessageFormatRuntimeException
- MessageNotWriteableRuntimeException
- ResourceAllocationRuntimeException
- TransactionInProgressRuntimeException
- TransactionRolledBackRuntimeException
Miscellaneous
JMSPasswordCrdential
Used to secure access to JMS provider before attempting any operations using an injected JMSContext object
Option 1 --------- @Inject @JMSConnectionFactory("jms/myConnectionFactory") @JMSPasswordCredential(password = "secret", userName = "admin") JMSContext ctx; ---------------------------------------------------------------- Option 2 --------- //inject javax.jms.ConnectionFactory @Resource("jndi/AppJMSConnFactory") ConnectionFactory cf; //use it to create the JMSContext JMSContext ctx = cf.createContext("admin","secret");
JMSSessionMode
Specifies session mode to be used during JMSContext injection
@Inject @JMSConnectionFactory("jms/myConnectionFactory") //Accepts an integer. Other options are: JMSContext.SESSION_TRANSACTED, JMSContext.CLIENT_ACKNOWLEDGE and JMSContext.DUPS_OK_ACKNOWLEDGE @JMSSessionMode(JMSContext.AUTO_ACKNOWLEDGE) JMSContext ctx;
That’s it for the new stuff in JMS 2.0 from an API perspective.
Cheers !
Reference: | New in JMS 2.0 from our JCG partner Abhishek Gupta at the Object Oriented.. blog. |