messaging with activemq

75
Real World Messaging With Apache ActiveMQ Bruce Snyder [email protected] 7 Nov 2008 New Orleans, Louisiana

Upload: bruce-snyder

Post on 07-Nov-2014

32.200 views

Category:

Technology


2 download

DESCRIPTION

Presentation from ApacheCon US 2008

TRANSCRIPT

Page 1: Messaging With ActiveMQ

Real World MessagingWith Apache ActiveMQ

Bruce [email protected] Nov 2008 New Orleans, Louisiana

Page 2: Messaging With ActiveMQ

Do You Use JMS?

2

Page 3: Messaging With ActiveMQ

Agenda

• Common questions • ActiveMQ features

3

Page 4: Messaging With ActiveMQ

4

What is ActiveMQ?

• Message-oriented middleware • Apache project

– http://activemq.apache.org/ • Apache licensed• JMS 1.1 compliant • Goal:

– Standards-based, message-oriented application integration across many languages and platforms

Page 5: Messaging With ActiveMQ

What kind of hardware is needed for a throughput of X number of messages per

second?

5

Page 6: Messaging With ActiveMQ

What Are Your Performance Objectives?

6

Page 7: Messaging With ActiveMQ

What Trade-Offs Can You Accept?

7

Page 8: Messaging With ActiveMQ

Common Trade-Offs

• Messaging Domain• Durability v. Persistence• Message acks v. transactions• Message consumption types

8

Page 9: Messaging With ActiveMQ

Messaging Domains

• Do all consumers need a copy of the messages?

• What if consumers are disconnected? • Can consumers deal with missing

messages? • Do you need request/reply messaging?

9

Page 10: Messaging With ActiveMQ

Durability v. Persistence

• What quality of service do you need?

– Durability - should messages be held while consumer is offline?

– Persistence - used to preserve messages in the event of a JMS provider failure

10

Page 11: Messaging With ActiveMQ

Message Acks v. Transactions

• When messages are received, the consumer must acknowledge the message– There is no unacknowledge!

• Transactions allow for: – Rollback for error handling – Batching for speed

11

Page 12: Messaging With ActiveMQ

Synchronous Message Consumption

12

public class MyConsumer {...

public void doCreateConsumer() {

Destination destination = consumer.getSession().createQueue("JOBS." + job);

MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination);

while ((message = consumer.receive(timeout)) != null) { processMessage(message); }}

...}

Page 13: Messaging With ActiveMQ

Asynchronous Message Consumption

13

public class MyConsumer {...

public void doCreateConsumer() {

Destination destination = consumer.getSession().createQueue("JOBS." + job);

MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination);

messageConsumer.setMessageListener(new MyMessageListener(job));}

...}

Page 14: Messaging With ActiveMQ

How can the broker be clustered to improve

scalability or guaranteeavailability?

• Let’s talk about two features – Network of brokers – Master/slave

14

Page 15: Messaging With ActiveMQ

Broker Options

• Connectors• Persistence • Master/slave• Security

15

Page 16: Messaging With ActiveMQ

Two Types of Transports

16

Page 17: Messaging With ActiveMQ

Transport Connectors

• Client-to-broker connections– Similar to JDBC connections to a database

• Various protocols are supported: – VM – TCP – NIO – UDP – SSL– HTTP/S

17

Page 18: Messaging With ActiveMQ

Clients Should Be Configured For Failover

18

• Use the failover protocol:

