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

179 lines
8.5 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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-hornetq">
<title>嵌入式HornetQ</title>
<para>HornetQ是由简单传统Java对象POJO实现因此它可以在任何依赖注入的框架中运行比如JBoss
MicrocontainerSprint或Google Guice。另外如果你的应用程序内部需要消息功能你可以在程序中
<emphasis>直接实例化</emphasis>HornetQ的客户端或服务器端。我们称之为<emphasis>嵌入式</emphasis>
HornetQ。</para>
<para>有些应用需要高性能、事务性及持久化的消息服务但是又不希望自己去费时费力实现它。于是嵌入式HornetQ就
成为了一个很适当的选择。</para>
<para>要使用嵌入式HornetQ只需要几个简单的步骤。首先初始化配置对象再初始化服务器并启动它在你的虚拟机
中就运行越来了一个HornetQ服务器。就是这么简单。</para>
<section>
<title>POJO的初始化</title>
<para>按照以下步骤去做:</para>
<para>创建配置对象这个对象包装了HornetQ的配置信息。如果你想使用配置文件则使用<literal
>FileConfigurationImpl</literal></para>
<programlisting>import org.hornetq.core.config.Configuration;
import org.hornetq.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.hornetq.core.config.Configuration;
import org.hornetq.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>接着就需要初始化并启动HornetQ服务。<literal
>org.hornetq.api.core.server.HornetQ</literal>类有一些静态方法可用来创建HornetQ服务器。</para>
<programlisting>import org.hornetq.api.core.server.HornetQ;
import org.hornetq.core.server.HornetQServer;
...
HornetQServer server = HornetQ.newHornetQServer(config);
server.start();</programlisting>
<para>你还可以直接实例化<literal>HornetQServerImpl</literal></para>
<programlisting>HornetQServer server =
new HornetQServerImpl(config);
server.start();</programlisting>
</section>
<section>
<title>使用依赖注入框架</title>
<para>你还可以使用一个依赖注入框架来启动HornetQ比如<trademark>JBoss
Microcontainer</trademark><trademark>Spring框架</trademark></para>
<para>HornetQ独立服务器使用的是JBoss Microcontainer作为其框架。在HornetQ的发布中包括的<literal
>HornetQBootstrapServer</literal><literal>hornetq-beans.xml</literal>文件共同实现了
在JBoss Microcontainer中对HornetQ服务器的引导。</para>
<para>要使用JBoss Microcontainer需要在xml文件中声明<literal>HornetQServer</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.hornetq.core.config.impl.FileConfiguration">
&lt;/bean>
&lt;!-- The core server -->
&lt;bean name="HornetQServer"
class="org.hornetq.core.server.impl.HornetQServerImpl">
&lt;constructor>
&lt;parameter>
&lt;inject bean="Configuration"/>
&lt;/parameter>
&lt;/constructor>
&lt;/bean>
&lt;/deployment></programlisting>
<para><literal>HornetQBootstrapServer</literal>实现了JBoss Microcontainer的简单封装。</para>
<programlisting>HornetQBootstrapServer bootStrap =
new HornetQBootstrapServer(new String[] {"hornetq-beans.xml"});
bootStrap.run();</programlisting>
</section>
<section>
<title>连接嵌入式HornetQ</title>
<para>嵌入式HornetQ的连接和普通的连接一样要创建连接工厂</para>
<section>
<title>核心接口</title>
<para>使用核心接口,需要创建一个<literal>ClientSessionFactory</literal>然后正常建立连接。</para>
<programlisting>ClientSessionFactory nettyFactory = HornetQClient.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接口连接嵌入HornetQ同样简单。只需要直接实例化
<literal>ConnectionFactory</literal>即可。如下面例子所示:</para>
<programlisting>ConnectionFactory cf =
HornetQJMSClient.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嵌入式HornetQ的例子</title>
<para>有关如何设置与运行JMS嵌入式HornetQ的例子请参见<xref linkend="examples.embedded"/></para>
</section>
</chapter>