Chapter 32. Java EE和应用服务器的集成

HornetQ可以容易地安装到JBoss 4应用服务器及其以上版本。有关安装的详细说明请参阅快速指南。

HornetQ提供了一个JCA适配器,使得它还可以与其它JEE兼容的应用服务器集成。请参阅其它应用服务器的 有关JCA适配器集成的说明来操作。

JCA适配器的作用是控制消息流入到消息Bean(MDB),并控制消息从各种JEE模块中发出(如EJB和Servlet)。

本章讲述这些JEE模块配置HornetQ的基本信息。

32.1. 配置消息Bean

使用HornetQ向MDB传递消息,需要在文件ra.xml中配置JCA适配器。该文件在 jms-ra.rar文件中。默认配置下HornetQ服务使用InVm连接器接收消息。在本章 后面列出了可配置的选项。

所有MDB都需要有目标类型和目标的相关配置。下面就是一个使用annotation配置MDB的例子:

@MessageDriven(name = "MDBExample",
               activationConfig =
                     {
                        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue")
                     })
@ResourceAdapter("hornetq-ra.rar")
public class MDBExample implements MessageListener
{
   public void onMessage(Message message)...
}

上例中配置了MDB从一个队列中接收消息,它的JNDI绑定名为queue/testQueue。 这个队列必须事先在HornetQ服务器配置文件中配置并部署好的。

ResourceAdapter annotation用来指出使用哪个适配器。要使用它必须要引入 org.jboss.ejb3.annotation.ResourceAdapter (JBoss AS 5.x或以上)。 这个类在 jboss-ejb3-ext-api.jar文件中。该文件可以在JBoss的repository中找到。 另外一个方法是使用部署描述文件(deployment descriptor),即在文件jboss.xml中加入类似以下的内容:

<message-driven>
   <ejb-name>ExampleMDB</ejb-name>
   <resource-adapter-name>hornetq-ra.rar</resource-adapter-name>
</message-driven>

你还可以将hornetq-ra.rar改名为jms-ra.rar而不需要任何annotation或额外的部署描述信息。但是你需要 编辑jms-ds.xml文件,将其中的rar-name项改成相应的值。

Note

HornetQ是JBoss AS 6默认的JMS提供者。从这个版本开始HornetQ的资源适配器名字是 jms-ra.rar,并且你不需要在MDB的annotation中指定它。

HornetQ发布包中的所有例子都使用annotation方法。

32.1.1. 使用容器管理事务(CMT)

当MDB使用容器管理事务时,消息的传递被包含在一个JTA事务中。事务的提交与回滚是由容器来控制的。如果事务 被回滚,消息传递会进行相应的处理(默认是重新传递消息,如果重新传递次数超过10次,消息就被发往DLQ)。使用 annotation配置如下:

@MessageDriven(name = "MDB_CMP_TxRequiredExample",
               activationConfig =
                     {
                        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue")
                     })
@TransactionManagement(value= TransactionManagementType.CONTAINER)
@TransactionAttribute(value= TransactionAttributeType.REQUIRED)
@ResourceAdapter("hornetq-ra.rar")
public class MDB_CMP_TxRequiredExample implements MessageListener
{
   public void onMessage(Message message)...
}

TransactionManagement 表示这个MDB使用容器管理持久性。 TransactionAttribute 表示这个MDB要求JTA事务。注意这个annotation的 另外唯一的合法值是TransactionAttributeType.NOT_SUPPORTED,它表示 MDB不需要JTA事务支持。

如果要回滚事务,可以调用MessageDrivenContextsetRollbackOnly方法。如下面的代码所示:

   @Resource
   MessageDrivenContextContext ctx;

   public void onMessage(Message message)
   {
      try
      {
         //something here fails
      }
      catch (Exception e)
      {
         ctx.setRollbackOnly();
      }
   }

如果你不需要使用XA事务,你可以用一相本地的事务来代替(比如你只有一个JMS资源)。 如下所示:

