JMS Introduction


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • illustrates the way these parts interact. Administrative tools allow you to bind destinations(D) and connection factories(CF) into a Java Naming and Directory InterfaceTM (JNDI) API namespace. A JMS client can then look up the administered objects in the namespace and then establish a logical connection to the same objects through the JMS provider.
  • Separate different clients by selector supports load balance
  • supports fail-over
  • A " persistent message " is a message that defines the relationship between a "Message Producer" and the "JMS Server". This can be established for both point-to-point and publish/subscribe. This has to do with the guaranteed once only delivery of the message by persisting the message after it has been recieved from the message producer.
  • A " durable message " is a message where the JMS server will hold on to a message if the subscriber is temporarily unavaliable. So the durability is defined by the relationship between a "Topc Subscriber" and the "JMS Server". Durability is applicable only to publish/Subscribe paradigm. For this to happen subscribers need to register themselves with a unique " client id ".
  • using rollback function for load balance purpose
  • Backup amq.dir
  • Backup amq.dir
  • The VM transport allows clients to connect to each other inside the VM without the overhead of the network communication. The connection used is not that of a socket connection but instead uses direct method invocations to enable a high performance embedded messaging system. The first client to use the VM connection will boot an embedded broker. Subsequent connections will attach that the same broker. Once all VM connections to the broker have been closed, the embedded broker will automatically shutdown. The failover transport uses random by default which lets you to load balance clients over a number of brokers. If you would rather connect to a primary first and only connect to a secondary backup broker if the primary is unavailable, turn of randomizing using something like failover:(tcp://primary:61616,tcp://secondary:61616)?randomize=false
  • Async publishing First some background: When an ActiveMQ message producer sends a non-persistent message, its dispatched asynchronously (fire and forget) - but for persistent messages, the publisher will block until it gets a notification that the message has been processed (saved to the store - queued to be dispatched to any active consumers etc) by the broker. messages are dispatched with delivery mode set to be persistent by default (which is required by the JMS spec). So if you are sending messages on a Topic, the publisher will block by default (even if there are no durable subscribers on the topic) until the broker has returned a notification. So if you looking for good performance with topic messages, either set the delivery mode on the publisher to be non-persistent, or set the useAsyncSend property on the ActiveMQ ConnectionFactory to be true . Pre-fetch sizes for Consumers ActiveMQ will push as many messages to the consumer as fast as possible, where they will be queued for processing by an ActiveMQ Session. The maximum number of messages that ActiveMQ will push to a Consumer without the Consumer processing a message is set by the pre-fetch size. You can improve throughput by running ActiveMQ with larger pre-fetch sizes. Pre-fetch sizes are determined by the ActiveMQPrefetchPolicy bean, which is set on the ActiveMQ ConnectionFactory. Straight through Session Consumption By default, a Consumer's session will dispatch messages to the consumer in a separate thread. If you are using Consumers with auto acknowledge, you can increase throughput by passing messages straight through the Session to the Consumer by setting the alwaysSessionAsync property on the ActiveMQ ConnectionFactory to be true
  • ActiveMQ optimized : Kaha persistence, async is true, topic : 65532, message prefetch : 2000 another performance benchmark
  • The number means the message count per second The higher the number, the higher the performance
  • JMS Introduction

    1. 1. JMS Introduction Alex Su 10/12/11
    2. 2. Agenda <ul><li>JMS Tutorial </li></ul><ul><li>ActiveMQ Configuration </li></ul><ul><li>Integration with Spring </li></ul><ul><li>Monitoring the Broker </li></ul><ul><li>Performance Test </li></ul><ul><li>Reference </li></ul><ul><li>Live Demo </li></ul><ul><li>Q & A </li></ul>
    3. 3. JMS Tutorial <ul><li>Basic JMS API Concepts </li></ul><ul><ul><li>Messaging Domains </li></ul></ul><ul><ul><li>Message Consumption </li></ul></ul><ul><li>The JMS API Programming Model </li></ul><ul><ul><li>Message Type </li></ul></ul><ul><ul><li>Message Persistence </li></ul></ul><ul><ul><li>Using Advanced Reliability Mechanisms </li></ul></ul><ul><ul><ul><li>Creating Durable Subscriptions </li></ul></ul></ul><ul><ul><ul><li>Using JMS API Transactions </li></ul></ul></ul>
    4. 4. Basic JMS API Concepts
    5. 5. Messaging Domains <ul><li>Point-to-Point Messaging Domain(Queue) </li></ul><ul><ul><li>Each message has only one consumer. </li></ul></ul><ul><ul><li>A sender and a receiver of a message have no timing dependencies. The receiver can fetch the message whether or not it was running when the client sent the message. </li></ul></ul><ul><ul><li>The receiver acknowledges the successful processing of a message. </li></ul></ul>
    6. 6. Messaging Domains <ul><li>Publish/Subscribe Messaging Domain(Topic) </li></ul><ul><ul><li>Each message may have multiple consumers. </li></ul></ul><ul><ul><li>Publishers and subscribers have a timing dependency. 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. </li></ul></ul>
    7. 7. Message Consumption <ul><li>Synchronously </li></ul><ul><ul><li>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. </li></ul></ul><ul><li>Asynchronously </li></ul><ul><ul><li>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 contents of the message. </li></ul></ul>
    8. 8. The JMS API Programming Model
    9. 9. The JMS API Programming Model <ul><li>Context ctx = new InitialContext(); </li></ul><ul><li>QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctx </li></ul><ul><li>.lookup( &quot;QueueConnectionFactory&quot; ); </li></ul><ul><li>Queue myQueue = (Queue) ctx.lookup( &quot;MyQueue&quot; ); </li></ul><ul><li>QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); </li></ul><ul><li>QueueSession queueSession = queueConnection.createQueueSession( true , </li></ul><ul><li>Session. AUTO_ACKNOWLEDGE ); </li></ul><ul><li>QueueSender queueSender = queueSession.createSender(myQueue); </li></ul><ul><li>TextMessage message = queueSession.createTextMessage(); </li></ul><ul><li>message.setText( &quot;Hello World&quot; ); </li></ul><ul><li>queueSender.send(message); </li></ul>
    10. 10. Message Type <ul><li>TextMessage </li></ul><ul><ul><li>A java.lang.String object </li></ul></ul><ul><li>MapMessage </li></ul><ul><ul><li>A set of name/value pairs </li></ul></ul><ul><li>BytesMessage </li></ul><ul><ul><li>A stream of uninterpreted bytes </li></ul></ul><ul><li>StreamMessage </li></ul><ul><ul><li>A stream of primitive values </li></ul></ul><ul><li>ObjectMessage </li></ul><ul><ul><li>A Serializable object </li></ul></ul>
    11. 11. <ul><li>The JMS API supports two delivery modes for messages to specify whether messages are lost if the JMS provider fails. </li></ul><ul><li>PERSISTENT </li></ul><ul><li>which is the default, instructs the JMS provider to take extra care to ensure that a message is not lost in transit in case of a JMS provider failure. </li></ul><ul><li>NON_PERSISTENT </li></ul><ul><li>does not require the JMS provider to store the message or otherwise guarantee that it is not lost if the provider fails. </li></ul>Message Persistence
    12. 12. Creating Durable Subscriptions <ul><li>durable subscriber </li></ul><ul><ul><li>registers a durable subscription with a unique identity that is retained by the JMS provider. </li></ul></ul><ul><li>nondurable subscriber </li></ul><ul><ul><li>receives only messages that are published while it is active. </li></ul></ul>
    13. 13. Using JMS API Transactions <ul><li>You can group a series of operations together into an atomic unit of work called a transaction. If any one of the operations fails, the transaction can be rolled back, and the operations can be attempted again from the beginning. If all the operations succeed, the transaction can be committed. </li></ul><ul><li>It is important to note that the production and the consumption of a message cannot both be part of the same transaction. </li></ul>
    14. 14. ActiveMQ Configuration <ul><li>Topologies </li></ul><ul><li>Persistence </li></ul><ul><ul><li>AMQ Message Store </li></ul></ul><ul><ul><li>Kaha Message Store </li></ul></ul><ul><ul><li>JDBC </li></ul></ul><ul><li>Transport Configuration Options </li></ul><ul><li>Cluster </li></ul><ul><li>Performance Tuning </li></ul><ul><li>Performance Benchmark Report </li></ul>
    15. 15. Topologies
    16. 16. Persistence <ul><li>AMQ Message Store </li></ul><ul><li>< broker useJmx = &quot;true&quot; xmlns = &quot;; </li></ul><ul><li>persistent = &quot;true&quot; dataDirectory = &quot;${amq.dir}&quot; > </li></ul><ul><li>< persistenceAdapter > </li></ul><ul><li>< amqPersistenceAdapter directory = &quot;${amq.dir}&quot; </li></ul><ul><li>maxFileLength = &quot;20 mb&quot; /> </li></ul><ul><li></ persistenceAdapter > </li></ul><ul><li></ broker > </li></ul>
    17. 17. Persistence <ul><li>Kaha Message Store </li></ul><ul><li>< broker useJmx = &quot;true&quot; xmlns = &quot;; </li></ul><ul><li>persistent = &quot;true&quot; dataDirectory = &quot;${amq.dir}&quot; > </li></ul><ul><li>< persistenceAdapter > </li></ul><ul><li><kaha PersistenceAdapter directory = &quot;${amq.dir}&quot; </li></ul><ul><li>maxFileLength = &quot;20 mb&quot; /> </li></ul><ul><li></ persistenceAdapter > </li></ul><ul><li></ broker > </li></ul>
    18. 18. Persistence <ul><li>JDBC </li></ul><ul><li>< broker useJmx = &quot;true&quot; xmlns = &quot;; > </li></ul><ul><li>< persistenceAdapter > </li></ul><ul><li>< journaledJDBC journalLogFiles = &quot;5&quot; </li></ul><ul><li>dataDirectory = &quot;${basedir}/activemq-data&quot; dataSource = &quot;#mysql-ds&quot; /> </li></ul><ul><li></ persistenceAdapter > </li></ul><ul><li></ broker > </li></ul><ul><li>< bean id = &quot;mysql-ds&quot; class = &quot;org.apache.commons.dbcp.BasicDataSource&quot; </li></ul><ul><li>destroy-method = &quot;close&quot; > </li></ul><ul><li>< property name = &quot;driverClassName&quot; value = &quot;com.mysql.jdbc.Driver&quot; /> </li></ul><ul><li>< property name = &quot; url &quot; </li></ul><ul><li>value = &quot;jdbc:mysql://localhost/activemq?relaxAutoCommit=true&quot; /> </li></ul><ul><li>< property name = &quot;username&quot; value = &quot;activemq&quot; /> </li></ul><ul><li>< property name = &quot;password&quot; value = &quot;activemq&quot; /> </li></ul><ul><li>< property name = &quot;poolPreparedStatements&quot; value = &quot;true&quot; /> </li></ul><ul><li></ bean > </li></ul>
    19. 19. Transport Configuration Options <ul><li>VM Transport </li></ul><ul><ul><li>allows clients to connect to each other inside the VM without the overhead of the network communication. </li></ul></ul><ul><ul><li>vm://localhost </li></ul></ul><ul><li>TCP Transport </li></ul><ul><ul><li>allows clients to connect a remote ActiveMQ using a a TCP socket. </li></ul></ul><ul><ul><li>tcp://localhost:61616 </li></ul></ul><ul><li>Failover Transport </li></ul><ul><ul><li>The Failover transport layers reconnect logic on top of any of the other transports. </li></ul></ul><ul><ul><li>failover:(tcp://primary:61616,tcp://secondary:61616)?randomize=false </li></ul></ul>
    20. 20. Cluster <ul><li>Pure Master Slave </li></ul><ul><ul><li>Requires manual restart to bring back a failed master and can only support 1 slave </li></ul></ul><ul><li>Shared File System Master Slave </li></ul><ul><ul><li>Requires shared file system </li></ul></ul><ul><li>JDBC Master Slave </li></ul><ul><ul><li>Requires a shared database. Also relatively slow as it cannot use the high performance journal </li></ul></ul>
    21. 21. Performance Tuning <ul><li>Async publishing </li></ul><ul><ul><li>the publisher will block by default until the broker has returned a notification. </li></ul></ul><ul><li>Pre-fetch sizes for Consumers </li></ul><ul><ul><li>The maximum number of messages that ActiveMQ will push to a Consumer without the Consumer processing a message </li></ul></ul><ul><li>Straight through Session Consumption </li></ul><ul><ul><li>By default, a Consumer's session will dispatch messages to the consumer in a separate thread. </li></ul></ul>
    22. 22. Performance Benchmark Report <ul><li>Software </li></ul><ul><ul><li>ActiveMQ4.0.1(default, kaha, optimized) </li></ul></ul><ul><ul><li>SwiftMQ 6.1 </li></ul></ul><ul><ul><li>SonicMQ 7.0 </li></ul></ul><ul><ul><li>JBossMessaging 1.0.1 </li></ul></ul><ul><li>Environment </li></ul><ul><ul><li>CPU : 2.40G </li></ul></ul><ul><ul><li>RAM : 1 G </li></ul></ul><ul><ul><li>OS : Windows Server 2003 SP1 </li></ul></ul><ul><li>Messaging Domains </li></ul><ul><ul><li>Topic </li></ul></ul><ul><ul><li>Queue </li></ul></ul><ul><li>Persistence </li></ul><ul><ul><li>PERSISTENT </li></ul></ul><ul><ul><li>NON_PERSISTENT </li></ul></ul><ul><li>Subscriber Mode </li></ul><ul><ul><li>Durable </li></ul></ul><ul><ul><li>Nondurable </li></ul></ul>
    23. 23. Performance Benchmark Report <ul><li>Topic Domain </li></ul>
    24. 24. Performance Benchmark Report <ul><li>Queue Domain </li></ul>
    25. 25. Integration with Spring <ul><li>Using an embedded broker </li></ul><ul><li>< amq:broker useJmx = &quot;true&quot; brokerName = &quot;BROKER&quot; persistent = &quot;true&quot; > </li></ul><ul><li>< amq:persistenceAdapter > </li></ul><ul><li>< amq:amqPersistenceAdapter </li></ul><ul><li>directory = &quot;d:/activemq&quot; /> </li></ul><ul><li></ amq:persistenceAdapter > </li></ul><ul><li>< amq:transportConnectors > </li></ul><ul><li>< amq:transportConnector uri = &quot;vm://localhost&quot; /> </li></ul><ul><li></ amq:transportConnectors > </li></ul><ul><li></ amq:broker > </li></ul><ul><li>< amq:queue name = &quot;queue&quot; physicalName = &quot;testQueue&quot; /> </li></ul>
    26. 26. Integration with Spring <ul><li>Configuring the JMS client </li></ul><ul><li>< amq:connectionFactory id = &quot;connectionFactory&quot; brokerURL = “vm://localhost&quot; /> </li></ul><ul><li>< bean id = &quot;jmsTemplate&quot; class = &quot;org.springframework.jms.core.JmsTemplate&quot; > </li></ul><ul><li>< property name = &quot;connectionFactory&quot; > </li></ul><ul><li> < bean class = &quot;org.springframework.jms.connection.SingleConnectionFactory&quot; > </li></ul><ul><li>< property name = &quot;targetConnectionFactory&quot; ref = &quot;connectionFactory&quot; /> </li></ul><ul><li></ bean > </li></ul><ul><li></ property > </li></ul><ul><li></ bean > </li></ul><ul><li>< bean id = &quot;queueService&quot; class = &quot;; > </li></ul><ul><li>< property name = &quot;template&quot; ref = &quot;jmsTemplate&quot; /> </li></ul><ul><li>< property name = &quot;destination&quot; ref = &quot;queue&quot; /> </li></ul><ul><li></ bean > </li></ul><ul><li>< bean id =“ queueListener” class = “” /> </li></ul><ul><li>< bean id = &quot;queueListenerContainer&quot; </li></ul><ul><li>class = &quot;org.springframework.jms.listener.DefaultMessageListenerContainer&quot; > </li></ul><ul><li>< property name = &quot;connectionFactory&quot; ref = &quot;connectionFactory&quot; /> </li></ul><ul><li>< property name = &quot;destination&quot; ref = &quot;queue&quot; /> </li></ul><ul><li>< property name = &quot;messageListener&quot; ref = &quot;queueListener&quot; /> </li></ul><ul><li></ bean > </li></ul>
    27. 27. Monitoring the Broker <ul><li>Web Console </li></ul><ul><ul><li>you can point your web browser at the URL </li></ul></ul><ul><ul><li>http://localhost:8161/admin </li></ul></ul><ul><li>Command Agent </li></ul><ul><ul><li>Using Jabber (XMPP) to talk to the Broker </li></ul></ul><ul><li>JMX (recommended) </li></ul><ul><ul><li>Run a JMX console (e.g. jconsole - JMX console included in the JDK <JAVA_HOME>/bin/jconsole.exe) </li></ul></ul><ul><ul><li>Connect to the given JMX URL: </li></ul></ul><ul><ul><li>service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi </li></ul></ul>
    28. 28. Monitoring the Broker
    29. 29. Performance Test <ul><li>Using JMeter </li></ul>
    30. 30. Reference <ul><li>Java Message Service Tutorial </li></ul><ul><ul><li> </li></ul></ul><ul><li>ActiveMQ </li></ul><ul><li> </li></ul><ul><li>JMeter </li></ul><ul><li> </li></ul>
    31. 31. Live Demo
    32. 32. Q & A