ActiveMQ官方文檔翻譯-內嵌消息中間件

在大多數通訊拓撲結構圖中都有分JMS Brokers (服務端) 和JMS客戶端。但有時會把消息中間件部署到你的jvm內部也是有道理的。This allows you to optimise away a network hop;讓JMS的網絡調用象RMI同樣高效,但又擁有一般JMS的特性好比位置獨立性,可靠性,負載均衡等。html

根據你實際使用Java,Spring,XBean或ActiveMQConnectionFactory 的狀況有許多種途徑能夠達到內嵌消息中間件。java

顯示的使用java代碼

下面的java代碼會建立一個內嵌的消息中間件
web

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),你可使用下面的代碼spring

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方式來鏈接消息中間件。apache

若是你有一個以上的內嵌的消息中間件,要確保你給它們每一個實例起了一個惟一的名字,好比下面這樣:api

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 javadocjvm

使用BrokerFactory

有一個叫BrokerFactory的輔助類能夠經過URL的配置方式來建立一個消息中間件
maven

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

相關文章
相關標籤/搜索