@MessageDriven(name = "MDB_CMP_TxLocalExample",
               activationConfig =
                     {
                           @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                           @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue"),
                           @ActivationConfigProperty(propertyName = "useLocalTx", propertyValue = "true")
                     })
@TransactionManagement(value = TransactionManagementType.CONTAINER)
@TransactionAttribute(value = TransactionAttributeType.NOT_SUPPORTED)
@ResourceAdapter("hornetq-ra.rar")
public class MDB_CMP_TxLocalExample implements MessageListener
{
   public void onMessage(Message message)...
}

32.1.2. 使用Bean管理事务(BMT)

消息Bean可以通过配置使用Bean管理的事务(BMT)。在种情况下会创建一个用户事务 (User Transaction)。如下所示:

@MessageDriven(name = "MDB_BMPExample",
               activationConfig =
                     {
                        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue"),
                        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Dups-ok-acknowledge")
                     })
@TransactionManagement(value= TransactionManagementType.BEAN)
@ResourceAdapter("hornetq-ra.rar")
public class MDB_BMPExample implements MessageListener
{
   public void onMessage(Message message)
}

使用BMT时,消息的传递在用户事务的范围之外,它的通知模式由acknowledgeMode参数决定。 该参数有两个合法的值,即Auto-acknowledgeDups-ok-acknowledge。请注意,由于消息的传递在事务之外,在MDB中如果发生错误消息 是不会重新传递的。

用户可以像如下所示控制事务的生命周期:

   @Resource
   MessageDrivenContext ctx;

   public void onMessage(Message message)
   {
      UserTransaction tx;
      try
      {
         TextMessage textMessage = (TextMessage)message;

         String text = textMessage.getText();

         UserTransaction tx = ctx.getUserTransaction();

         tx.begin();
         
         //do some stuff within the transaction

         tx.commit();

      }
      catch (Exception e)
      {
         tx.rollback();
      }
   }

32.1.3. 在MDB中使用选择器

MDB可以配置消息选择器。如下所示:

@MessageDriven(name = "MDBMessageSelectorExample",
               activationConfig =
                     {
                        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue"),
                        @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "color = 'RED'")
                     })
@TransactionManagement(value= TransactionManagementType.CONTAINER)
@TransactionAttribute(value= TransactionAttributeType.REQUIRED)
@ResourceAdapter("hornetq-ra.rar")
public class MDBMessageSelectorExample implements MessageListener
{
   public void onMessage(Message message)....
}

32.2. 在JEE模块内发送消息

JCA适配器支持发送消息。连接工厂的默认配置在jms-ds.xml文件中,对应的JNDI名字是 java:/JmsXA。在JEE中使用它发送消息将作为JTA事务的一部分来对待。

如果消息发送失败,整个事务将回滚,消息会被重新发送。下面是一个MDB发送消息的例子:

@MessageDriven(name = "MDBMessageSendTxExample",
               activationConfig =
                     {
                        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue")
                     })
@TransactionManagement(value= TransactionManagementType.CONTAINER)
@TransactionAttribute(value= TransactionAttributeType.REQUIRED)
@ResourceAdapter("hornetq-ra.rar")
public class MDBMessageSendTxExample implements MessageListener
{
   @Resource(mappedName = "java:/JmsXA")
   ConnectionFactory connectionFactory;

   @Resource(mappedName = "queue/replyQueue")
   Queue replyQueue;

