JMS Introduction

Post on 19-Jan-2015

3.135 views 2 download

Tags:

description

 

Transcript of JMS Introduction

Trend Micro Confidential

JMS Introduction

Alex Su

10/04/23

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Agenda

• JMS Tutorial• ActiveMQ Configuration• Integration with Spring• Monitoring the Broker• Performance Test• Reference• Live Demo• Q & A

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

JMS Tutorial

• Basic JMS API Concepts– Messaging Domains– Message Consumption

• The JMS API Programming Model– Message Type – Message Persistence– Using Advanced Reliability Mechanisms

• Creating Durable Subscriptions• Using JMS API Transactions

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Basic JMS API Concepts

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Messaging Domains

• Point-to-Point Messaging Domain(Queue)– Each message has only one consumer. – A sender and a receiver of a message have no timing dependen

cies. The receiver can fetch the message whether or not it was running when the client sent the message.

– The receiver acknowledges the successful processing of a message.

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Messaging Domains

• Publish/Subscribe Messaging Domain(Topic)– Each message may have multiple consumers. – Publishers and subscribers have a timing dependency. A client t

hat 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.

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Message Consumption

• SynchronouslyA subscriber or a receiver explicitly fetches the message from the d

estination 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.

• AsynchronouslyA client can register a message listener with a consumer. A messag

e 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.

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

The JMS API Programming Model

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

The JMS API Programming Model

Context ctx = new InitialContext();

QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ctx

.lookup("QueueConnectionFactory");

Queue myQueue = (Queue) ctx.lookup("MyQueue");

QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();

QueueSession queueSession = queueConnection.createQueueSession(true,

Session.AUTO_ACKNOWLEDGE);

QueueSender queueSender = queueSession.createSender(myQueue);

TextMessage message = queueSession.createTextMessage();message.setText("Hello World");

queueSender.send(message);

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Message Type

• TextMessageA java.lang.String object

• MapMessage A set of name/value pairs

• BytesMessageA stream of uninterpreted bytes

• StreamMessage A stream of primitive values

• ObjectMessage A Serializable object

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

The JMS API supports two delivery modes for messages to specify whether messages are lost if the JMS provider fails.

• PERSISTENTwhich 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.

• NON_PERSISTENT does not require the JMS provider to store the message or otherwise guarantee that it is not lost if the provider fails.

Message Persistence

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Creating Durable Subscriptions

• durable subscriber registers a durable subscription with a unique identity that is retained by the JMS

provider.

• nondurable subscriber receives only messages that are published while it is active.

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Using JMS API Transactions

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.

It is important to note that the production and the consumption of a message cannot both be part of the same transaction.

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

ActiveMQ Configuration

• Topologies• Persistence

– AMQ Message Store– Kaha Message Store– JDBC

• Transport Configuration Options• Cluster• Performance Tuning• Performance Benchmark Report

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Topologies

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Persistence

• AMQ Message Store<broker useJmx="true" xmlns="http://activemq.apache.org/schema/cor

e"

persistent="true" dataDirectory="${amq.dir}">

<persistenceAdapter>

<amqPersistenceAdapter directory="${amq.dir}"

maxFileLength="20 mb" />

</persistenceAdapter>

</broker>

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Persistence

• Kaha Message Store<broker useJmx="true" xmlns="http://activemq.apache.org/schema/cor

e"

persistent="true" dataDirectory="${amq.dir}">

<persistenceAdapter>

<kahaPersistenceAdapter directory="${amq.dir}"

maxFileLength="20 mb" />

</persistenceAdapter>

</broker>

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Persistence

• JDBC

<broker useJmx="true" xmlns="http://activemq.apache.org/schema/core"> <persistenceAdapter> <journaledJDBC journalLogFiles="5" dataDirectory="${basedir}/activemq-data" dataSource="#mysq

l-ds" /> </persistenceAdapter></broker>

<bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true" /> <property name="username" value="activemq" /> <property name="password" value="activemq" /> <property name="poolPreparedStatements" value="true" /></bean>

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Transport Configuration Options

• VM Transportallows clients to connect to each other inside the VM without the overhead

of the network communication.

vm://localhost

• TCP Transportallows clients to connect a remote ActiveMQ using a a TCP socket.

tcp://localhost:61616

• Failover TransportThe Failover transport layers reconnect logic on top of any of the other tran

sports.

failover:(tcp://primary:61616,tcp://secondary:61616)?randomize=false

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Cluster

• Pure Master Slave Requires manual restart to bring back a failed master and can only support 1 slave

• Shared File System Master SlaveRequires shared file system

• JDBC Master SlaveRequires a shared database. Also relatively slow as it cannot use the high

performance journal

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Performance Tuning

• Async publishingthe publisher will block by default until the broker has returned a no

tification.

• Pre-fetch sizes for ConsumersThe maximum number of messages that ActiveMQ will push to a Consume

r without the Consumer processing a message

• Straight through Session ConsumptionBy default, a Consumer's session will dispatch messages to the consumer i

n a separate thread.

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Performance Benchmark Report

• Software– ActiveMQ4.0.1(default, kaha, optimized)– SwiftMQ 6.1– SonicMQ 7.0 – JBossMessaging 1.0.1

• Environment– CPU : 2.40G– RAM : 1 G– OS : Windows Server 2003 SP1

• Messaging Domains– Topic– Queue

• Persistence– PERSISTENT– NON_PERSISTENT

• Subscriber Mode– Durable– Nondurable

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Performance Benchmark Report

• Topic Domain

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Performance Benchmark Report

• Queue Domain

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Integration with Spring

• Using an embedded broker

<amq:broker useJmx="true" brokerName="BROKER" persistent="true"> <amq:persistenceAdapter> <amq:amqPersistenceAdapter directory="d:/activemq" /> </amq:persistenceAdapter> <amq:transportConnectors> <amq:transportConnector uri="vm://localhost" /> </amq:transportConnectors> </amq:broker>

<amq:queue name="queue" physicalName="testQueue" />

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Integration with Spring

• Configuring the JMS client

<amq:connectionFactory id="connectionFactory" brokerURL=“vm://localhost" />

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory">

<bean class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory" ref="connectionFactory" /> </bean> </property></bean>

<bean id="queueService" class="com.trend.tmma.mq.QueueServiceImpl"> <property name="template" ref="jmsTemplate" /> <property name="destination" ref="queue" /></bean>

<bean id=“queueListener” class=“com.trend.tmma.mq.QueueListener” />

<bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queue" /> <property name="messageListener" ref="queueListener" /></bean>

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Monitoring the Broker

• Web Consoleyou can point your web browser at the URL

http://localhost:8161/admin

• Command AgentUsing Jabber (XMPP) to talk to the Broker

• JMX (recommended)1. Run a JMX console (e.g. jconsole - JMX console included in the JDK <

JAVA_HOME>/bin/jconsole.exe)

2. Connect to the given JMX URL:

service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Monitoring the Broker

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Performance Test

• Using JMeter

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Reference

• Java Message Service Tutorialhttp://java.sun.com/products/jms/tutorial/1_3_1-fcs/doc/copyright.html

• ActiveMQ

http://activemq.apache.org/index.html

• JMeter

http://jakarta.apache.org/jmeter/index.html

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Live Demo

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Q & A