1 / 34

JMS - Java Message Service K Sreenivasa Rao

JMS - Java Message Service K Sreenivasa Rao. Agenda. What is JMS and MOM? JMS Models What is Point to Point Model with Architecture What is Publisher to Subscriber with Architecture JMS API Sample Program For Point to Point Model

neviah
Download Presentation

JMS - Java Message Service K Sreenivasa Rao

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JMS - Java Message Service K Sreenivasa Rao

  2. Agenda • What is JMS and MOM? • JMS Models • What is Point to Point Model with Architecture • What is Publisher to Subscriber with Architecture • JMS API • Sample Program For Point to Point Model • Configuration of Queue and Topic in the Application server • Real Time Technical flow of architecture using JMS technology

  3. What is JMS and MOM JMS • It provides the standards abstraction for java applications to communicate the messages by using asynchronous mode. JMS act as a message oriented middleware. MOM (Message Oriented Middleware) • How two systems in networks can exchange the message in asynchronous mode. • Here client is not waiting until processing the request in the server. • By using middleware server we can easily communicating the messages in networks. • With less resource we can process number of clients request by using middleware. • Performance, Availability and responsiveness are the advantages of MOM.

  4. What is JMS and MOM • In order to communicating messages in networks between two systems we need to design one special middleware service that can listen messages, store them and send to connected consumers. • So that some venders have to be introduced middleware services to providing the services between two systems and they provide its own API and communicating its own protocol for java applications. • If our java application using these vender middleware services then our application is vender dependent. • To avoid this problem sun has introduced java message service (JMS) to communicating request messages in asynchronous mode. • Asynchronous message means: • If our client request is not depending on next set of operations on the response of the request made such request can be Asynchronous message. • If our client is depending on next set of operations on the response of the request made such request is not asynchronous message it is a synchronous message.

  5. What is JMS and MOM Example: The below diagrams for synchronous and asynchronous messages. Synchronous mode: Client Side Program Server Side Program rr Request msg Response msg Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7

  6. Architecture of Point to Point Model: What is JMS and MOM Asynchronous mode: Client Side Program Server Side Program Request msg ACK Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7

  7. JMS Models Jms Provides two type of Models: • Point to Point (one to one relationship): - This model supports the Jms Queue Technique. • Publishers to Subscriber ( one to many relationships ) : - This model supports the Jms Topic Technique

  8. What is Point to Point Model with Architecture Point to Point: • When sender sends the message to only one receiver not for all the receivers and the receiver receives the message and process the request only one time, so it is a one to one relationship. Queue: • The Queue responsibility is establishing the logical connection between sender and receiver. It can maintain all the senders request messages in the form of queue and sent to connected receivers for processing the request message. • After receiving the senders request messages, the queues are listen all the messages and respective queues are send the acknowledgements to all the sends. • It has sent one request message to one receiver, but not sent one message to all the connected receivers. • One queue has connected too many receivers.

  9. What is Point to Point Model with Architecture Sender: • The responsibility of sender is sends the message to connected Queue. • One sender can connect to only one queue not more than one queue. • The sender sends the message to respective connected queue only once, but not sent same message repeatedly. Receiver: • The responsibility of receiver, receives the message from connected Queue. • Process all the request messages. • One receiver can’t connect more then one Queue.

  10. What is Point to Point Model with Architecture Architecture of Point to Point Model: Sender Middleware Server Receiver connect Req msg send msg ack ack Req msg Req msg Req msg R1 S1 Q1 R2 S2 Q2 R3 S3 Q3 S4 R4

  11. What is Publisher to Subscriber with Architecture • Once Publisher publish the message, this message should be sent to all subscribers which is connected to Topic. Publisher: • The responsibility of publisher is publish the message to topic. • Publisher connected to only one topic and send message only once. • More than one publisher will be connected to same topic. Topic: • The responsibility of topic is, establish the connection between Publishers and Subscribers for communicating message request. • The topic also listen all the messages and sent to all subscribers which is connected to topic.

  12. What is Publisher to Subscriber with Architecture • The topic maintains all Durable subscriber details but not non durable subscribers. If the subscriber receives online and offline messages then that subscriber called as Durable Subscriber. If the subscriber receives only online messages then that subscriber called as non-durable subscriber. • Topic stores the durable subscriber’s names and ids for communicating the online and offline messages. Subscribers: • The responsibility of Subscribers is, connected to any specific topic, get the request message and process the request. • One subscriber will be connected to only one topic but not more than one. • Here two types of subscribers: - Durable Subscribers - Non-Durable Subscribers

  13. What is Publisher to Subscriber with Architecture Architecture of Publishers to Subscribers: Publishers Middleware Server Subscribers connect msg ack send msg ack msg msg msg T1 T2 T3 S1 Durable P1 S1 S2 S2 Durable P2 S3 S3 Durable P3 S4 Non Durable P4

  14. JMS API • Jms provides api for communicating java applications and it provides interfaces and classes. All these interfaces and classes are available in Javax.jms package. • Get the Queue and Topic Connection factory by using following interfaces QueueConnectionFactory and TopicConnectionfactory. • Get the Queue and Topic connections by using following interfaces QueueConnection and TopicConnection. • Get the Queue and Topic Sessions by using following interfaces QueueSession and TopicSession. • Get the JNDI Queue and Topic names by using Queue and Topic interfaces.

  15. JMS API Code implementation of Point to Point model: Sender: • Get the queue connection factory object QueueConnectionFactory qcf = (QueueConnectionFactory)ic.lookup(Javax.jms. QueueConnectionFactory); • Get the queue connection from queue connection factory object QueueConnection qc = qcf.createQueueConnection(); • Get Queue Session from queue connection object  QueueSession qs = qc.createQueueSession(Session.AUTO_ACK, false); Session.AUTO_ACK means acknowledgement and true/false means weather your session is transactional or not. • Get the JNDI queue name By using Queue interface Queue q = (Queue)ic.lookup(“Queue JNDI Name”);

  16. JMS API • Create the Queue sender object by providing queue QueueSender qSender = qs.createSender(q); • Start the queue connection qc.start(); • Create the text message for Processing request (Jms supports Text message, Object message, Byte Message, Map Message) TextMessage tx = qs.createTextMessage(); tx.setText(“some message/operation”); • Now send the message to Queue.. qsender.send(tx); Receiver: • Up to 4th step same as above sender section. • Create a Queue receiver object by providing queue QueueReceiver qreceiver = qs.createReceiver(q);

  17. JMS API • Now receiver listen the message from the queue. qreceiver.setMessageListener( new MyListener()); • Start the queue connection qc.start(); Message Listener Class • The purpose of message listener class, listen and process the message. • The message listener class must implement MessageListener interface. • Override onMessage() method, and implement the logic for processing request inside this method.

  18. JMS API Code implementation of publisher to Subscriber Publisher: • Get the topic connection factory object by using lookup method TopicConnectionFactory tcf = (TopicConnectionFactory).ic.lookup(“Javax.jms. TopicConnectionFactory”); • Get the topic connection object by using topic connection factory object TopicConnection tc = tcf.createTopicConnection(); • Get the topic session object by using topic connection object TopicSession ts = tc.createTopicSession(Session.AUTO_ACK, false); • Get the JNDI topic name by using Topic Topic topic = (Topic)ic.lookup(“JNDI Topic Name”);

  19. JMS API • Create the publisher object by providing topic as an argument TopicPublisher tp = ts.createpublisher(topic); • Now start the topic connection tc.start(); • Create the message to publish all the subscribers by using topic session TextMessage tx = ts.createTextMessage(); tx.setText(“test message”); • Now publish the message to server by using topic publisher object tp.publish(tx); Non Durable Subscriber: • Up to 4th step same as above publisher section. • Create the non durable subscriber object by providing topic as an argument. TopicSubscriber tnds= ts.createSubscriber(topic);

  20. JMS API • Now listens the message from the topic. tnds.setMessageListener( new MyListener()); • Now start the topic connection tc.start(); Durable Subscriber: • Up to 3rd step same as above publisher section • Set the durable subscriber Id Tc.setClientId(“S[0]”); • Get the JNDI topic Topic t = (Topic)ic.lookup(“JNDI Topic Name”); • Create the durable subscriber object by using topic session object TopicDurableSubscriber tds = ts.createDurableSubscriber(t, sName[0]); • Now listens the message from the topic tds.setMessageListener( new MyListener()); • Start the topic connection tc.start();

  21. Sample Program For Point to Point Model QueueHome.jsp <ps:form method="post" action="/Queue/Queues/" > <html:hidden property="action" /> <table> <tr><td><h1 >Sample JMS Queue Program</h1></td></tr> <tr> <td><b>Sender Text Message :</b><td><html:text name = "queueForm" property = "senderMessage"/></td></td></tr> <tr><td><html:button styleId="sender" value="sender" onclick="return doSubmit('sender');" property="button" styleClass="button134" /></td> <td><html:button styleId="receiver" value="receiver" onclick="return doSubmit('receiver');" property="button" styleClass="button134" /></td></tr> </table> </ps:form>

  22. Sample Program For Point to Point Model QueueAction.java public ActionForward doSender(ActionMapping mapping, AutoActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, SystemException, ApplicationException { if(logger.isDebugEnabled()) { logger.debug("Entry -> doSave" ); } QueueForm queueForm = (QueueForm)actionForm; if(queueForm.getSenderMessage() != null ){ QueueSerivceDelegate queueeServiceDelegate = QueueServiceDelegate.getInstance(); queueeServiceDelegate.senderRequest(queueForm.getSenderMessage()); return mapping.findForward("sender"); }else{ return mapping.findForward("queueHome"); } }

  23. Sample Program For Point to Point Model public ActionForward doReceiver(ActionMapping mapping, AutoActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, SystemException, ApplicationException { if(logger.isDebugEnabled()) { logger.debug("Entry -> doReciever" ); } QueueForm queueForm = (QueueForm)actionForm; if(StringUtils.isBlank(queueForm.getSenderMessage())){ QueueServiceDelegate queueServiceDelegate =QueueServiceDelegate.getInstance(); queueServiceDelegate.receiverProcess(); } return mapping.findForward("queueHome"); } }

  24. Sample Program For Point to Point Model QueueServiceDelegate.java public class QueueServiceDelegate extends AbstractServiceDelegate { public void senderRequest(String senderMessage) throws SystemException{ if(senderMessage != null){ QueueService queueServices = (QueueService)getService(); try{ queueServices.senderRequest(senderMessage); }catch(RemoteException e){ } } } public void receiverProcess() throws SystemException{ EmployeeService employeeServices = (EmployeeService)getService(); try{ employeeServices.receiverProcess(); }catch(RemoteException e){ } }}

  25. Sample Program For Point to Point Model QueueServiceBean.java public class EmployeeServiceBean implements SessionBean { public void senderRequest(String sendMessage) throws RemoteException, SystemException{ QueueConnection queueConnection = null; InitialContext ic = getInstance(); try { QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ic.lookup("jms/queueConnectionFactory"); queueConnection = queueConnectionFactory.createQueueConnection(); QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); javax.jms.Queue queue = (javax.jms.Queue)ic.lookup("jms/inQueue"); QueueSender queueSender = queueSession.createSender(queue); queueConnection.start(); TextMessage textMessage = queueSession.createTextMessage();

  26. Sample Program For Point to Point Model if(sendMessage != null){ textMessage.setText(sendMessage); }queueSender.send(textMessage); } catch (JMSException jmsException) { getSessionContext().setRollbackOnly(); throw ExceptionHandlerUtility.wrap(jmsException); } catch (NamingException namingException) { getSessionContext().setRollbackOnly(); throw ExceptionHandlerUtility.wrap(namingException); }}

  27. Sample Program For Point to Point Model public void receiverProcess() throws RemoteException, SystemException{ QueueConnection queueConnection = null; InitialContext ic = getInstance(); try { QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ic.lookup("jms/queueConnectionFactory"); queueConnection = queueConnectionFactory.createQueueConnection(); QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); javax.jms.Queue queue = (javax.jms.Queue)ic.lookup("jms/inQueue"); QueueReceiver queueReceiver = queueSession.createReceiver(queue); queueReceiver.setMessageListener(new com.manulife.pension.service.employee.dao.JmsMessageListener()); queueConnection.start(); } catch (JMSException jmsException) { getSessionContext().setRollbackOnly(); throw ExceptionHandlerUtility.wrap(jmsException);}}

  28. Sample Program For Point to Point Model public class JmsMessageListener implements MessageListener{ public void onMessage(Message arg0) { // TODO Auto-generated method stub try{ TextMessage textMessage = (TextMessage)arg0; String message = textMessage.getText(); System.out.println("Sender Message Received-> " + message); }catch(Exception exception){ } } }

  29. Configuration of Queue and Topic in the Application Server Go to application server Console Create the Bus by following steps Service Integration -> Buses -> New -> Enter the bus name and click Next -> Finish and save the configuration. • Click on created bus name -> Configuration -> Destinations -> New -> select Queue -> Enter Queue Attribute name for assign the queue to a bus member(It can be store and process the messages for the queue). • Click on created bus name -> Configuration -> Destinations -> New -> select Topic -> Enter Topic Attribute name for assign the topic to a bus member (It can be store and process the messages for the topic). Create Connection Factory for Queue and Topic. • Resources -> JMS -> Jms Provider -> choose scope (PSWServer) -> select Default messaging provider -> select Queue Connection Factories-> New -> Enter the mandatory fields and choose created Queue bus name -> click Ok and save the changes. • Resources -> JMS -> Jms Provider -> choose scope (PSWServer) -> select Default messaging provider -> select Topic Connection Factories -> Enter the mandatory fields and choose created Topic bus name -> click Ok and save the changes.

  30. Configuration of Queue and Topic in the Application Server • Resources -> JMS -> Jms Provider -> choose scope (PSWServer) -> select Default messaging provider -> select Queue -> New -> Enter Queue Name and JNDI Name -> select Queue name in Connection area-> click Ok and save the changes. • Resources -> JMS -> Jms Provider -> choose scope (PSWServer) -> select Default messaging provider -> select Topic -> New -> Enter Topic Name and JNDI Name -> select Topic name in Connection area-> click Ok and save the changes. • Finally after creating the required Connection factories, queues and topics, you must stop and start the WebSphere Application Server. Check Messages on the Queue • In the Admin console, Service Integration -> Buses -> Click on newly created bus -> Destinations -> Queue Name -> Queue points -> click on Messages This shows a list of the messages that are available on the queue

  31. Configuration of Queue and Topic in the Application Server Check Messages on the Topic • In the Admin console, Service Integration -> Buses -> Click on newly created bus -> Destinations -> Topic Name -> Topic points -> click on Messages This shows a list of the messages that are available on the topic.

  32. Real Time Technical flow of architecture using JMS technology Enterprise Application/ EJB container client Middle ware service EJB Java App Sender Receive MDB DAO DB

  33. Questions

  34. THANKS

More Related