JMS.Next(): JMS 2.0 and Beyond

100,902 views

Published on

JMS is the Java standard for accessing enterprise messaging systems. This session introduces JMS 2.0 (JSR 343), the first update in more than a decade and easily the longest-awaited component of the Java EE 7 standard. The biggest new feature of JMS 2.0 is a new API that makes JMS much easier to use, especially in a Java EE application. JMS 2.0 also provides API features to support greater scalability as well as additional messaging features and a long list of minor enhancements and clarifications.

In this session, we will also briefly discuss what might be next for the JMS specification.

Published in: Technology
2 Comments
22 Likes
Statistics
Notes
  • Please post on the GlassFish forums with details? Sounds like a classpath problem.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Hi Reza,
    I get a NoSuchMethodException when I try to call createContext on my connection factory. Can you think of any reason why this would happen?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
100,902
On SlideShare
0
From Embeds
0
Number of Embeds
67,499
Actions
Shares
0
Downloads
358
Comments
2
Likes
22
Embeds 0
No embeds

No notes for slide

JMS.Next(): JMS 2.0 and Beyond

  1. 1. Others Talk, We Listen. JMS.Next(): JMS 2.0 and Beyond Reza Rahman Senior Architect rrahman@captechconsulting.com @reza_rahman
  2. 2. CapTech Full-service US national IT consulting firm that focuses on client best interests, trust, servant leadership, culture, professionalism and technical excellence. #28 in Vault's Consulting Top 50 #3 Best Consulting Internship #9 Best Overall Internship #1 in Meeting Client’s Needs #7 Best Firm to Work For #1 in Career Development Ranked for the 7th Consecutive Year Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  3. 3. JMS • Small, successful Java API for Message Oriented Middleware (MOM) • JMS 1.1 – 2002 • J2EE era! • JMS 2 – 2013 • Included in Java EE 7 • Overdue and well received • JMS 2.1 started • Included in Java EE 8? Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  4. 4. JMS 2 Goals • API Modernization • Some New Features • Java EE Alignment • EJB3/MDB Alignment • Minor Corrections and Clarifications Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  5. 5. Modernizing the API Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  6. 6. JMS 1.1 Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Resource(lookup = "java:global/jms/demoConnectionFactory") private ConnectionFactory connectionFactory; @Resource(lookup = "java:global/jms/demoQueue") private Queue demoQueue; public void sendMessage(String payload) { try { Connection connection = connectionFactory.createConnection(); try { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer messageProducer = session.createProducer(demoQueue); TextMessage textMessage = session.createTextMessage(payload); messageProducer.send(textMessage); } finally { connection.close(); } } catch (JMSException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } }
  7. 7. • Maintain backwards compatibility! • Simplify existing JMS API where possible • Define new abstractions where needed • JMSContext, JMSProducer, JMSConsumer • Take advantage of modern Java EE paradigms • Annotations • Convention-over-configuration • Builder pattern • Injection/scoping Copyright © 2015 CapTech Ventures, Inc. All rights reserved. Simplifying the API Strategy
  8. 8. Streamlining Session Creation • Methods in javax.jms.Connection to create a Session: • Existing method (will remain) • connection.createSession(transacted, deliveryMode) • New method mainly for Java SE • connection.createSession(sessionMode) • New method mainly for Java EE • connection.createSession() Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  9. 9. Auto-Closing JMS Objects • Make JMS objects implement java.jang.AutoCloseable • Connection • Session • MessageProducer • MessageConsumer • QueueBrowser Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  10. 10. JMS Auto-Closable Example Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Resource(lookup = "jms/connFactory") private ConnectionFactory cf; @Resource(lookup="jms/inboundQueue") Destination dest; public void sendMessage (String payload) throws JMSException { try (Connection conn = cf.createConnection(); Session session = conn.createSession(); MessageProducer producer = session.createProducer(dest); ){ Message mess = sess.createTextMessage(payload); producer.send(mess); } catch(JMSException e){ // exception handling } }
  11. 11. A First Look at the New API Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Resource(lookup = "java:global/jms/demoConnectionFactory") private ConnectionFactory connectionFactory; @Resource(lookup = "java:global/jms/demoQueue") private Queue demoQueue; public void sendMessageNew(String payload) { try (JMSContext context = connectionFactory.createContext();){ context.createProducer().send(demoQueue, payload); } catch (JMSRuntimeException ex) { Logger.getLogger(getClass().getName()).log( Level.SEVERE, null, ex); } }
  12. 12. JMSContext • An abstraction that encapsulates Connection, Session, Producer and Consumer • Created from ConnectionFactory • JMSContext context = connectionFactory.createContext(sessionMode); • Used to create JMSProducer objects for sending messages • Used to create JMSConsumer objects for receiving messages • Methods on JMSContext, JMSProducer and JMSConsumer throw only unchecked exceptions Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  13. 13. JMSProducer • Messages are sent by creating a JMSProducer object • Geared towards common use cases while retaining flexibility • Fluent API Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  14. 14. • JMS 1.1 • JMS 2 Copyright © 2015 CapTech Ventures, Inc. All rights reserved. JMSProducer Example Setting message delivery options MessageProducer producer = session.createProducer(); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); producer.setPriority(1); producer.setTimeToLive(1000); producer.send(destination,message); context.createProducer() .setDeliveryMode(DeliveryMode.NON_PERSISTENT) .setPriority(1).setTimeToLive(1000) .send(destination, message);
  15. 15. • JMS 1.1 • JMS 2 Copyright © 2015 CapTech Ventures, Inc. All rights reserved. JMSProducer Example Setting message properties and headers MessageProducer producer = session.createProducer(); TextMessage textMessage = session.createTextMessage("Hello); textMessage.setStringProperty("foo", "bar"); producer.send(destination, message); context.createProducer() .setProperty("foo","bar") .send(destination,"Hello");
  16. 16. Sending Message Payloads Directly • Sending just the payload • send(Destination dest, String payload) • send(Destination dest, Serializable payload) • send(Destination dest, byte[] payload) • send(Destination dest, Map<String, Object> payload) • Use methods on JMSProducer to set delivery options, message headers and message properties • Sending JMS Message Object • send(Destination dest, Message message) Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  17. 17. JMSConsumer • Messages are consumed by creating a JMSConsumer object • Geared towards common use cases while retaining flexibility • Underlying connection is automatically started (configurable) Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  18. 18. Receiving Message Payloads Directly • Methods on JMSConsumer that return message payload directly: • <T> T receivePayload(Class<T> c); • <T> T receivePayload(Class<T> c, long timeout); • <T> T receivePayloadNoWait(Class<T> c); • You can still return a JMS Message Object: • Message receive(); • Message receive(long timeout); • Message receiveNoWait(); Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  19. 19. JMSConsumer Example Copyright © 2015 CapTech Ventures, Inc. All rights reserved. public String receiveMessage() throws NamingException { InitialContext initialContext = getInitialContext(); ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("jms/connectionFactory"); Queue inboundQueue = (Queue) initialContext.lookup("jms/inboundQueue"); try (JMSContext context = connectionFactory.createContext();) { JMSConsumer consumer = context.createConsumer(inboundQueue); return consumer.receivePayload(String.class); } }
  20. 20. Enter the Injection Dragon Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @Inject @JMSConnectionFactory("jms/connectionFactory") private JMSContext context; @Resource(lookup = "jms/inboundQueue") private Queue inboundQueue; public void sendMessage (String payload) { context.createProducer().send(inboundQueue, payload); }
  21. 21. • Connection factory will default to platform default JMS • Specifying session mode • Specifying username and password (may be aliased) Copyright © 2015 CapTech Ventures, Inc. All rights reserved. Injection of JMSContext Objects @Inject private JMSContext context; @Inject @JMSConnectionFactory("jms/connectionFactory") @JMSSessionMode(JMSContext.AUTO_ACKNOWLEDGE) private JMSContext context; @Inject @JMSConnectionFactory("jms/connectionFactory") @JMSPasswordCredential(userName="admin", password="mypassword") private JMSContext context;
  22. 22. • Injected JMSContext objects have a scope • In a JTA transaction, scope is the transaction • If no JTA transaction, scope is the request • JMSContext is automatically closed when scope ends • Inject two JMSContext objects within the same scope and you get the same object • If @JMSConnectionFactory, @JMSPasswordCredential and @JMSSessionMode annotations match • Makes it easier to use same session within a transaction Copyright © 2015 CapTech Ventures, Inc. All rights reserved. Injection Under the Hood
  23. 23. The New Features Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  24. 24. • Allows future timed delivery of a message • Deferred processing, e.g. end of day • Method on JMSProducer • Sets minimum time in ms from that a message should be retained by the messaging system before delivery to a consumer • Also available in the old API Copyright © 2015 CapTech Ventures, Inc. All rights reserved. Delivery Delay public void setDeliveryDelay(long deliveryDelay)
  25. 25. • Send a message and return immediately without blocking until an acknowledgement has been received from the JMS server • Allows thread to do other work whilst waiting for acknowledgement • When the acknowledgement is received an asynchronous callback will be invoked • Method on JMSProducer: • Also available in the old API Copyright © 2015 CapTech Ventures, Inc. All rights reserved. Asynchronous Send producer.send(message, completionListener)
  26. 26. Asynchronous Send Listener Copyright © 2015 CapTech Ventures, Inc. All rights reserved. public interface CompletionListener { void onCompletion(Message message); void onException(Message message, Exception exception); }
  27. 27. • JMS 1.1 defines optional JMS message property JMSXDeliveryCount • Number of times message has been delivered • JMS 2 makes this mandatory • Allows applications (and the JMS provider) to handle poison messages better • Not a replacement to dead letter queues, etc Copyright © 2015 CapTech Ventures, Inc. All rights reserved. Better Handling of Poison Messages JMSXDeliveryCount mandatory
  28. 28. • Allows scalable consumption of messages from a topic subscription • multiple threads, multiple JVMs • New methods needed for non-durable subscriptions: • Existing methods used for durable subscriptions: • Also available in old API Copyright © 2015 CapTech Ventures, Inc. All rights reserved. Multiple Consumers on a Topic Subscription JMSConsumer consumer = context.createSharedConsumer(topic, sharedSubscriptionName); JMSConsumer consumer = context.createDurableConsumer(topic, durableSubscriptionName);
  29. 29. Java EE Alignment Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  30. 30. • Ships preconfigured with the runtime • Useful for majority of cases Copyright © 2015 CapTech Ventures, Inc. All rights reserved. Platform Default Connection Factory @Resource( lookup="java:comp/defaultJMSConnectionFactory") ConnectionFactory myJMScf;
  31. 31. Defining JMS Resources Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @JMSConnectionFactoryDefinition( name = "java:global/jms/demoConnectionFactory", interfaceName = "javax.jms.ConnectionFactory", description = "ConnectionFactory to use in demonstration") @JMSDestinationDefinition( name = "java:global/jms/demoQueue", description = "Queue to use in demonstration", interfaceName = "javax.jms.Queue", destinationName = "demoQueue")
  32. 32. Supplanted/Overridden in XML… Copyright © 2015 CapTech Ventures, Inc. All rights reserved. <jms-destination> <name>java:global/jms/demoQueue</name> <interface-name>javax.jms.Queue</interface-name> <resource-adapter-name>jmsra</resource-adapter-name> <destination-name>demoQueue</destination-name> </jms-destination> <jms-connection-factory> <name>java:global/jms/demoConnectionFactory</name> <property> <name>addressList</name> <value>mq://localhost:7676</value> </property> <max-pool-size>30</max-pool-size> <min-pool-size>20</min-pool-size> <max-idle-time>5</max-idle-time> </jms-connection-factory>
  33. 33. EJB3/MDB Alignment Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  34. 34. Better Standard Configuration for MDBs • Configuration of MDBs surprisingly non-standard in Java EE 6 • JNDI name of queue or topic • JNDI name of connection factory • Client ID • Durable subscription name Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  35. 35. Specifying Queue or Topic Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @MessageDriven(activationConfig = { @ActivationConfigProperty( propertyName = "destinationLookup", propertyValue = "jms/myTopic"), ... })
  36. 36. Specifying Connection Factory Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @MessageDriven(activationConfig = { @ActivationConfigProperty( propertyName = "connectionFactoryLookup", propertyValue = "jms/myCF"), ... })
  37. 37. Specifying Durable Subscriptions Copyright © 2015 CapTech Ventures, Inc. All rights reserved. @MessageDriven(activationConfig = { @ActivationConfigProperty( propertyName = "subscriptionDurability", propertyValue = "Durable"), @ActivationConfigProperty( propertyName = "clientId", propertyValue = "myClientID"), @ActivationConfigProperty( propertyName = "subscriptionName", propertyValue = "MySub"), ... })
  38. 38. JMS 2 Implementations Copyright © 2015 CapTech Ventures, Inc. All rights reserved. GlassFish
  39. 39. JMS 2.1 • Declarative JMS Listeners • https://java.net/jira/browse/JMS_SPEC-134 • https://java.net/jira/browse/JMS_SPEC-154 • Cloud/Multitenancy • https://java.net/jira/browse/JMS_SPEC-57 • Java SE 8 Alignment • https://java.net/jira/browse/JMS_SPEC-151 • Java SE Bootstrap • https://java.net/jira/browse/JMS_SPEC-89 • Management/Monitoring • https://java.net/jira/browse/JMS_SPEC-18 Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  40. 40. Java EE Guardians http://javaee-guardians.io @javaee_guardian Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  41. 41. Summary • Long awaited and well received • API modernization • Some new features • Java EE/MDB alignment • Move JMS 2.1 forward! Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  42. 42. Resources • Java EE/JMS Tutorials • http://docs.oracle.com/javaee/7/tutorial/partmessaging.htm • JMS 2 Hands-on-Lab • https://github.com/m-reza-rahman/jms2-lab • JMS Transparent Expert Group • http://jms-spec.java.net Copyright © 2015 CapTech Ventures, Inc. All rights reserved.
  43. 43. Copyright©2015CapTechVentures,Inc.Allrightsreserved.

×