JMS Introduction

32
Trend Micro Confidential JMS Introduction Alex Su 07/05/22

description

 

Transcript of JMS Introduction

Page 1: JMS Introduction

Trend Micro Confidential

JMS Introduction

Alex Su

10/04/23

Page 2: JMS Introduction

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

Page 3: JMS Introduction

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

Page 4: JMS Introduction

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Basic JMS API Concepts

Page 5: JMS Introduction

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.

Page 6: JMS Introduction

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.

Page 7: JMS Introduction

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.

Page 8: JMS Introduction

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

The JMS API Programming Model

Page 9: JMS Introduction

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);

Page 10: JMS Introduction

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

Page 11: JMS Introduction

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

Page 12: JMS Introduction

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.

Page 13: JMS Introduction

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.

Page 14: JMS Introduction

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

Page 15: JMS Introduction

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Topologies

Page 16: JMS Introduction

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>

Page 17: JMS Introduction

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>

Page 18: JMS Introduction

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>

Page 19: JMS Introduction

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

Page 20: JMS Introduction

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

Page 21: JMS Introduction

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.

Page 22: JMS Introduction

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

Page 23: JMS Introduction

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Performance Benchmark Report

• Topic Domain

Page 24: JMS Introduction

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Performance Benchmark Report

• Queue Domain

Page 25: JMS Introduction

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" />

Page 26: JMS Introduction

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>

Page 27: JMS Introduction

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

Page 28: JMS Introduction

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Monitoring the Broker

Page 29: JMS Introduction

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Performance Test

• Using JMeter

Page 30: JMS Introduction

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

Page 31: JMS Introduction

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Live Demo

Page 32: JMS Introduction

Trend Micro Confidential Copyright 2008 - Trend Micro Inc.

Q & A