2014-10-31 06:20:28 -04:00
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
<!-- <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3CR3//EN"
|
|
|
|
|
"../../../lib/docbook-support/support/docbook-dtd/docbookx.dtd"> -->
|
|
|
|
|
<!-- ============================================================================= -->
|
|
|
|
|
<!-- Copyright © 2009 Red Hat, Inc. and others. -->
|
|
|
|
|
<!-- -->
|
|
|
|
|
<!-- The text of and illustrations in this document are licensed by Red Hat under -->
|
|
|
|
|
<!-- a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). -->
|
|
|
|
|
<!-- -->
|
|
|
|
|
<!-- An explanation of CC-BY-SA is available at -->
|
|
|
|
|
<!-- -->
|
|
|
|
|
<!-- http://creativecommons.org/licenses/by-sa/3.0/. -->
|
|
|
|
|
<!-- -->
|
|
|
|
|
<!-- In accordance with CC-BY-SA, if you distribute this document or an adaptation -->
|
|
|
|
|
<!-- of it, you must provide the URL for the original version. -->
|
|
|
|
|
<!-- -->
|
|
|
|
|
<!-- Red Hat, as the licensor of this document, waives the right to enforce, -->
|
|
|
|
|
<!-- and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent -->
|
|
|
|
|
<!-- permitted by applicable law. -->
|
|
|
|
|
<!-- ============================================================================= -->
|
|
|
|
|
|
|
|
|
|
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<!ENTITY % BOOK_ENTITIES SYSTEM "ActiveMQ_User_Manual.ent">
|
2014-10-31 06:20:28 -04:00
|
|
|
|
%BOOK_ENTITIES;
|
|
|
|
|
]>
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<chapter id="embedding-activemq">
|
|
|
|
|
<title>Embedding ActiveMQ</title>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<para>ActiveMQ is designed as set of simple Plain Old Java Objects (POJOs).
|
|
|
|
|
This means ActiveMQ can be instantiated and run in any dependency injection
|
2014-10-31 06:20:28 -04:00
|
|
|
|
framework such as JBoss Microcontainer, Spring or Google Guice. It also
|
|
|
|
|
means that if you have an application that could use messaging functionality
|
2014-11-17 22:39:49 -05:00
|
|
|
|
internally, then it can <emphasis>directly instantiate</emphasis> ActiveMQ
|
2014-10-31 06:20:28 -04:00
|
|
|
|
clients and servers in its own application code to perform that
|
2014-11-17 22:39:49 -05:00
|
|
|
|
functionality. We call this <emphasis>embedding</emphasis> ActiveMQ.</para>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
|
|
|
|
<para>Examples of applications that might want to do this include any
|
|
|
|
|
application that needs very high performance, transactional, persistent
|
|
|
|
|
messaging but doesn't want the hassle of writing it all from scratch.</para>
|
|
|
|
|
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<para>Embedding ActiveMQ can be done in very few easy steps. Instantiate the
|
2014-10-31 06:20:28 -04:00
|
|
|
|
configuration object, instantiate the server, start it, and you have a
|
2014-11-17 22:39:49 -05:00
|
|
|
|
ActiveMQ running in your virtual machine. It's as simple and easy as
|
2014-10-31 06:20:28 -04:00
|
|
|
|
that.</para>
|
|
|
|
|
|
|
|
|
|
<section>
|
|
|
|
|
<title>Simple Config File Embedding</title>
|
|
|
|
|
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<para>The simplest way to embed ActiveMQ is to use the embedded wrapper
|
|
|
|
|
classes and configure ActiveMQ through its configuration files. There are
|
2014-10-31 06:20:28 -04:00
|
|
|
|
two different helper classes for this depending on whether your using the
|
2014-11-17 22:39:49 -05:00
|
|
|
|
ActiveMQ Core API or JMS.</para>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
|
|
|
|
<section>
|
|
|
|
|
<title>Core API Only</title>
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<para>For instantiating a core ActiveMQ Server only, the steps are pretty
|
2014-10-31 06:20:28 -04:00
|
|
|
|
simple. The example requires that you have defined a configuration file
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<literal>activemq-configuration.xml</literal> in your
|
2014-10-31 06:20:28 -04:00
|
|
|
|
classpath:</para>
|
|
|
|
|
<programlisting>
|
2014-11-17 22:39:49 -05:00
|
|
|
|
import org.apache.activemq.core.server.embedded.EmbeddedActiveMQ;
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
2014-11-17 22:39:49 -05:00
|
|
|
|
EmbeddedActiveMQ embedded = new EmbeddedActiveMQ();
|
2014-10-31 06:20:28 -04:00
|
|
|
|
embedded.start();
|
|
|
|
|
|
2014-11-17 22:39:49 -05:00
|
|
|
|
ClientSessionFactory nettyFactory = ActiveMQClient.createClientSessionFactory(
|
2014-10-31 06:20:28 -04:00
|
|
|
|
new TransportConfiguration(
|
|
|
|
|
InVMConnectorFactory.class.getName()));
|
|
|
|
|
|
|
|
|
|
ClientSession session = factory.createSession();
|
|
|
|
|
|
|
|
|
|
session.createQueue("example", "example", true);
|
|
|
|
|
|
|
|
|
|
ClientProducer producer = session.createProducer("example");
|
|
|
|
|
|
|
|
|
|
ClientMessage message = session.createMessage(true);
|
|
|
|
|
|
|
|
|
|
message.getBody().writeString("Hello");
|
|
|
|
|
|
|
|
|
|
producer.send(message);
|
|
|
|
|
|
|
|
|
|
session.start();
|
|
|
|
|
|
|
|
|
|
ClientConsumer consumer = session.createConsumer("example");
|
|
|
|
|
|
|
|
|
|
ClientMessage msgReceived = consumer.receive();
|
|
|
|
|
|
|
|
|
|
System.out.println("message = " + msgReceived.getBody().readString());
|
|
|
|
|
|
|
|
|
|
session.close();</programlisting>
|
|
|
|
|
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<para>The <literal>EmbeddedActiveMQ</literal> class has a
|
2014-10-31 06:20:28 -04:00
|
|
|
|
few additional setter methods that allow you to specify a different
|
|
|
|
|
config file name as well as other properties. See the javadocs for this
|
|
|
|
|
class for more details.</para>
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
<section id="simple.embedded.jms">
|
|
|
|
|
<title>JMS API</title>
|
|
|
|
|
|
|
|
|
|
<para>JMS embedding is simple as well. This example requires that you
|
|
|
|
|
have defined the config files
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<literal>activemq-configuration.xml</literal>,
|
|
|
|
|
<literal>activemq-jms.xml</literal>, and a
|
|
|
|
|
<literal>activemq-users.xml</literal> if you have security enabled. Let's
|
2014-10-31 06:20:28 -04:00
|
|
|
|
also assume that a queue and connection factory has been defined in the
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<literal>activemq-jms.xml</literal> config file.</para>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
|
|
|
|
<programlisting>
|
2014-11-17 09:23:06 -05:00
|
|
|
|
import org.apache.activemq.jms.server.embedded.EmbeddedJMS;
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
EmbeddedJMS jms = new EmbeddedJMS();
|
|
|
|
|
jms.start();
|
|
|
|
|
|
2014-11-17 22:39:49 -05:00
|
|
|
|
// This assumes we have configured activemq-jms.xml with the appropriate config information
|
2014-10-31 06:20:28 -04:00
|
|
|
|
ConnectionFactory connectionFactory = jms.lookup("ConnectionFactory");
|
|
|
|
|
Destination destination = jms.lookup("/example/queue");
|
|
|
|
|
|
|
|
|
|
... regular JMS code ...</programlisting>
|
|
|
|
|
<para>By default, the <literal>EmbeddedJMS</literal>
|
|
|
|
|
class will store component entries defined within your
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<literal>activemq-jms.xml</literal> file in an internal concurrent hash
|
2014-10-31 06:20:28 -04:00
|
|
|
|
map. The <literal>EmbeddedJMS.lookup()</literal> method returns
|
|
|
|
|
components stored in this map. If you want to use JNDI, call the
|
|
|
|
|
<literal>EmbeddedJMS.setContext()</literal> method with the root JNDI
|
|
|
|
|
context you want your components bound into. See the javadocs for this
|
|
|
|
|
class for more details on other config options.</para>
|
|
|
|
|
</section>
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
<section>
|
|
|
|
|
<title>POJO instantiation - Embedding Programmatically</title>
|
|
|
|
|
|
|
|
|
|
<para>You can follow this step-by-step guide to programmatically embed the
|
2014-11-17 22:39:49 -05:00
|
|
|
|
core, non-JMS ActiveMQ Server instance:</para>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
|
|
|
|
<para>Create the configuration object - this contains configuration
|
2014-11-17 22:39:49 -05:00
|
|
|
|
information for a ActiveMQ instance. The setter methods of this class allow
|
2014-10-31 06:20:28 -04:00
|
|
|
|
you to programmatically set configuration options as describe in the <xref
|
|
|
|
|
linkend="server.configuration" /> section.</para>
|
|
|
|
|
|
|
|
|
|
<para>The acceptors are configured through
|
|
|
|
|
<literal>ConfigurationImpl</literal>. Just add the
|
|
|
|
|
<literal>NettyAcceptorFactory</literal> on the transports the same way you
|
|
|
|
|
would through the main configuration file.</para>
|
|
|
|
|
|
|
|
|
|
<programlisting>
|
2014-11-17 09:23:06 -05:00
|
|
|
|
import org.apache.activemq.core.config.Configuration;
|
|
|
|
|
import org.apache.activemq.core.config.impl.ConfigurationImpl;
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
Configuration config = new ConfigurationImpl();
|
|
|
|
|
HashSet<TransportConfiguration> transports = new HashSet<TransportConfiguration>();
|
|
|
|
|
|
|
|
|
|
transports.add(new TransportConfiguration(NettyAcceptorFactory.class.getName()));
|
|
|
|
|
transports.add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
|
|
|
|
|
|
|
|
|
|
config.setAcceptorConfigurations(transports);</programlisting>
|
|
|
|
|
|
|
|
|
|
<para>You need to instantiate an instance of
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<literal>org.apache.activemq.api.core.server.embedded.EmbeddedActiveMQ</literal>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
and add the configuration object to it.</para>
|
|
|
|
|
|
|
|
|
|
<programlisting>
|
2014-11-17 22:39:49 -05:00
|
|
|
|
import org.apache.activemq.api.core.server.ActiveMQ;
|
|
|
|
|
import org.apache.activemq.core.server.embedded.EmbeddedActiveMQ;
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
2014-11-17 22:39:49 -05:00
|
|
|
|
EmbeddedActiveMQ server = new EmbeddedActiveMQ();
|
2014-10-31 06:20:28 -04:00
|
|
|
|
server.setConfiguration(config);
|
|
|
|
|
|
|
|
|
|
server.start();</programlisting>
|
|
|
|
|
|
|
|
|
|
<para>You also have the option of instantiating
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<literal>ActiveMQServerImpl</literal> directly:</para>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
|
|
|
|
|
<programlisting>
|
2014-11-17 22:39:49 -05:00
|
|
|
|
ActiveMQServer server = new ActiveMQServerImpl(config);
|
2014-10-31 06:20:28 -04:00
|
|
|
|
server.start();</programlisting>
|
|
|
|
|
|
|
|
|
|
<para>For JMS POJO instantiation, you work with the EmbeddedJMS class
|
|
|
|
|
instead as described earlier. First you define the configuration
|
|
|
|
|
programmatically for your ConnectionFactory and Destination objects, then
|
|
|
|
|
set the JmsConfiguration property of the EmbeddedJMS class. Here is an
|
|
|
|
|
example of this:</para>
|
|
|
|
|
|
|
|
|
|
<programlisting>
|
2014-11-17 22:39:49 -05:00
|
|
|
|
// Step 1. Create ActiveMQ core configuration, and set the properties accordingly
|
2014-10-31 06:20:28 -04:00
|
|
|
|
Configuration configuration = new ConfigurationImpl();
|
|
|
|
|
configuration.setPersistenceEnabled(false);
|
|
|
|
|
configuration.setSecurityEnabled(false);
|
|
|
|
|
configuration.getAcceptorConfigurations().add(new TransportConfiguration(NettyAcceptorFactory.class.getName()));
|
|
|
|
|
|
|
|
|
|
// Step 2. Create the JMS configuration
|
|
|
|
|
JMSConfiguration jmsConfig = new JMSConfigurationImpl();
|
|
|
|
|
|
|
|
|
|
// Step 3. Configure the JMS ConnectionFactory
|
|
|
|
|
TransportConfiguration connectorConfig = new TransportConfiguration(NettyConnectorFactory.class.getName());
|
|
|
|
|
ConnectionFactoryConfiguration cfConfig = new ConnectionFactoryConfigurationImpl("cf", connectorConfig, "/cf");
|
|
|
|
|
jmsConfig.getConnectionFactoryConfigurations().add(cfConfig);
|
|
|
|
|
|
|
|
|
|
// Step 4. Configure the JMS Queue
|
|
|
|
|
JMSQueueConfiguration queueConfig = new JMSQueueConfigurationImpl("queue1", null, false, "/queue/queue1");
|
|
|
|
|
jmsConfig.getQueueConfigurations().add(queueConfig);
|
|
|
|
|
|
2014-11-17 22:39:49 -05:00
|
|
|
|
// Step 5. Start the JMS Server using the ActiveMQ core server and the JMS configuration
|
2014-10-31 06:20:28 -04:00
|
|
|
|
EmbeddedJMS jmsServer = new EmbeddedJMS();
|
|
|
|
|
jmsServer.setConfiguration(configuration);
|
|
|
|
|
jmsServer.setJmsConfiguration(jmsConfig);
|
|
|
|
|
jmsServer.start();</programlisting>
|
|
|
|
|
|
|
|
|
|
<para>Please see <xref linkend="examples.embedded.jms" /> for an example which
|
2014-11-17 22:39:49 -05:00
|
|
|
|
shows how to setup and run ActiveMQ embedded with JMS.</para>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
<section>
|
|
|
|
|
<title>Dependency Frameworks</title>
|
|
|
|
|
|
|
|
|
|
<para>You may also choose to use a dependency injection framework such as
|
|
|
|
|
<trademark>JBoss Micro Container</trademark> or <trademark>Spring
|
|
|
|
|
Framework</trademark>. See <xref linkend="spring.integration" /> for more
|
2014-11-17 22:39:49 -05:00
|
|
|
|
details on Spring and ActiveMQ, but here's how you would do things with the
|
2014-10-31 06:20:28 -04:00
|
|
|
|
JBoss Micro Container.</para>
|
|
|
|
|
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<para>ActiveMQ standalone uses JBoss Micro Container as the injection
|
|
|
|
|
framework. <literal>ActiveMQBootstrapServer</literal> and
|
|
|
|
|
<literal>activemq-beans.xml</literal> which are part of the ActiveMQ
|
2014-10-31 06:20:28 -04:00
|
|
|
|
distribution provide a very complete implementation of what's needed to
|
|
|
|
|
bootstrap the server using JBoss Micro Container.</para>
|
|
|
|
|
|
|
|
|
|
<para>When using JBoss Micro Container, you need to provide an XML file
|
2014-11-17 22:39:49 -05:00
|
|
|
|
declaring the <literal>ActiveMQServer</literal> and
|
2014-10-31 06:20:28 -04:00
|
|
|
|
<literal>Configuration</literal> object, you can also inject a security
|
|
|
|
|
manager and a MBean server if you want, but those are optional.</para>
|
|
|
|
|
|
|
|
|
|
<para>A very basic XML Bean declaration for the JBoss Micro Container
|
|
|
|
|
would be:</para>
|
|
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
<deployment xmlns="urn:jboss:bean-deployer:2.0">
|
|
|
|
|
<!-- The core configuration -->
|
|
|
|
|
<bean name="Configuration"
|
2014-11-17 09:23:06 -05:00
|
|
|
|
class="org.apache.activemq.core.config.impl.FileConfiguration">
|
2014-10-31 06:20:28 -04:00
|
|
|
|
</bean>
|
|
|
|
|
|
|
|
|
|
<!-- The core server -->
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<bean name="ActiveMQServer"
|
|
|
|
|
class="org.apache.activemq.core.server.impl.ActiveMQServerImpl">
|
2014-10-31 06:20:28 -04:00
|
|
|
|
<constructor>
|
|
|
|
|
<parameter>
|
|
|
|
|
<inject bean="Configuration"/>
|
|
|
|
|
</parameter>
|
|
|
|
|
</constructor>
|
|
|
|
|
</bean>
|
|
|
|
|
</deployment></programlisting>
|
|
|
|
|
|
2014-11-17 22:39:49 -05:00
|
|
|
|
<para><literal>ActiveMQBootstrapServer</literal> provides an easy
|
2014-10-31 06:20:28 -04:00
|
|
|
|
encapsulation of JBoss Micro Container.</para>
|
|
|
|
|
|
|
|
|
|
<programlisting>
|
2014-11-17 22:39:49 -05:00
|
|
|
|
ActiveMQBootstrapServer bootStrap = new ActiveMQBootstrapServer(new String[] {"activemq-beans.xml"});
|
2014-10-31 06:20:28 -04:00
|
|
|
|
bootStrap.run();</programlisting>
|
|
|
|
|
</section>
|
|
|
|
|
</chapter>
|