jms introduction

32
Trend Micro Confidential JMS Introduction 21/06/22

Upload: bui-kiet

Post on 14-Apr-2017

151 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Jms introduction

Trend Micro Confidential

JMS Introduction

03/05/23

Page 2: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Agenda

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

Page 3: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Basic JMS API Concepts

Page 5: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

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

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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Messaging Domains

• Publish/Subscribe Messaging Domain(Topic)– Each message may have multiple consumers. – 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.

Page 7: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Message Consumption

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

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

Page 8: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

The JMS API Programming Model

Page 9: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

The JMS API Programming ModelContext 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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Message Type • TextMessage

A 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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Using JMS API TransactionsYou 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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

ActiveMQ Configuration

• Topologies• Persistence

– AMQ Message Store– Kaha Message Store– JDBC

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

Page 15: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Topologies

Page 16: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Persistence

• AMQ Message Store<broker useJmx="true"

xmlns="http://activemq.apache.org/schema/core" persistent="true" dataDirectory="${amq.dir}"> <persistenceAdapter> <amqPersistenceAdapter directory="${amq.dir}" maxFileLength="20 mb" /> </persistenceAdapter></broker>

Page 17: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Persistence

• Kaha Message Store<broker useJmx="true"

xmlns="http://activemq.apache.org/schema/core" persistent="true" dataDirectory="${amq.dir}"> <persistenceAdapter> <kahaPersistenceAdapter directory="${amq.dir}" maxFileLength="20 mb" /> </persistenceAdapter></broker>

Page 18: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

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

dataSource="#mysql-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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

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

transports.failover:(tcp://primary:61616,tcp://secondary:61616)?

randomize=false

Page 20: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Performance Tuning

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

notification.

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

Consumer without the Consumer processing a message

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

in a separate thread.

Page 22: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Performance Benchmark Report

• Topic Domain

Page 24: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Performance Benchmark Report

• Queue Domain

Page 25: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Monitoring the Broker• Web Console

you can point your web browser at the URLhttp://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

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Monitoring the Broker

Page 29: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Performance Test

• Using JMeter

Page 30: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Reference

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

• ActiveMQhttp://activemq.apache.org/index.html

• JMeterhttp://jakarta.apache.org/jmeter/index.html

Page 31: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Live Demo

Page 32: Jms introduction

Copyright 2008 - Trend Micro Inc.

Trend Micro Confidential

Q & A