   public void onMessage(Message message)
   {
      Connection conn = null;
      try
      {
         //Step 9. We know the client is sending a text message so we cast
         TextMessage textMessage = (TextMessage)message;

         //Step 10. get the text from the message.
         String text = textMessage.getText();

         System.out.println("message " + text);

         conn = connectionFactory.createConnection();

         Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

         MessageProducer producer = sess.createProducer(replyQueue);

         producer.send(sess.createTextMessage("this is a reply"));

      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      finally
      {
         if(conn != null)
         {
            try
            {
               conn.close();
            }
            catch (JMSException e)
            { 
            }
         }
      }
   }
   }

在JBoss应用服务器的EJB(包括会话Bean, 实体Bean和消息Bean)、Servlet(包括jsp)我定制的MBean中 都可以使用JMS的JCA适配器来发送消息。

32.3. MDB与接收池的大小

包括JBoss在内的绝大多数应用服务器允许用户定义一个池中的MDB数量。在JBoss中这个参数名是MaxPoolSize,它在文件 ejb3-interceptors-aop.xml中配置。这个参数不影响实际创建的会话/接收者的数量。资源适配器不知道MDB的具体实现。所以即使你设置MDB池在大小 为1,仍然会有15个会话/接收者被创建(默认值)。如果你想改变会话/接收者创建的数量,你可以通过设置资源适配器的参数 maxSession实现。也可以通过设置MDB的激活配置参数来完成:

@MessageDriven(name = "MDBMessageSendTxExample",
               activationConfig =
                     {
                        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue"),
                        @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "1")
                     })
@TransactionManagement(value= TransactionManagementType.CONTAINER)
@TransactionAttribute(value= TransactionAttributeType.REQUIRED)
@ResourceAdapter("hornetq-ra.rar")
public class MyMDB implements MessageListener
{ ....}
      

32.4. 配置JCA适配器

通过JCA适配器可以将HornetQ集成到JEE兼容的模块中,如MDB和EJB。它的配置决定了这些模块如何接收和发送消息。

HornetQ的JCA适配器是通过jms-ra.rar部署的。它的配置文件中其中的 META-INF/ra.xml

下面是它的具体配置内容:

<resourceadapter>
      <resourceadapter-class>org.hornetq.ra.HornetQResourceAdapter</resourceadapter-class>
      <config-property>
         <description>The transport type</description>
         <config-property-name>ConnectorClassName</config-property-name>
         <config-property-type>java.lang.String</config-property-type>
         <config-property-value>org.hornetq.core.remoting.impl.invm.InVMConnectorF
         actory</config-property-value>
      </config-property>
      <config-property>
         <description>The transport configuration. These values must be in the form of key=val;key=val;</description>
         <config-property-name>ConnectionParameters</config-property-name>
         <config-property-type>java.lang.String</config-property-type>
         <config-property-value>server-id=0</config-property-value>
      </config-property>

      <outbound-resourceadapter>
         <connection-definition>
            <managedconnectionfactory-class>org.hornetq.ra.HornetQRAManagedConnection
            Factory</managedconnectionfactory-class>

            <config-property>
               <description>The default session type</description>
               <config-property-name>SessionDefaultType</config-property-name>
               <config-property-type>java.lang.String</config-property-type>
               <config-property-value>javax.jms.Queue</config-property-value>
            </config-property>
            <config-property>
               <description>Try to obtain a lock within specified number of seconds; less
               than or equal to 0 disable this functionality</description>
               <config-property-name>UseTryLock</config-property-name>
               <config-property-type>java.lang.Integer</config-property-type>
               <config-property-value>0</config-property-value>
            </config-property>

            <connectionfactory-interface>org.hornetq.ra.HornetQRAConnectionFactory
            </connectionfactory-interface>
            <connectionfactororg.hornetq.ra.HornetQConnectionFactoryImplonFactoryImpl
            </connectionfactory-impl-class>
            <connection-interface>javax.jms.Session</connection-interface>
            <connection-impl-class>org.hornetq.ra.HornetQRASession
            </connection-impl-class>
         </connection-definition>
         <transaction-support>XATransaction</transaction-support>
         <authentication-mechanism>
            <authentication-mechanism-type>BasicPassword
            </authentication-mechanism-type>
            <credential-interface>javax.resource.spi.security.PasswordCredential
            </credential-interface>
         </authentication-mechanism>
         <reauthentication-support>false</reauthentication-support>
      </outbound-resourceadapter>

      <inbound-resourceadapter>
         <messageadapter>
            <messagelistener>
               <messagelistener-type>javax.jms.MessageListener</messagelistener-type>
               <activationspec>
                  <activationspec-class>org.hornetq.ra.inflow.HornetQActivationSpec
                  </activationspec-class>
                  <required-config-property>
                      <config-property-name>destination</config-property-name>
                  </required-config-property>
               </activationspec>
            </messagelistener>
         </messageadapter>
      </inbound-resourceadapter>

   </resourceadapter>

