1. 程式人生 > >ActiveMQ 5.x資料庫持久化配置和嵌入式Broker的使用

ActiveMQ 5.x資料庫持久化配置和嵌入式Broker的使用

1.持久化配置

ActiveMQ 5.x以上實現資料庫持久化配置:

<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">
    <networkConnectors>
      <!-- 
      <networkConnector uri="multicast://default?initialReconnectDelay=100" />
      <networkConnector uri="static://(tcp://localhost:61616)" />
       -->
    </networkConnectors>
    <persistenceFactory>
      <journalPersistenceAdapterFactory journalLogFiles="5" dataDirectory="${basedir}/target" />
       
      <!-- To use a different dataSource, use the following syntax : -->
      <!-- 
      <journalPersistenceAdapterFactory journalLogFiles="5" dataDirectory="${basedir}/activemq-data" dataSource="#mysql-ds"/>
       -->
    </persistenceFactory>
    <transportConnectors>
      <transportConnector uri="tcp://localhost:61636" />
    </transportConnectors>
  </broker>
  <!-- MySql DataSource Sample Setup -->
  <!-- 
  <bean id="mysql-ds" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/>
    <property name="username" value="activemq"/>
    <property name="password" value="activemq"/>
    <property name="poolPreparedStatements" value="true"/>
  </bean>
  --> 
</beans>

2.Broker的使用

在許多訊息拓撲有JMS代理(伺服器端)和JMS客戶端。通常是有意義的部署一個Broker在您的JVM。這允許您優化了網路躍點;使JMS作為高效的網路純RMI,但通常的位置獨立性JMS的特性,可靠性、負載平衡等。有多種方法嵌入一個Broker在ActiveMQ取決於如果您使用的是Java,Spring,XBean ActiveMQConnectionFactory或使用ActiveMQConnectionFactory。

下面的Java程式碼將建立一個嵌入式broker;

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

如果你想延遲繫結傳輸聯結器的一部分start(),有用的時候start()將阻塞等待一個儲存鎖(如一個slave節點啟動),您可以使用下面的程式碼;
BrokerService broker = new BrokerService();
 
TransportConnector connector = new TransportConnector();
connector.setUri(new URI("tcp://localhost:61616"));
broker.addConnector(connector);
broker.start();

在同一個JVM客戶可以使用vm:/ /transport
連線到嵌入式broker,同時外部客戶可以使用tcp://protocol

如果你有超過一個嵌入式代理,確保你給他們一個唯一的名稱,如:
BrokerService broker = new BrokerService();
// configure the broker
broker.setBrokerName("fred");
broker.addConnector("tcp://localhost:61616");
broker.start();
然後如果你想連線到代理名叫“fred”在相同的JVM中,您可以通過使用uri 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();

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

URI scheme

Example

Description

xbean:

xbean:activemq.xml

Searches the classpath (and file system) for an XML document with the given URI (activemq.xml in this case) which will then be used as theXml Configuration

broker:

broker:tcp://localhost:61616

Spring

<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>

XBean
<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>
Spring 2.0 Example
<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

1.嵌入式broker也可以使用一個ActiveMQConnectionFactory建立和使用一個vm聯結器作為一個uri:

ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
2.使用查詢引數”broker.<property>”來配置代理,<property>匹配BrokerService bean屬性。
broker將在建立第一個建立連線,你可以關掉自動建立通過設定在VM建立屬性傳輸錯誤:
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?create=false");

From:http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html
更多經典文章參考:http://topmanopensource.iteye.com/category/64329