activemq-artemis/docs/user-manual/zh/embedding-hornetq.xml

179 lines
8.6 KiB
XML
Raw Normal View History

<?xml version="1.0" encoding="UTF-8"?>
<!-- ============================================================================= -->
<!-- Copyright © 2009 Red Hat, Inc. and others. -->
<!-- -->
<!-- The text of and illustrations in this document are licensed by Red Hat under -->
<!-- a Creative Commons AttributionShare 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. -->
<!-- ============================================================================= -->
<chapter id="embedding-activemq">
<title>嵌入式ActiveMQ</title>
<para>ActiveMQ是由简单传统Java对象POJO实现因此它可以在任何依赖注入的框架中运行比如JBoss
MicrocontainerSprint或Google Guice。另外如果你的应用程序内部需要消息功能你可以在程序中
<emphasis>直接实例化</emphasis>ActiveMQ的客户端或服务器端。我们称之为<emphasis>嵌入式</emphasis>
ActiveMQ。</para>
<para>有些应用需要高性能、事务性及持久化的消息服务但是又不希望自己去费时费力实现它。于是嵌入式ActiveMQ就
成为了一个很适当的选择。</para>
<para>要使用嵌入式ActiveMQ只需要几个简单的步骤。首先初始化配置对象再初始化服务器并启动它在你的虚拟机
中就运行越来了一个ActiveMQ服务器。就是这么简单。</para>
<section>
<title>POJO的初始化</title>
<para>按照以下步骤去做:</para>
<para>创建配置对象这个对象包装了ActiveMQ的配置信息。如果你想使用配置文件则使用<literal
>FileConfigurationImpl</literal></para>
<programlisting>import org.apache.activemq.core.config.Configuration;
import org.apache.activemq.core.config.impl.FileConfiguration;
...
Configuration config = new FileConfiguration();
config.setConfigurationUrl(urlToYourconfigfile);
config.start();</programlisting>
<para>如果不需要配置文件,可以用<literal>ConfigurationImpl</literal>,只要将其中的各种配置参数设置好
即可。如添加适当的接收器。</para>
<para><literal>ConfigurationImpl</literal>用来配置接收器。和主要配置文件相似,只需要添加
<literal>NettyAcceptorFactory</literal>即可。</para>
<programlisting>import org.apache.activemq.core.config.Configuration;
import org.apache.activemq.core.config.impl.ConfigurationImpl;
...
Configuration config = new ConfigurationImpl();
HashSet&lt;TransportConfiguration> transports = new HashSet&lt;TransportConfiguration>();
transports.add(new TransportConfiguration(NettyAcceptorFactory.class.getName()));
transports.add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
config.setAcceptorConfigurations(transports);</programlisting>
<para>接着就需要初始化并启动ActiveMQ服务。<literal
>org.apache.activemq.api.core.server.ActiveMQ</literal>类有一些静态方法可用来创建ActiveMQ服务器。</para>
<programlisting>import org.apache.activemq.api.core.server.ActiveMQ;
import org.apache.activemq.core.server.ActiveMQServer;
...
ActiveMQServer server = ActiveMQ.newActiveMQServer(config);
server.start();</programlisting>
<para>你还可以直接实例化<literal>ActiveMQServerImpl</literal></para>
<programlisting>ActiveMQServer server =
new ActiveMQServerImpl(config);
server.start();</programlisting>
</section>
<section>
<title>使用依赖注入框架</title>
<para>你还可以使用一个依赖注入框架来启动ActiveMQ比如<trademark>JBoss
Microcontainer</trademark><trademark>Spring框架</trademark></para>
<para>ActiveMQ独立服务器使用的是JBoss Microcontainer作为其框架。在ActiveMQ的发布中包括的<literal
>ActiveMQBootstrapServer</literal><literal>activemq-beans.xml</literal>文件共同实现了
在JBoss Microcontainer中对ActiveMQ服务器的引导。</para>
<para>要使用JBoss Microcontainer需要在xml文件中声明<literal>ActiveMQServer</literal>
<literal>Configuration</literal>对象。另外还可以注入一个安全管理器和一个MBean服务器。但是这些
注入是可选的。</para>
<para>下面是一个基本的JBoss Microcontainer的XML Bean的声明</para>
<programlisting>&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;deployment xmlns="urn:jboss:bean-deployer:2.0">
&lt;!-- The core configuration -->
&lt;bean name="Configuration"
class="org.apache.activemq.core.config.impl.FileConfiguration">
&lt;/bean>
&lt;!-- The core server -->
&lt;bean name="ActiveMQServer"
class="org.apache.activemq.core.server.impl.ActiveMQServerImpl">
&lt;constructor>
&lt;parameter>
&lt;inject bean="Configuration"/>
&lt;/parameter>
&lt;/constructor>
&lt;/bean>
&lt;/deployment></programlisting>
<para><literal>ActiveMQBootstrapServer</literal>实现了JBoss Microcontainer的简单封装。</para>
<programlisting>ActiveMQBootstrapServer bootStrap =
new ActiveMQBootstrapServer(new String[] {"activemq-beans.xml"});
bootStrap.run();</programlisting>
</section>
<section>
<title>连接嵌入式ActiveMQ</title>
<para>嵌入式ActiveMQ的连接和普通的连接一样要创建连接工厂</para>
<section>
<title>核心接口</title>
<para>使用核心接口,需要创建一个<literal>ClientSessionFactory</literal>然后正常建立连接。</para>
<programlisting>ClientSessionFactory nettyFactory = ActiveMQClient.createClientSessionFactory(
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>
</section>
<section>
<title>JMS接口</title>
<para>使用JMS接口连接嵌入ActiveMQ同样简单。只需要直接实例化
<literal>ConnectionFactory</literal>即可。如下面例子所示:</para>
<programlisting>ConnectionFactory cf =
ActiveMQJMSClient.createConnectionFactory(
new TransportConfiguration(InVMConnectorFactory.class.getName()));
Connection conn = cf.createConnection();
conn.start();
Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer prod = sess.createProducer(queue);
TextMessage msg = sess.createTextMessage("Hello!");
prod.send(msg);
sess.commit();
MessageConsumer consumer = sess.createConsumer(queue);
TextMessage txtmsg = (TextMessage)consumer.receive();
System.out.println("Msg = " + txtmsg.getText());
sess.commit();
conn.close();</programlisting>
</section>
</section>
<section>
<title>JMS嵌入式ActiveMQ的例子</title>
<para>有关如何设置与运行JMS嵌入式ActiveMQ的例子请参见<xref linkend="examples.embedded"/></para>
</section>
</chapter>