整个配置可以分为三个主要部分

  1. 适配器的全局参数

  2. 适配器外部(outbound)配置。用于在JEE环境中创建JMS资源。

  3. 适配器内部(inbound)配置。用于控制MDB的消息接收。

32.4.1. 适配器的全局参数

首先看到的第一个参数是resourceadapter-class。这是HornetQ 的适配器类。此参数不可更改。

在此之后是可配置的参数。前两个配置适配器所使用的传输。其余的用来配置连接工厂。

Note

所有连接工厂的参数在没有定义时使用默认值。参数reconnectAttempts 的默认值取-1,表示如果连接失败,HornetQ会不停地尝试重新连接。这个参数只适用于创建远程 连接的情况。如果是InVm连接器,则永远不可能发生连接故障。

下面给出了每个参数的说明:

Table 32.1. 全局配置参数

参数名参数类型参数说明
ConnectorClassNameString连接器的类名,参见 Chapter 16, 传输层的配置
ConnectionParametersString传输配置参数。它的值必须是采用 key1=val1;key2=val2;的形式。不同连接器有不同的参数。
useLocalTxboolean设为True,则进行本地事务优化。
UserNameString用于创建连接时使用的用户名
PasswordString用于创建连接时使用的用户密码
BackupConnectorClassNameString发生故障是使用的备份传输
BackupConnectionParametersString备份传输的配置参数
DiscoveryAddressString服务器自动检测所使用的发现组(discovery group)地址
DiscoveryPortInteger检测所使用的端口号
DiscoveryRefreshTimeoutLong刷新的时间(timeout)。单位为毫秒
DiscoveryInitialWaitTimeout Long检测之前所需等待的时间
ConnectionLoadBalancingPolicyClassNameString负载均衡策略使用的类
DiscoveryInitialWaitTimeout Long向服务器发送ping的周期,单位毫秒
ConnectionTTLLong连接的存活时间(TTL)
CallTimeoutLong每个数据包的调用超时。单位毫秒
DupsOKBatchSizeIntegerDups OK的情况下消息批量的大小。
TransactionBatchSizeInteger在事务中发送消息的批量大小
ConsumerWindowSizeInteger接收者内部缓存的窗口大小

接上页..

ConsumerMaxRateInteger接收者接收消息的最大速度
ConfirmationWindowSizeInteger用于确认的窗口大小
ProducerMaxRateInteger发送者发送消息的最大速度
MinLargeMessageSizeInteger大消息的最小数值,单位字节。
BlockOnAcknowledgeBoolean如果为true,表示以阻塞方法发送消息通知。
BlockOnNonDurableSendBoolean如果为true,表示以阻塞方式发送非持久消息
BlockOnDurableSendBoolean如果为true,表示以阻塞方式发送持久消息
AutoGroupBoolean如果为true,表示自动消息分组
PreAcknowledgeBoolean决定是否进行消息的预先通知(pre-acknowledge)。
ReconnectAttemptsInteger连接重试的次数,默认为 -1
RetryIntervalLong每次连接重试前等待的时间,单位毫秒。
RetryIntervalMultiplierDouble用于计算重试间隔
FailoverOnServerShutdownBoolean如果设为true表示尝试连接其它的服务器
ClientIDString连接的客户端ID
ClientFailureCheckPeriodLong客户端如果在这个时间内没有收到服务器数据包,将认为连接出现故障。
UseGlobalPoolsBoolean是否使用全局线程池
ScheduledThreadPoolMaxSizeInteger可计划线程池的最大线程数
ThreadPoolMaxSizeInteger线程池的大小

32.4.2. 适配器外部(outbound)配置

