Active MQ
Active MQ
Active MQ
Connector URI
URI = compact string of characters for identifying an abstract or a physical resource
tcp://localhost:61616 = create a TCP connection to the localhost on port 61616.
Transport Connectors
Mechanism used to accept and listen to connection from clients.
<transportConnectors>
<transportConnector name="openwire" uri="tcp://localhost:61616"
discoveryUri="multicast://default"/>
<transportConnector name="ssl" uri="ssl://localhost:61617"/>
<transportConnector name="stomp" uri="stomp://localhost:61613"/>
<transportConnector name="xmpp" uri="xmpp://localhost:61222"/>
</transportConnectors>
Summary
Protocol Description
TCP Transmission Control Protocol. Default network protocol for most use cases. Highly reli-
able host-to-host protocol.
tcp://hostname:port?key=value&key=value
NIO New I/O protocol. Provides better scalability for connections from producers to the
broker.
nio://hostname:port?key=value
UDP User Datagram Protocol. To deal with the firewall between clients and the broker. UDP
does not guarantee packet ordering and uniqueness, and is connectionless.
udp://hostname:port?key=value
HTTP(S) To deal with the firewall between clients and the broker
http[s]://hostname:port?key=value
VM For communication within the same Java Virtual Machine (JVM)
vm://brokerName?key=value
Keystores = hold your private certificates with their corresponding private <> Truststores = hold
other applications trusted certificates.
A network of brokers creates a cluster composed of multiple ActiveMQ instances that are interconnec-
ted to meet more advanced scenarios.
Discovery is a process of detecting remote broker services.
A static network connector is used to create a static configuration of multiple brokers:
static:(uri1,uri2,uri3,...)?key=value
IP multicast is a network technique for transmission to a group of interested receivers. The group ad-
dress is an IP address in the range of 224.0.0.0 to 239.255.255.255. Brokers use the multicast protocol to
advertise their services and locate the services of other brokers. Clients use multicast to locate brokers
and establish a connection with them.
multicast://ipadaddress:port?key=value
The discovery protocol allows client to discover brokers and randomly choose one to connect to.
A fanout protocol is used by clients to connect to multiple brokers and replicate operations between
them.
5. ActiveMQ message storage
Delivery modes: persistent and non-persistent. Persistent message must be logged to a stable storage.
The AMQ message store, like KahaDB, is a combination of a transactional journal for reliable persistence
(to survive system crashes) and high-performance indexes, which makes this store the best option when
message throughput is the main requirement for an application.
The JDBC message store allows messages to be store into databases. Default database: Apache Derby.
Recovery policies allow for fine-tuning the duration and type of messages that are cached for non-
durable topic consumers.
6. Securing ActiveMQ
The easiest way to secure the broker is through the use of authentication credentials placed directly in
the brokers XML configuration file.
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="admin" password="password"
groups="admins,publishers,consumers"/>
<authenticationUser username="publisher" password="password"
groups="publishers,consumers"/>
<authenticationUser username="consumer" password="password"
groups="consumers"/>
<authenticationUser username="guest" password="password"
groups="guests"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
JAAS plug-in
The JAAS plug-in provides the same functionalities than the simple authentication plug-in, but uses
standardized Java mechanism.
<plugins>
<jaasAuthenticationPlugin configuration="activemq-domain" />
</plugins>
Authorization
Message level authorization controls access to particular messages in a destination. Use the
MessageAuthorizationPolicy with the method isAllowedToConsume.
SSL Certificates
Certificates can be used to avoid storing client credentials using plain user names and passwords.
7. Creating Java applications with ActiveMQ
A fully configured broker can serve clients from the same application (using the VM protocol) as well as
clients from remote applications.
The xbean URI scheme tells the broker to search for the given XML configuration file in the classpath.
To use a pure Spring XML syntax with ActiveMQ, define the BrokerService as a bean in the Spring
configuration file.
By default, ActiveMQ uses Spring and Apache XBean for its internal configuration purposes. XBean
provides the ability to define and use a custom XML syntax.
For a high level, a request / reply scenario involves an application that sends a message (request) and
expects to receive a message (reply) in return. Some of the most scalable systems in the world are
implemented using asynchronous processing.
In the diagram below, the producer creates a request in the form of a JMS message and sets a couple of
important properties: the correlation ID and the reply destination. The client configures a consumer to
listen on the reply destination.
Second, a worker receives the request, processes it, and sends a reply message using the destination
named in the JMSReplyTo property of the request message.
To increase the scalability of the process, just add additional workers to handle the load.
tempDest = session.createTemporaryQueue();
consumer = session.createConsumer(tempDest);
consumer.setMessageListener(this);
<bean id="jmsConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
<property name="userName" value="admin" />
<property name="password" value="password" />
</bean>
Configuring JMS destinations
<bean id="cscoConsumer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="cscoDest" />
<property name="messageListener" ref="portfolioListener" />
</bean>
Spring can be used to start ActiveMQ and to provide access to the JMS destinations. It is also possible to
create an instance of the broker with a JMS connection.
Java AS provide a JNDI implementation that expose objects to be used by applications deployed to the
container. Local JNDI is used to configure objects that will be exposed to a specific application, while
global JNDI is for any application in the entire web container.
web.xml:
<resource-ref>
<res-ref-name>jms/ConnectionFactory</res-ref-name>
<res-type>org.apache.activemq.ActiveMQConnectionFactory</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>jms/FooQueue</res-ref-name>
<res-type>javax.jms.Queue</res-type>
<res-auth>Container</res-auth>
</resource-ref>
The <resource-ref> elements reference the JNDI resources that are registered with the AS. This make
the resources available to the web application.
<jee:jndi-lookup id="connectionFactory"
jndi-name="java:comp/env/jms/ConnectionFactory"
cache="true"
resource-ref="true"
lookup-on-startup="true"
expected-type="org.apache.activemq.ActiveMQConnectionFactory"
proxy-interface="javax.jms.ConnectionFactory" />
The <jndi-lookup> elements utilize Spring to perform a JNDI lookup of the noted resource. The
resources are injected into the messageSenderService bean.
<bean id="messageSenderService"
class="org....book.ch8.jms.service.JmsMessageSenderService"
p:jmsTemplate-ref="jmsTemplate" />
This bean is used by the web application to send a JMS message.
<Context reloadable="true">
<Resource auth="Container"
name="jms/ConnectionFactory"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="vm://localhost?brokerConfig=xbean:activemq.xml"
brokerName="MyActiveMQBroker"/>
<Resource auth="Container"
name="jms/FooQueue"
type="org.apache.activemq.command.ActiveMQQueue"
description="JMS queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="FOO.QUEUE"/>
</Context>
If a resource has been defined in a <Context> element it is not necessary for that resource to be
defined in /WEB-INF/web.xml. However, it is recommended to keep the entry in /WEB-INF/ web.xml to
document the resource requirements for the web application.
9. ActiveMQ messaging for other languages
STOMP: Streaming Text Oriented Messaging Protocol
A failure of the master is detected by loss of connectivity from the slave to the master.
Shared nothing master/slave: each broker has its own unique message storage. All messages are
replicated from the master to the slave. A master is allowed only one slave, and a slave itself can't have
another slave. Solution acceptable when some down time on failure is acceptable. Manual intervention
by an administrator will be necessary to configure a new slave for the new master.
Shared storage master/slave: multiple brokers can connect to the shared message store (a DB or a share
file system) with only one broker active at a time. No manual intervention is required to maintain the
integrity of the application. No limitation on the number of slave brokers. The new master will be the
slave able to grab the lock on the DB.
Store and forward: messages are always stored in the local broker, and forwarded across the network to
another broker.
Vertical Scaling: technique used to increase the number of connections that a single broker can handle.
By default, ActiveMQ uses blocking I/O for connections (= one thread per connection). Non-blocking I/O
reduces the number of threads.
Horizontal Scaling: technique to increase the number of ActiveMQ brokers available for the applications.
Introduce some latency because messages may have to pass through multiple brokers before being
delivered to a consumer.
Default delivery mode is persistent. Non-persistent messages are significantly faster than persistent
messages (the producer does need to wait for a receipt from the broker).
In ActiveMQ non-persistent delivery is reliable: the delivery of messages will survive network outages
and system crashes, as long as the producer is active: it holds messages for redelivery in its failover
transport cache.
Transactions
Embedding brokers
Binary format used for transporting commands over a transport (such as TCP) to the broker.
Asynchronous send
Tell the MessageProducer not to except a receipt for messages it sends to the ActiveMQ broker.
Producer flow control allows the message broker to slow the rate of messages that are passed through it
when resources are running low.
Consumer
Some of the biggest performance gains are obtained by tuning the consumers.
SUNJMX="-Dcom.sun.management.jmxremote"
The JMX agent in the JVM is controlled by the com.sun.management.jmxremote property, whereas
the ActiveMQ domain is controlled by the useJmx attribute in the broker configuration file.
MBean name:
Command agent: allows you to issue administration commands to the broker using plain JMS messages.
JConsole: client application that allows you to browse and call methods of exposed MBeans.