码迷,mamicode.com
首页 > 其他好文 > 详细

ActiveMQ官方文档翻译-内嵌消息中间件

时间:2015-06-01 17:01:09      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

在大多数通信拓扑结构图中都有分JMS Brokers (服务端) 和JMS客户端。但有时会把消息中间件部署到你的jvm内部也是有道理的。This allows you to optimise away a network hop;让JMS的网络调用象RMI一样高效,但又拥有通常JMS的特性比如位置独立性,可靠性,负载均衡等。

根据你实际使用Java,Spring,XBean或ActiveMQConnectionFactory 的情况有许多种途径可以达到内嵌消息中间件。

显示的使用java代码

下面的java代码会创建一个内嵌的消息中间件

BrokerService broker = new BrokerService();
 
// configure the broker
broker.addConnector("tcp://localhost:61616");
 
broker.start();

If you want to lazily bind the transport connector as part of start(), useful when start() will block pending a store lock (as in a slave start),你可以使用下面的代码

BrokerService broker = new BrokerService();
 
TransportConnector connector = new TransportConnector();
connector.setUri(new URI("tcp://localhost:61616"));
broker.addConnector(connector);
broker.start();

在同一个JVM内部的客户端能够使用vm:// transport这样的方式连接到消息中间件-同时外部的客户端可以使用tcp:// protocol方式来连接消息中间件。

如果你有一个以上的内嵌的消息中间件,要确保你给它们每个实例起了一个唯一的名字,比如下面这样:

BrokerService broker = new BrokerService();
// configure the broker
broker.setBrokerName("fred");
broker.addConnector("tcp://localhost:61616");
broker.start();

然后如果你想在同一个jvm内部连接到一个名叫‘fred‘的消息中间件,你可以使用url vm://fred

也有可能使用应用代码完整的配置一个消息中间件,比如下面这样:

BrokerService broker = new BrokerService();
broker.setBrokerName("fred");
broker.setUseShutdownHook(false);
//Add plugin
broker.setPlugins(new BrokerPlugin[]{new JaasAuthenticationPlugin()});
//Add a network connection
NetworkConnector connector = answer.addNetworkConnector("static://"+"tcp://somehost:61616");
connector.setDuplex(true);
broker.addConnector("tcp://localhost:61616");
broker.start();

注:请注意你要把设置插件的代码放在连接前面否则它不会被初始化

更多关于可用的属性的细节,请参见BrokerService javadoc

使用BrokerFactory

有一个叫BrokerFactory的辅助类可以通过URL的配置方式来创建一个消息中间件

BrokerService broker = BrokerFactory.createBroker(new URI(someURI));

URI可用的值如下

URI scheme 例子
描述
xbean: xbean:activemq.xml 在clpassth(和文件系统)中搜索URI中给出的xml文件(这边是activemq.xml),然后把他当作xml配置
broker: broker:tcp://localhost:61616 使用 Broker Configuration URI的方式来配置中间件

使用spring

有一个factory bean能够指向外部的ActiveMQ XML配置文件

<bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean">
    <property name="config" value="classpath:org/apache/activemq/xbean/activemq.xml" />
    <property name="start" value="true" />
  </bean>

在这个例子中使用的是普通的Spring‘classpath:org/apache/activemq/xbean/activemq.xml‘资源文件,所以要确保这个activemq.xml文件在你的工程目录下的clpsspath目录下能找到‘org/apache/activemq/xbean/activemq.xml‘这个文件。当然你也可以使用其它的你喜欢的文件路径。比如使用:classpath:activemq.xml如果你把配置文件直接放在了classpqth的根目录下,象web工程中的WEB-INF/classes。


也行你希望你能够使用URL的方式来代替file:* or *http: 这样的前缀。更多的细节可以参考 Spring deals with resources

使用XBean

如果你已经在使用XBean你可以混合使用你的 Spring/XBean xml配置和ActiveMQ的配置。

<beans 
  xmlns="http://www.springframework.org/schema/beans" 
  xmlns:amq="http://activemq.apache.org/schema/core"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
 
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
 
  <broker useJmx="true" xmlns="http://activemq.apache.org/schema/core">
 
    <persistenceFactory>
      <kahaDB directory="${basedir}/target" />
    </persistenceFactory>
 
    <transportConnectors>
      <transportConnector uri="tcp://localhost:61636" />
    </transportConnectors>
 
  </broker>
</beans>

使用spring2.0

如果你正在使用Spring 2.0和ActiveMQ 4.1及以后的版本(和xbean-spring 2.5及以后的版本)你可以直接把ActiveMQ的配置放到任何合法的Spring.xml文件中而不需要上面的factory bean。例如下面就是一个例子:

<beans 
  xmlns="http://www.springframework.org/schema/beans" 
  xmlns:amq="http://activemq.apache.org/schema/core"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
   
  <!--  lets create an embedded ActiveMQ Broker -->
  <amq:broker useJmx="false" persistent="false">
    <amq:transportConnectors>
      <amq:transportConnector uri="tcp://localhost:0" />
    </amq:transportConnectors>
  </amq:broker>
 
   <!--  ActiveMQ destinations to use  -->
  <amq:queue id="destination"  physicalName="org.apache.activemq.spring.Test.spring.embedded"/>
 
  <!-- JMS ConnectionFactory to use, configuring the embedded broker using XML -->
  <amq:connectionFactory id="jmsFactory" brokerURL="vm://localhost"/>
   
 
  <!-- Spring JMS Template -->
  <bean id="myJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory">
      <!-- lets wrap in a pool to avoid creating a connection per send -->
      <bean class="org.springframework.jms.connection.SingleConnectionFactory">
        <property name="targetConnectionFactory">
          <ref local="jmsFactory" />
        </property>
      </bean>
    </property>
  </bean>
 
  <bean id="consumerJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="jmsFactory"/>
  </bean>
 
  <!-- a sample POJO which uses a Spring JmsTemplate -->
  <bean id="producer" class="org.apache.activemq.spring.SpringProducer">
    <property name="template">
      <ref bean="myJmsTemplate"></ref>
    </property>
 
    <property name="destination">
      <ref bean="destination" />
    </property>
 
    <property name="messageCount">
      <value>10</value>
    </property>
  </bean>
 
  <!-- a sample POJO consumer -->
  <bean id="consumer" class="org.apache.activemq.spring.SpringConsumer">
    <property name="template" ref="consumerJmsTemplate"/>
    <property name="destination" ref="destination"/>
  </bean>
 
</beans>

使用ActiveMQConnectionFactory

也可以通过使用ActiveMQConnectionFactory来创建一个内嵌的中间件并且使用一个vm connector象下面这样:

ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

使用查询参数 "broker.<property>" 来配置消息中间件,这边的<property>属性必须与BrokerService中的相应的属性匹配。

消息中间件将会在第一次连接时马上创建。

你可以通过设置create属性为假来关闭自动创建的功能:

ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?create=false");

原文链接

How do I embed a Broker inside a Connection


ActiveMQ官方文档翻译-内嵌消息中间件

标签:

原文地址:http://my.oschina.net/u/914897/blog/423428

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!