外部配置参数应该保持不变。这是因为它所定义的连接工厂要被Java EE的模块所使用。这些连接工厂 可以定义在名字样式为*-ds.xml的配置文件中。在JBoss的部署目录 hornetq.sar下有一个默认的配置文件jms-ds.xml。 在这个文件中的连接工厂的配置从主要的配置文件ra.xml中继承, 但是可以在这里重新定义。下面的例子中给出了重新定义的方法。

Note

请注意这里的配置只适用于在JBoss应用服务器中安装的HornetQ。如果要在其它JEE应用服务器中 使用并配置HornetQ,请参照相应的应用服务器手册。

<tx-connection-factory>
      <jndi-name>RemoteJmsXA</jndi-name>
      <xa-transaction/>
      <rar-name>jms-ra.rar</rar-name>
      <connection-definition>org.hornetq.ra.HornetQRAConnectionFactory
</connection-definition>
      <config-property name="SessionDefaultType" type="String">javax.jms.Topic
      </config-property>
      <config-property name="ConnectorClassName" type="String">
        org.hornetq.core.remoting.impl.netty.NettyConnectorFactory
      </config-property>
      <config-property name="ConnectionParameters" type="String">
          port=5445</config-property>
      <max-pool-size>20</max-pool-size>
</tx-connection-factory>

上面的例子中的连接工厂绑定到JNDI名字RemoteJmsXA。EJB和MDB可以用 下面的方法来得到它:

@Resource(mappedName="java:RemoteJmsXA")
private ConnectionFactory connectionFactory;

config-property覆盖了ra.xml文件中的配置。 以此类推,其它有关连接工厂的参数也可以在此覆盖。

除了全局的配置参数外,外部的配置还定义了其它一些参数。

Table 32.2. 外部配置参数

参数名参数类型说明
SessionDefaultTypeString默认的会话类型
UseTryLockInteger在规定的秒数内获得锁。如果不想使用这个功能,将它设为0或负数

32.4.3. 适配器内部(inbound)配置

内部配置参数也应该保持不变。它们控制向MDB转发消息的各种属性。通过在MDB上添加相应的激活配置 (activation configuration)可以覆盖这些参数的值。它可以用来配置一个MDB从另外一个服务器 接收消息。

除了全局的配置参数外,内部的配置还定义了其它一些参数。

Table 32.3. Inbound Configuration Properties

参数名参数类型说明
DestinationString目标的JNDI名字
DestinationTypeString目标的类型,javax.jms.Queue或者javax.jms.Topic (默认是javax.jms.Queue)
AcknowledgeModeString通知模式,Auto-acknowledgeDups-ok-acknowledge (默认值是Auto-acknowledge). AUTO_ACKNOWLEDGEDUPS_OK_ACKNOWLEDGE也是有效值
MaxSessionInteger内部配置创建的最大会话数(默认是5)
MessageSelectorString接收者的消息选择器
SubscriptionDurabilityString订阅的类型,Durable或者NonDurable
SubscriptionNameString订阅的名字
TransactionTimeoutLong事务超时(毫秒,默认是0,表示事务不会超时)
UseJNDIBoolean是否使用JNDI来查找目标(默认是true)

32.4.4. 配置适配器访问单独的HornetQ服务器

在有的情况下,消息服务器与应用服务器运行在不同的机器上。这时客户端只需要安装HornetQ的客户端的库文件。本节给出了具体的配置和所需要的jar文件。

你需要配置两个配置文件。一个是MDB所用的内部适配器,另一个是可以创建外部连接的外部适配器。

32.4.4.1.1. 配置内部适配器

首先在deploy目录下创建一个以.rar为结尾的文件夹。 在这里我们将其命名为hornetq-ra.rar。主注意这一点很重要,因为MDB和外部配置都需要引用 该文件夹的名字。

Note

在JBoss中该文件夹的默认名为jms-ra.rar。你可以直接使用这个名字。但是你可能需要避免 与其它的通用适配器相冲突。