failover:(tcp://broker1:61616,tcp://broker2:61616, \tcp://broker3:61616)?initialReconnectDelay=100

Page 19: Messaging With ActiveMQ

Network of Brokers

• Broker-to-broker connections • Various protocols supported:

– Static – Failover– Multicast – Zeroconf– Peer– Fanout– Discovery

19

Page 20: Messaging With ActiveMQ

Networks of Brokers

• Provides large scalability • ActiveMQ store-and-forward allows

messages to traverse brokers – Demand-based forwarding – Some people call this distributed queues

• Many possible configurations or topologies are supported

20

Page 21: Messaging With ActiveMQ

Topology Example

21

Page 22: Messaging With ActiveMQ

Topology Example

22

Page 23: Messaging With ActiveMQ

Topology Example

23

Page 24: Messaging With ActiveMQ

Topology Example

24

Page 25: Messaging With ActiveMQ

Topology Example

25

Page 26: Messaging With ActiveMQ

Master/Slave Broker Configurations

26

Page 27: Messaging With ActiveMQ

Three Types of Master/Slave

• Pure master/slave• Shared filesystem master/slave• JDBC master/slave

27

Page 28: Messaging With ActiveMQ

Pure Master/Slave

• Shared nothing, fully replicated topology– Does not depend on shared filesystem or

database• A Slave broker consumes all message

states from the Master broker (messages, acks, tx states)

• Slave does not start any networking or transport connectors

28

Page 29: Messaging With ActiveMQ

Pure Master/Slave

• Master broker will only respond to client when a message exchange has been successfully passed to the slave broker

29

Page 30: Messaging With ActiveMQ

Pure Master/Slave

• If the master fails, the slave optionally has two modes of operation: – Start up all it’s network and transport

connectors • All clients connected to failed Master resume

on Slave – Close down completely

• Slave is simply used to duplicate state from Master

30

Page 31: Messaging With ActiveMQ

Shared Filesystem Master/Slave

• Utilizes a directory on a shared filesystem

• No restriction on number of brokers• Simple configuration (point to the data

dir)• One master selected at random

31

Page 32: Messaging With ActiveMQ

JDBC Master/Slave

• Recommended when using a shared database

• No restriction on the number of brokers• Simple configuration• Clustered database negates single

point of failure• One master selected at random

32

Page 33: Messaging With ActiveMQ

Client ConnectivityWith Master/Slave

• Clients should use failover transport:

33

failover://(tcp://masterhost:61616, \tcp://slavehost:61616)?randomize=false

Page 34: Messaging With ActiveMQ

How can the broker be clustered to improve

scalability or guaranteeavailability?

• Create a network of brokers• Use master/slave for broker failover

34

Page 35: Messaging With ActiveMQ

High Availability and Fault Tolerance

• RAIDed disks• A Storage Area Network • Clustered relational databases• Clustered JDBC via C-JDBC

– http://c-jdbc.objectweb.org/

35

Page 36: Messaging With ActiveMQ

What's the real difference between message

persistence strategies?

36

Page 37: Messaging With ActiveMQ

AMQ Message Store

• Transactional message storage solution• Fast and reliable • Composed of two parts:

– Data Store - holds messages in a transactional journal

– Reference store - stores message locations for fast retrieval

• The default message store in ActiveMQ 5

37

Page 38: Messaging With ActiveMQ

Non-Journaled JDBC

• Transactional message storage solution• Reliable but not fast

– JDBC connection overhead is prohibitively slow

38

Page 39: Messaging With ActiveMQ

Journaled JDBC

• Transactional message storage solution• Reliable and faster than non-journaled• Two-piece store

– Journal - A high-performance, transactional journal

– Database - A relational database of your choice

• Default database in ActiveMQ 4.x is Apache Derby

39

Page 40: Messaging With ActiveMQ

Message Cursors

• Messages are no longer stored in memory– Previous to 5.1, message references were

stored in memory • Messages are paged in from storage

when space is available in memory

40

Page 41: Messaging With ActiveMQ

What security options are available?

• Authentication – I.e., are you allowed to connect to ActiveMQ?

• Authorization – I.e., do you have permission to use that

ActiveMQ resource?

41

Page 42: Messaging With ActiveMQ

Authentication

• File based • JAAS based

42

Page 43: Messaging With ActiveMQ

Authorization

• Destination level • Message level via custom plugin

43

Page 44: Messaging With ActiveMQ

What's the best way to connect non-Java clients?

44

• Let’s talk about wire formats

Page 45: Messaging With ActiveMQ

Wire Formats

• OpenWire – The default in ActiveMQ; a binary protocol

• STOMP – Simple Text Oriented Messaging Protocol; a text based protocol

• XMPP– The Jabber XML protocol

• REST– HTTP POST and GET

• AMQP – Not yet fully supported

45

Page 46: Messaging With ActiveMQ

OpenWire

• A binary wire level protocol for marshaling objects to/from byte arrays

• Designed for performance – Sacrificed some ease of implementation

• Clients for C++, Java and .NET– The default is Java

46

Page 47: Messaging With ActiveMQ

STOMP

• A text-based wire level protocol • Designed to be very easy to understand

– Encourages implementation in additional languages

• Can be easily demonstrated via telnet• Clients for C, Javascript, Perl, PHP,

Python, Ruby and more

47

Page 48: Messaging With ActiveMQ

What's the best approach if a producer or consumer

may be down forsome time?

48

Page 49: Messaging With ActiveMQ

Use the failover Protocol

• Utilize the failover options – initialReconnectDelay - wait before reconnect – maxReconnectDelay - max time between reconnects – useExponentialBackOff - exponentially grow time between

reconnects– backOffMultiplier - multiplier for backoff – maxReconnectAttempts - max number of reconnects – randomize - randomly choose from list of brokers – backup - start and hold a hot standby connection

49

Page 50: Messaging With ActiveMQ

Use Producer Flow Control

• Prevents slow consumers from being flooded

50

...<destinationPolicy> <policyMap> <policyEntries> <policyEntry topic="FOO.>"

producerFlowControl="false" memoryLimit="128mb">

</policyEntries> </policyMap></destinationPolicy>...

Page 51: Messaging With ActiveMQ

Use Message Persistence

• Quality of service to preserve messages in the event of broker failure

51

Page 52: Messaging With ActiveMQ

Consumer Options

• Message prefetch• Consumer dispatch async• Exclusive consumer• Consumer priority • Message groups • Redeliery policies • Retroactive consumer • Selectors • Some slow consumer strategies

52

Page 53: Messaging With ActiveMQ

Message Prefetch

• Used for slow consumer situations – Consumer is flooded by messages from the broker

• FIFO buffer on the consumer side

53

Page 54: Messaging With ActiveMQ

Async Dispatch

• Asynchronous message delivery to consumers– Default is true

• Useful for slow consumers – Incurs a bit of overhead

54

Page 55: Messaging With ActiveMQ

Exclusive Consumers

• Anytime more than one consumer is consuming from a queue, message order is lost

• Allows a single consumer to consume all messages on a queue to maintain message ordering

55

Page 56: Messaging With ActiveMQ

Consumer Priority

• Just like it sounds – Gives a consumer priority for message delivery– Allows for the weighting of consumers to optimize

network traversal for message delivery

56

Page 57: Messaging With ActiveMQ

Message Groups

• Uses the JMSXGroupID property to define which message group a message belongs – Guarantees ordered processing of related messages across a

single destination – Load balancing of message processing across multiple

consumers – HA/failover if consumer goes down

57

Page 58: Messaging With ActiveMQ

Redelivery Policy

• Messages are redelivered to a client when:– A transacted session is rolled back– A transacted session is closed before commit – A session is using CLIENT_ACKNOWLEDGE

and Session.recover() is explicitly called

58

Page 59: Messaging With ActiveMQ

Retroactive Consumer

• Message replay at start of a subscription– At the start of every subscription, send any old

messages that the consumer may have missed – Configurable via policies

59

Page 60: Messaging With ActiveMQ

Wildcards on Destinations

• Price.>• Price.Stock.>• Price.Stock.NASDAQ.*• Price.Stock.*.IBM

60

...<destinationPolicy> <policyMap> <policyEntries> <policyEntry topic="Price.Stock.>"

memoryLimit="128mb"> </policyEntries> </policyMap></destinationPolicy>...

Page 61: Messaging With ActiveMQ

Selectors

• Used to attach a filter to a subscription • Defined using a subset SQL 92 syntax • JMS selectors

– Filters only message properties • JMSType = ‘stock’ and trader = ‘bob’ and price < ‘105’

• XPath selectors – Filters message bodies that contain XML

• ‘/message/cheese/text() = 'swiss'’

61

Page 62: Messaging With ActiveMQ

Slow Consumers Strategies

62

Page 63: Messaging With ActiveMQ

Slow Consumer Strategies

• Various configurable strategies for handling slow consumers

• Slow consumer situations are very common • Caused by:

– Slow network connections– Unreliable network connections – Busy network situations – Busy JVM situations – Half disconnects with sockets

63

Page 64: Messaging With ActiveMQ

Use Message Limit Strategies

• PendingMessageLimitStrategy– Calculates the max number of pending messages

to be held in memory for a consumer above its prefetch size

• ConstantPendingMessageLimitStrategy– A constant limit for all consumers

• PrefetchRatePendingMessageLimitStrategy– Calculates the max number of pending messages

using a multiplier of the consumers prefetch size

64

Page 65: Messaging With ActiveMQ

Use Prefetch and an Eviction Policy

• Use the prefetch policy – The prefetch policy has a property named

maximumPendingMessageLimit that can be used on a per connection or per consumer basis

• Use a message eviction policy – oldestMessageEvictionStrategy - Evict the oldest

messages first – oldestMessageWithLowestPriorityEvictionStrategy -

Evict the oldest messages with the lowest priority first

65

Page 66: Messaging With ActiveMQ

Use Destination Policies

• Configured on the destination policies in the ActiveMQ XML configuration file

• Combined with wildcards, this is very powerful

66

Page 67: Messaging With ActiveMQ

Additional Tips

• Consider configuring message cursors • The status of slow consumers can be monitored via

JMX properties – discarded - The count of how many messages have been

discarded during the lifetime of the subscription due to it being a slow consumer

– matched - The current number of messages matched and to be dispatched to the subscription as soon as some capacity is available in the prefetch buffer. So a non-zero value implies that the prefetch buffer is full for this subscription

67

Page 68: Messaging With ActiveMQ

How to monitor individual messages, queue depths, or other broker statistics?

• JMX• ActiveMQ web console • Additional consumers

– Camel routes

• SpringSource AMS – Based on Hyperic

• IONA FuseHQ– Based on Hyperic 68

Page 69: Messaging With ActiveMQ

What is Apache Camel?

http://activemq.apache.org/camel/

Page 70: Messaging With ActiveMQ
Page 71: Messaging With ActiveMQ

Camel Components

Page 72: Messaging With ActiveMQ

Fluent Java API

RouteBuilder MyRoute = new RouteBuilder() { public void configure() { from("activemq:TEST.QUEUE").

to("file:///Users/bsnyder/camelinbox/text.txt").to("log:MyLog");

}};

Page 73: Messaging With ActiveMQ

XML Config

<camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring"> <package>com.mycompany</package> <route> <from uri="activemq:example.A" /> <to uri="file:///Users/bsnyder/camelinbox/text.txt" /> <to uri=”log:MyLog?showProperties=true” /> </route></camelContext>

Page 74: Messaging With ActiveMQ

Information Overload Yet?

Page 75: Messaging With ActiveMQ

Thank You For Attending!

Questions?