Java Message Service JMS CS 595 Web Services
Java Message Service (JMS) CS 595 Web Services Aysu Betin-Can
What is JMS? w A specification that describes a common way for Java programs to create, send, receive and read distributed enterprise messages w loosely coupled communication w Asynchronous messaging w Reliable delivery n A message is guaranteed to be delivered once and only once. w Outside the specification n n Security services Management services
A JMS Application w JMS Clients n Java programs that send/receive messages w Messages w Administered Objects n n preconfigured JMS objects created by an admin for the use of clients Connection. Factory, Destination (queue or topic) w JMS Provider n messaging system that implements JMS and administrative functionality
JMS Administration Administrative Tool Bind JNDI Namespace Lookup JMS Client Logical Connection JMS Provider
JMS Messaging Domains w Point-to-Point (PTP) n n built around the concept of message queues each message has only one consumer w Publish-Subscribe systems n n uses a “topic” to send and receive messages each message has multiple consumers
Point-to-Point Messaging Msg Client 1 sends consumes Client 2 Queue acknowledges
Publish/Subscribe Messaging subscribes Client 2 Msg Client 1 delivers publishes Topic subscribes Client 3 delivers
Message Consumptions w Synchronously n n A subscriber or a receiver explicitly fetches the message from the destination by calling the receive method. The receive method can block until a message arrives or can time out if a message does not arrive within a specified time limit. w Asynchronously n n A client can register a message listener with a consumer. Whenever a message arrives at the destination, the JMS provider delivers the message by calling the listener's on. Message() method.
JMS API Programming Model Connection Factory creates Connection Message Producer creates sends to Destination Session creates Msg Message Consumer receives from Destination
JMS Client Example w Setting up a connection and creating a session Initial. Context jndi. Context=new Initial. Context(); //look up for the connection factory Connection. Factory cf=jndi. Context. lookup(connectionfactoryname); //create a connection Connection connection=cf. create. Connection(); //create a session Session session=connection. create. Session(false, Session. AUTO_ACKNOW LEDGE); //create a destination object Destination dest 1=(Queue) jndi. Context. lookup(“/jms/my. Queue”); //for Point. To. Point
Producer Sample w Setup connection and create a session w Creating producer Message. Producer producer=session. create. Producer(dest 1); w Send a message Message m=session. create. Text. Message(); m. set. Text(“just another message”); producer. send(m); w Closing the connection. close();
Consumer Sample (Synchronous) w Setup connection and create a session w Creating consumer Message. Consumer consumer=session. create. Consumer(dest 1); w Start receiving messages connection. start(); Message m=consumer. receive();
Consumer Sample (Asynchronous) w Setup the connection, create a session w Create consumer w Registering the listener n n Message. Listener listener=new my. Listener(); consumer. set. Message. Listener(listener); w my. Listener should have on. Message() public void on. Message(Message msg){ //read the massage and do computation }
Listener Example public void on. Message(Message message) { Text. Message msg = null; try { if (message instanceof Text. Message) { msg = (Text. Message) message; System. out. println("Reading message: " + msg. get. Text()); } else { System. out. println("Message of wrong type: " + message. get. Class(). get. Name()); } } catch (JMSException e) { System. out. println("JMSException in on. Message(): " + e. to. String()); } catch (Throwable t) { System. out. println("Exception in on. Message(): " + t. get. Message()); } }
JMS Messages w Message Header n n n used for identifying and routing messages contains vendor-specified values, but could also contain application-specific data typically name/value pairs w Message Properties (optional) w Message Body(optional) n n contains the data five different message body types in the JMS specification
JMS Message Types Message Type Contains Some Methods Text. Message String get. Text, set. Text Map. Message set of name/value pairs set. String, set. Double, set. Lo ng, get. Double, get. String Bytes. Message stream of uninterpreted bytes write. Bytes, read. Bytes Stream. Message stream of primitive values write. String, write. Double, write. Long, read. String Object. Message serialize object set. Object, get. Object
More JMS Features w Durable subscription n n by default a subscriber gets only messages published on a topic while a subscriber is alive durable subscription retains messages until a they are received by a subscriber or expire w Request/Reply n by creating temporary queues and topics Session. create. Temporary. Queue() producer=session. create. Producer(msg. get. JMSReply. To()); reply= session. create. Text. Message(“reply”); reply. set. JMSCorrelation. ID(msg. get. JMSMessage. ID); producer. send(reply); l n
More JMS Features w Transacted sessions n n n session=connection. create. Session(true, 0) combination of queue and topic operation in one transaction is allowed void on. Message(Message m) { try { Message m 2=process. Order(m); publisher. publish(m 2); session. commit(); } catch(Exception e) { session. rollback(); }
More JMS Features w Persistent/nonpersistent delivery n n producer. set. Delivery. Method(Delivery. Mode. NON_PERSISTEN T); producer. send(mesg, Delivery. Mode. NON_PERSISTENT , 3, 1000); w Message selectors n SQL-like syntax for accessing header: subscriber = session. create. Subscriber(topic, “priority > 6 AND type = ‘alert’ ”); n n Point to point: selector determines single recipient Pub-sub: acts as filter
JMS API in a J 2 EE Application w Since the J 2 EE 1. 3 , the JMS API has been an integral part of the platform w J 2 EE components can use the JMS API to send messages that can be consumed asynchronously by a specialized Enterprise Java Bean n message-driven bean
Enterprise Java Beans w EJB is a server-side component that encapsulates the business logic of an application w EJB simplifies the development of large, distributed applications n EJB Container provides system-level services l n Beans have the control logic l n e. g. transaction management, authorization thin client applications Portable components l can run on any compliant J 2 EE server
Message–Driven Bean w acts as a listener for the JMS, processing messages asynchronously w specialized adaptation of the JMS API used in the context of J 2 EE applications
JMS with EJB Example
MDB Example public class MB implements Message. Driven. Bean, Message. Listener{ public void ejb. Create(){} public void ejb. Remove(){} public void set. Message. Driven. Context(Message. Driven. Context mdc){} pubic void on. Message(Message m){ //do computation on the incoming message try{ if (m instanceof Text. Message) System. out. println(“MBean: message”+m. get. Text());
JMS and JNDI w JMS does not define a standard address syntax by which clients communicate with each other w Instead JMS utilizes Java Naming & Directory Interface(JNDI). w Using JNDI provides the following advantages: n n n It hides provider-specific details from JMS clients. It abstracts JMS administrative information into Java objects that are easily organized and administrated from a common management console. Since there will be JNDI providers for all popular naming services, this means JMS providers can deliver one implementation of administered objects that will run everywhere. Thereby eliminating deployment and configuration issues.
SOAP and JMS w Use JMS as a transportation layer for SOAP w Example: Sun™ ONE Message Queue n n n enables to send JMS messages that contain a SOAP payload allowing transportation of SOAP messages reliably and publishing SOAP messages to JMS subscribers http: //docs. sun. com/source/817 -0355 -10/SOAP. html
SOAP and JMS (using Sun™ ONE MQ) w Send a SOAP message n n n Create a JMS session Create a SOAP message Transfer the SOAP message into JMS message Message my. Msg= Message. Transformer. SOAPMessage. Into. JMSMe ssage (SOAPMessage, Session); n Send the JMS message
SOAP and JMS (using Sun™ ONE MQ) w Receive a SOAP message n n n Create a JMS session Receive the JMS message Transfer the JMS message into SOAP message SOAPMessage my. Msg= Message. Transformer. SOAPMessage. From. JMSM essage (Message, Message. Factory);
SOAP and JMS (using Sun™ ONE MQ) Deferring SOAP Processing
Publishing a SOAP message
JMS Providers w Sun. ONE Message Queue (SUN) n n a JMS provider integrated with the Sun. ONE Application Server http: //www. sun. com w MQ JMS (IBM) n n n MQSeries is another messaging technology can configure MQ as a JMS provider (http: //www 7 b. software. ibm. com/wsdd/library/techtip/01 12_cox. html)
JMS Providers w Web. Logic JMS (BEA) n n enterprise-class messaging system integrated into Web. Logic Server http: //dev 2 dev. bea. com/technologies/jms/index. jsp w JMSCourier (Codemesh) n n merging C++ applications into a JMS environment http: //www. codemesh. com/en/Align. Technology. Case. Stu dy. html
More JMS Vendors w Fiorano Software http: //www. fiorano. com w JRUN Server http: //www. allaire. com w Gem. Stone http: //www. gemstone. com w Nirvana http: //www. pcbsys. com w Oracle http: //www. oracle. com w A more exhaustive listing is available at n http: //java. sun. com/products/jms/vendors. html
- Slides: 34