在文件夹 hornetq-ra.rar 下创建名为 META-INF 的文件夹,然后在些文件夹内创建一个 ra.xml配置文件。在HornetQ发布包中 有一个ra.xml模板文件供你使用。

要配置MDB接收远程HornetQ服务器的消息,你需要修改deploy/hornet-ra.rar/META-INF下面的 ra.xml文件,将传输层改为netty连接器(不要使用invm连接器)及其相应的参数。下面 是一个配置的例子:

            <resourceadapter-class>org.hornetq.ra.HornetQResourceAdapter</resourceadapter-class>
              <config-property>
                 <description>The transport type</description>
                 <config-property-name>ConnectorClassName</config-property-name>
                 <config-property-type>java.lang.String</config-property-type>
                 <config-property-value>org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</config-property-value>
              </config-property>
              <config-property>
                 <description>The transport configuration. These values must be in the form of key=val;key=val;</description>
                 <config-property-name>ConnectionParameters</config-property-name>
                 <config-property-type>java.lang.String</config-property-type>
                 <config-property-value>host=127.0.0.1;port=5446</config-property-value>
              </config-property>
          

上面的配置中,适配器连接到一个运行在本机上端口为5446的服务器。

32.4.4.1.2. 配置外部适配器

你还需要创建一个hornetq-ds.xml文件来配置外部连接。该文件需要放置在deploy 下的任意一个文件夹中。在一相标准的安装中这个文件夹是horneq 或者 hornetq.sar。 当然你可以选择其它文件夹。该文件的名字只要是以-ds.xml即可。在HornetQ的发布包中包含有一个模板文件, 它的名字是jms-ds.xml,位置就在config文件夹下。

下面是一个配置的例子。

             <tx-connection-factory>
              <jndi-name>RemoteJmsXA</jndi-name>
              <xa-transaction/>
              <rar-name>hornetq-ra.rar</rar-name>
              <connection-definition>org.hornetq.ra.HornetQRAConnectionFactory</connection-definition>
              <config-property name="SessionDefaultType" type="java.lang.String">javax.jms.Topic</config-property>
              <config-property name="ConnectorClassName" type="java.lang.String">org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</config-property>
              <config-property name="ConnectionParameters" type="java.lang.String">host=127.0.0.1;port=5446</config-property>
              <max-pool-size>20</max-pool-size>
           </tx-connection-factory>
          

这个配置同样是连接到运行在本机上的端口为5446的HornetQ服务器。JEE模块可以通过JNDI查找 java:/RemoteJmsXA(由jndi-name参数定义)。如上节所述, 资源适配器根据文件夹下的配置来创建外部连接。

32.4.4.1.2.1. 依赖的jar文件

下面列出了HornetQ所需要的第三方jar文件

Table 32.4. Jar文件

Jar文件名说明位置
hornetq-ra.jarHornetQ资源适配器文件deploy/hornetq-ra.rar或相应的位置
hornetq-core-client.jarHornetQ核心客户类库在JBoss的default/lib下或者是$JBOSS_HOME/common/lib下。
hornetq-jms-client.jarHornetQ的JMS类同上
netty.jarNetty类库同上

32.4.5. 高可获得性JNDI (HA-JNDI)

采用JNDI来查找JMS对象时(队列,话题及连接工厂),使用HA-JNDI可以增加容错的能力。即当你正在使用 的JNDI服务器发生故障时,客户端可以使用集群中的其它JNDI服务器继续工作。

HA-JNDI是JBoss应用服务器的一项服务,它为客户端提供JNDI服务,客户端无需知道JNDI具体服务器的连接 细节。这个服务只有在集群的JBoss应用服务器上才可使用。

要使用HA-JNDI,需要使用下面的JNDI参数。

Hashtable<String, String> jndiParameters = new Hashtable<String, String>();
jndiParameters.put("java.naming.factory.initial", 
    "org.jnp.interfaces.NamingContextFactory");
