1 / 27

JMS

JMS. Eric F. Gerlofsma eric.gerlofsma@hu.nl. Java Message Service (JMS). JMS Client. JMS Client. JMS Provider =JBOSS. JMS Client. JMS Client. JNDI provides the access ports. /ConnectionFactory: org.jboss.mq.SpyConnectionFactory /XAConnectionFactory: org.jboss.mq.SpyXAConnectionFactory

genera
Download Presentation

JMS

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 Eric F. Gerlofsma eric.gerlofsma@hu.nl

  2. Java Message Service (JMS) JMS Client JMS Client JMS Provider =JBOSS JMS Client JMS Client

  3. JNDI provides the access ports • /ConnectionFactory: org.jboss.mq.SpyConnectionFactory • /XAConnectionFactory: org.jboss.mq.SpyXAConnectionFactory • /queue: org.jnp.interfaces.NamingContext • /queue/A: org.jboss.mq.SpyQueue • /queue/testQueue: org.jboss.mq.SpyQueue • /queue/ex: org.jboss.mq.SpyQueue • /queue/DLQ: org.jboss.mq.SpyQueue • /queue/D: org.jboss.mq.SpyQueue • /queue/C: org.jboss.mq.SpyQueue • /queue/B: org.jboss.mq.SpyQueue • /topic: org.jnp.interfaces.NamingContext • /topic/testDurableTopic: org.jboss.mq.SpyTopic • /topic/testTopic: org.jboss.mq.SpyTopic • /topic/securedTopic: org.jboss.mq.SpyTopic Note: replace in: C:\jboss-5.1.0.GA\server\default\deploy\messaging the file: destinations-service.xml with: example-destinations-service.xml from: C:\jboss-5.1.0.GA\docs\examples\jms

  4. Point-to Point (P2P) send JMS Client receive JMS Provider JBOSS JMS Client

  5. JBoss queue/A Receiver Messages Sender

  6. DEMO P2P(Receiver1) public class Main { public static void main(String[] args) throws Exception { for (int i=0; i<5; i++) { Receiver receiver = new Receiver(i); Thread thread = new Thread(receiver); thread.start(); } } }

  7. DEMO P2P(Receiver2) Import . . . public class Receiver implements Runnable { public void run() { try { Properties env = new Properties(); String fac = "org.jnp.interfaces.NamingContextFactory"; String url = "jnp://localhost:1099"; env.setProperty(Context.INITIAL_CONTEXT_FACTORY, fac); env.setProperty(Context.PROVIDER_URL, url); Context context = new InitialContext(env); String cfac = "ConnectionFactory"; QueueConnectionFactory qcf = (QueueConnectionFactory)context.lookup(cfac); QueueConnection qc = qcf.createQueueConnection(); QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); String qA = "queue/A"; Queue q = (Queue)context.lookup(qA); QueueReceiver qreceiver = qs.createReceiver(q); qc.start(); TextMessage message = (TextMessage)qreceiver.receive(); System.out.println(message.getText()); qreceiver.close(); qs.close(); qc.close(); } catch (Exception e) { System.err.println(e.getMessage()); } } } create: 1. factory 2. connection 3. session 4. Queue 5. receiver 6. message

  8. DEMO P2P(Sender) Import . . . public class Main { public static void main(String[] args) throws Exception { Properties env = new Properties(); String fac = "org.jnp.interfaces.NamingContextFactory"; String url = "jnp://localhost:1099"; env.setProperty(Context.INITIAL_CONTEXT_FACTORY, fac); env.setProperty(Context.PROVIDER_URL, url); Context context = new InitialContext(env); String cfac = "ConnectionFactory"; QueueConnectionFactory qcf = (QueueConnectionFactory)context.lookup(cfac); QueueConnection qc = qcf.createQueueConnection(); QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); String qA = "queue/A"; Queue q = (Queue)context.lookup(qA); QueueSender qsender = qs.createSender(q); TextMessage tm = qs.createTextMessage(); tm.setText("Hello World! "); qsender.send(tm); System.out.println("Message send"); qsender.close(); qs.close(); qc.close(); } } create: 1. factory 2. connection 3. session 4. Queue 5. sender 6. message

  9. Publish-Subscribe (Pub-Sub) 5. receive 4. publish JMS Client JMS Client 1. subscribe 6. receive 5. receive JMS Provider JBOSS 3. subscribe 2. subscribe JMS Client JMS Client

  10. JBoss Topic/testTopic

  11. DEMO Pub-Sub (Subscriber1) import . . . public class MyFrame extends Frame implements WindowListener { public static String ncf = "org.jnp.interfaces.NamingContextFactory"; public static String url = "jnp://localhost:1099"; public static String cf = "ConnectionFactory"; public static String tt = "topic/testTopic"; public static void main(String[] arg) { for (int i=0; i<3; i++) new Thread() { public void run() { new MyFrame(); }}.start(); } private TextArea textArea = new TextArea(16, 64); public MyFrame() { super("subscriber nr. "+System.currentTimeMillis()%1000); setResizable(false); addWindowListener(this); add(textArea); pack(); setVisible(true); try { listen(); } catch (Exception e) { setText(""+e); } } . . .

  12. DEMO Pub-Sub (Subscriber2) private void listen()throws Exception { Properties env = new Properties(); env.setProperty(Context.INITIAL_CONTEXT_FACTORY, ncf); env.setProperty(Context.PROVIDER_URL, url); Context context = new InitialContext(env); TopicConnectionFactory tcf = (TopicConnectionFactory)context.lookup(cf); Topic t = (Topic)context.lookup(tt); TopicConnection tc = tcf.createTopicConnection(); TopicSession ts = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicSubscriber tsub = ts.createSubscriber(t); tc.start(); setText("waiting for message"); while (true) { TextMessage tm = (TextMessage)tsub.receive(); setText("receive="+tm.getText()); } } public void setText(String s) { textArea.append(s+"\n"); } public void windowActivated (WindowEvent evt){} public void windowClosed (WindowEvent evt){} public void windowClosing (WindowEvent evt){ dispose();} public void windowDeactivated(WindowEvent evt){} public void windowDeiconified(WindowEvent evt){} public void windowIconified (WindowEvent evt){} public void windowOpened (WindowEvent evt){} }

  13. DEMO Pub-Sub (Publisher) import . . . public class Main { public static String ncf = "org.jnp.interfaces.NamingContextFactory"; public static String url = "jnp://localhost:1099"; public static String cf = "ConnectionFactory"; public static String tt = "topic/testTopic"; public static void main(String[] args) throws Exception { Properties env = new Properties(); env.setProperty(Context.INITIAL_CONTEXT_FACTORY, ncf); env.setProperty(Context.PROVIDER_URL, url); Context context = new InitialContext(env); TopicConnectionFactory tcf = (TopicConnectionFactory)context.lookup(cf); Topic t = (Topic)context.lookup(tt); for (int i=0; i<10; i++) { TopicConnection tc = tcf.createTopicConnection(); TopicSession ts = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicPublisher tpub = ts.createPublisher(t); TextMessage tm = ts.createTextMessage(); tm.setText(i+" Hello World!"); System.out.println(tm); tpub.publish(t, tm); System.out.println("published"); tpub.close(); ts.close(); tc.close(); } } } create: 1. factory 2. connection 3. session 4. topic 5. publisher 6. message

  14. Overview P2P ConnectionFactory /QueueConnectionFactory creates Connection /QueueConnection creates MessageConsumer /QueueReceiver creates Session /QueueSession creates MessageProducer /QueueSender creates Message Destination /Queue /TemporaryQueue receives message from queue sends message to queue

  15. Overview Pub/Sub ConnectionFactory /TopicConnectionFactory creates Connection /topicConnection creates MessageConsumer /TopicSubscriber creates Session /TopicSession creates MessageProducer /TopicPublisher creates Message Destination /Topic /TemporaryTopic receives message from topic Sends message to topic

  16. ConnectionFactory javax.jms.ConnectionFactory javax.jms.QueueConnectionFactory QueueConnection createQueueConnection() QueueConnection createQueueConnection(String userName, String password) javax.jms.TopicConnectionFactory TopicConnection createTopicConnection() TopicConnection createTopicConnection(String userName, String password)

  17. Connection (a Factory for Sessions) javax.jms.Connection javax.jms.ConnectionMetaData void close() Session createSession(..) String getClientID() ExceptionListener getExceptionListener() ConnectionMetaData getMetaData() void setClientID(...) void setExceptionListener(...) void start() void stop() int getJMSMajorVersion() int getJMSMinorVersion() String getJMSProviderName() String getJMSVersion() Enumeration getJMSPropertyNames() int getProviderMajorVersion() int getProviderMinorVersion() String getProviderVersion() javax.jms.ExceptionListener void onException(JMSException e) javax.jms.QueueConnection javax.jms.TopicConnection javax.jms.TopicConnection QueueSession createQueueSession(...) TopicSession createTopicSession(...) TopicSession createTopicSession(...) start() Stopped Started create() stop() can send messages can receive messages Closed close() close()

  18. Session (a Factory for Messages, Topics, P2P and PubSub) javax.jms.Session void close() void commit() int getAcknowledgeMode() MessageListener getMessageListener() boolean getTransacted() void recover() void rollback() void run() void setMessageListener(MessageListener listener) void unsubscribe(String name) Message createMessage() Topic createTopic(String topicName) ... create...(...) BytesMessage Consumer MapMessage ObjectMessage Producer StreamMessage TextMessage P2P PubSub javax.jms.QueueSession javax.jms.TopicSession QueueBrowser createBrowser(...) QueueReceiver createReceiver(...) QueueSender createSender(Queue queue) TemporaryQueue createTemporaryQueue() TopicPublisher createPublisher(Topic topic) TopicSubscriber createSubscriber(...) TemporaryTopic createTemporaryTopic() messageSelector

  19. MessageProducer deliveryMode destination (get only) priority timeToLive dissableMessageID disableMessageTimeStamp javax.jms.MessageProducer Properties void close() void send(Message m) void send(Message m, int deliveryMode, int priority, long timeToLive) void send(Destination d Message m) void send(Destination d Message m, int deliveryMode, int priority, long timeToLive) P2P javax.jms.QueueSender void send(Queue q, Message m) void send(Queue q, Message m, int deliveryMode, int priority, long timeToLive) Queue getQueue() PubSub javax.jms.TopicPublisher void publish(Message m) void publish(Message m, int deliveryMode, int priority, long timeToLive) void publish(Topic t, Message m) void publish(Topic t, Message m, int deliveryMode, int priority, long timeToLive) Topic getTopic()

  20. Message Producer Properties deliveryMode = DeliveryMode.PERSISTENT (default) DeliveryMode.NON_PERSISTENT priority = 0 (low) – 4(default) - 9(high) 0-4 normal priorities 5-9 expedited priorities timeToLive = 0 (default) unlimited The time in milliseconds for which the message provider will retain the message after the producer sends it.

  21. MessageConsumer javax.jms.MessageConsumer void close() MessageListener getMessageListener() Message receive() Message receive(long timeout) Message receiveNoWait() void setMessageListener(MessageListener listener) P2P PubSub javax.jms.QueueReceiver javax.jms.TopicSubscriber Queue getQueue() boolean getNoLocal() Topic getTopic()

  22. Destination javax.jms.Destination P2P PubSub javax.jms.Topic javax.jms.Queue String getTopicName() String toString() String getQueueName() String toString() javax.jms.TemporaryQueue javax.jms.TemporaryTopic void delete() void delete()

  23. Message Structure Header JMSDestination (set by send or publish) JMSDeliveryMode (set by send or publish) JMSPriority (set by send or publish) JMSExpiration (set by send or publish) JMSMessageID (set by send or publish) JMSTimeStamp (set by send or publish) JMSCorrelationID (set by Client) JMSReplyTo (set by Client) JMSType (set by Client) JMSRedelivered (set by provider) Standard Properties JMSXUserID (set by provider) JMSXAppID (set by provider) JMSXDeliveryCount (set by provider) JMSXGroupID (set by Client) JMSXGroupSeq (set by Client) JMSXProducerTXID (set by provider) JMSXConsumerTXID (set by provider) JMSXRcvTimestamp (set by provider) Application Properties Provider Properties Message Body (one of ...) TextMessage: String MapMessage: Set of name-value pairs BytesMessage: Stream of bytes StreamMessage: Stream of primitive values ObjectMessage: Serializable Object

  24. Message BooleanProperty ByteProperty DoubleProperty FloatProperty IntProperty LongProperty ObjectProperty ShortProperty StringProperty JMSCorrelationProperty JMSCorrelationIDAsBytes JMSDeliveryMode JMSDestination JMSExpiration JMSMessageID JMSPriority JMSDelivered JMSReplyTo JMSTimeStamp JMSType javax.jms.Message Properties void acknowledge() void clearBody() void clearProperties() ... get...Property(String name) ... getJMS...() Enumeration getPropertyNames() boolean propertyExists(String name) void set..Property(...) void setJMS...(...) BytesMessage MapMessage ObjectMessage StreamMessage TextMessage

  25. Message Listener javax.jms.MessageListener void onMessage(Message m)

  26. Message Selector • Message selectors are specified as strings during the creation of message consumers. • These strings are used for filtering messages. • They conform to the standard NSI SQL-92 syntax for predicates. • They’re evaluated using message headers and properties. A Selector can contain: Literals Identifiers White spaces Expressions Standard bracketing Arithmetic operators Comparison operators

  27. Request/Response Paradigm

More Related