Download presentation
Presentation is loading. Please wait.
Published byLilian Green Modified over 8 years ago
1
Slide No. 1 of 111 JMS ( J AVA M ESSAGE S ERVICE ) -Dhananjay Singh
2
Slide No. 2 of 111 W HAT IS JMS? A specification that describes a common way for Java programs to create, send, receive and read distributed enterprise messages Asynchronous - A JMS provider can deliver messages to a client as they arrive; a client does not have to request messages in order to receive them. -The sender and receiver do not have to be available at the same time in order to communicate. Reliable delivery -A message is guaranteed to be delivered once and only once.
3
Slide No. 3 of 111 JMS JMS Clients –Java programs that send/receive messages Messages –are objects that communicate information between the JMS clients Administered Objects –preconfigured JMS objects created by an admin for the use of clients –ConnectionFactory, Destination (queue or topic) JMS Provider –messaging system that implements JMS and administrative functionality
4
Slide No. 4 of 111 JMS A RCHITECTURE Administrative Tool 1: bind JNDI Namespace JMS client JMS Provider 2: lookup 3: Logical connection 1: Bind destinations and connection factories objects 2. Client looks up for administered objects in the namespace 3. Establish a logical connection to the looked up object thru’ a JMS provider.
5
Slide No. 5 of 111 JMS M ESSAGING D OMAINS Point-to-Point (PTP) : –application is built around the concept of message queues, sender and receivers. –Each message is addressed to a specific queue and the receiving clients extract messages from the queues established to hold their messages. –Each message has only one consumer. –A sender and receiver have no time dependencies. –The receiver acknowledges the successful processing of a message. –Use PTP when every message you send must be processed successfully by one consumer.
6
Slide No. 6 of 111 JMS M ESSAGING D OMAINS Point-to-Point (PTP) : Queue: virtual data channel (“destination”) Producer is a “sender” Consumer is a “receiver”
7
Slide No. 7 of 111 JMS M ESSAGING D OMAINS Publish/Subscribe domain: –Uses a “topic” to send and receive messages –Each message has multiple consumer. –There is a timing dependency between publishers and subscribers, because a client that subscribes to a topic can consume only messages published after the client has created a subscription, and the subscriber must continue to be active in order for it to consume messages.
8
Slide No. 8 of 111 JMS M ESSAGING D OMAINS Publish/Subscribe domain: Topic – “destination” (virtual data channel) Producer is a “publisher” Consumer is a “subscriber”
9
Slide No. 9 of 111 JMS M ESSAGE C ONSUMPTION Synchronous: A subscriber or receiver explicitly fetches the message from the destination by calling the receive method. Asynchronous: A client can register a message listener with a consumer. A message listener is similar to an event listener. Whenever a message arrives at the destination, the JMS provider delivers the message by calling the listener’s onMessage method, which acts on the content.
10
Slide No. 10 of 111 JMS M ESSAGES Message Header : used for identifying and routing messages contains vendor-specified values, but could also contain application-specific data typically name/value pairs Message Properties (optional) Message Body(optional) contains the data five different message body types in the JMS specification
11
Slide No. 11 of 111 JMS M ESSAGE T YPES Message TypeContainsSome Methods TextMessageStringgetText,setText MapMessageset of name/value pairs setString,setDouble,setLong,getDouble,getString BytesMessagestream of uninterpreted bytes writeBytes,readByt es StreamMessagestream of primitive values writeString,writeD ouble,writeLong,re adString ObjectMessageserialize objectsetObject,getObject
12
Slide No. 12 of 111 JMS A PPLICATION A NATOMY A Connection Factory is the object a client uses to create a connection with a provider. A connection factory encapsulates a set of connection configuration parameters that has been defined by an administrator. A Session is a single-threaded context for producing and consuming messages. You use sessions to create message producers, message consumers, and messages.
13
Slide No. 13 of 111 JMS C LIENT E XAMPLE Setting up a connection and creating a session InitialContext jndiContext=new InitialContext(); //look up for the connection factory ConnectionFactory cf=jndiContext.lookup(connectionfactoryname); //create a connection Connection connection=cf.createConnection(); //create a session Session session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE); //create a destination object Destination dest1=(Queue) jndiContext.lookup(“/jms/myQueue”); //for PointToPoint Destination dest2=(Topic)jndiContext.lookup(“/jms/myTopic”); //for publish- subscribe
14
Slide No. 14 of 111 P RODUCER S AMPLE Setup connection and create a session Creating producer MessageProducer producer=session.createProducer(dest1); Send a message Message m=session.createTextMessage(); m.setText(“just another message”); producer.send(m); Closing the connection connection.close();
15
Slide No. 15 of 111 C ONSUMER S AMPLE (S YNCHRONOUS ) Setup connection and create a session Creating consumer MessageConsumer consumer=session.createConsumer(dest1); Start receiving messages connection.start(); Message m=consumer.receive();
16
Slide No. 16 of 111 C ONSUMER S AMPLE (A SYNCHRONOUS ) Setup the connection, create a session Create consumer Registering the listener MessageListener listener=new myListener(); consumer.setMessageListener(listener); myListener should have onMessage() public void onMessage(Message msg){ //read the massage and do computation }
17
Slide No. 17 of 111 L ISTENER E XAMPLE public void onMessage(Message message) { TextMessage msg = null; try { if (message instanceof TextMessage) { msg = (TextMessage) message; System.out.println("Reading message: " + msg.getText()); } else { System.out.println("Message of wrong type: " + message.getClass().getName()); } } catch (JMSException e) { System.out.println("JMSException in onMessage(): " + e.toString()); } catch (Throwable t) { System.out.println("Exception in onMessage():" + t.getMessage()); }
18
Slide No. 18 of 111 You’re all set to use JMS!!!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.