jndiParameters.put("java.naming.factory.url.pkgs=", 
    "org.jboss.naming:org.jnp.interfaces");

initialContext = new InitialContext(jndiParameters);

有关HA-JNDI更多的信息请参见JBoss应用服务器集群文档

32.4.6. XA恢复

XA恢复解决的是事务在系统或应用出现故障时的处理。它可以保证在应用进程或主机出现故障 或网络崩溃等情况下,事务内所有资源的状态的一致性。有关XA恢复的更多信息请见 JBoss 事务

当HornetQ与JBoss应用服务器集成时,它可以利用JBoss的事务处理来对消息资源进行恢复。如果消息处理包括 在一个XA事务中,如果服务器出现故障并重启时,恢复管理器将负责恢复事务,这样其中的消息要么被提交,要么被回滚(取 决于事务的处理决定)。

32.4.6.1. XA恢复的配置

要想HornetQ具有XA恢复功能,则必须配置恢复管理器连接到HornetQ并恢复其资源。下面的参数必须要加到 conf/jbossts-properties.xml文件中的jta部分:

<properties depends="arjuna" name="jta">
   ...
                     
   <property name="com.arjuna.ats.jta.recovery.XAResourceRecovery.HornetQ1"
                value="org.hornetq.jms.server.recovery.HornetQXAResourceRecovery;[连接配置]"/>
</properties>
            

其中[连接配置]包括连接HornetQ节点所必需的信息。 它的格式是[连接工厂类名],[用户名], [密码], [连接参数]

  • [连接工厂类名]指的是ConnectorFactory 的类名,用来连接HornetQ服务器。其值可以是org.hornetq.core.remoting.impl.invm.InVMConnectorFactoryorg.hornetq.core.remoting.impl.netty.NettyConnectorFactory

  • [用户名]是用于创建客户会话的用户名。是可选项。

  • [密码]是创建客户会话时所用的密码。只有在用户名存在时需要。

  • [连接参数] 是用逗号分隔的一串键-值对。它们会传递给连接器工厂 (参见 Chapter 16, 传输层的配置)。

Note

HornetQ必须有一个与conf/jbossts-properties.xml中定义的连接器相对应的接受器(acceptor)。

32.4.6.1.1. 配置参数

如果HornetQ配置了一个默认的in-vm接受器:

<acceptor name="in-vm">
    <factory-class>org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory</factory-class>
</acceptor>
                

相应地在 conf/jbossts-properties.xml文件中:

<property name="com.arjuna.ats.jta.recovery.XAResourceRecovery.HORNETQ1"
   value="org.hornetq.jms.server.recovery.HornetQXAResourceRecovery;org.hornetq.core.remoting.impl.invm.InVMConnectorFactory"/>        			
                

如果配置了一个netty接受器,并且端口不是默认的:

<acceptor name="netty">
    <factory-class>org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</factory-class>
    <param key="port" value="8888"/>
</acceptor>
                

相应的在 conf/jbossts-properties.xml文件中:

<property name="com.arjuna.ats.jta.recovery.XAResourceRecovery.HORNETQ1"
       value="org.hornetq.jms.server.recovery.HornetQXAResourceRecovery;org.hornetq.core.remoting.impl.netty.NettyConnectorFactory, , , port=8888"/>        			                    
                

Note

注意在没有用户名和密码时,逗号是不能省略的。

如果恢复必须要求是admin, adminpass,则其配置 应为如下所示:

                    <property name="com.arjuna.ats.jta.recovery.XAResourceRecovery.HORNETQ1"
                           value="org.hornetq.jms.server.recovery.HornetQXAResourceRecovery;org.hornetq.core.remoting.impl.netty.NettyConnectorFactory, admin, adminpass, port=8888"/>        			                    
                

推荐在XA恢复中,将HornetQ配置一个invm接受器,并配置恢复管理器使用invm连接器。

32.4.6.2. 例子

参见Section 11.3.9, “XA 恢复(recovery)”。这个例子展示了如何来配置XA恢复以便在服务器崩溃后恢复消息。