ActiveMQ消息存储-JDBC消息存储、Memory消息存储

ActiveMQ默认使用的JDBC 驱动是Derby.

Derby: 100%使用Java语言编写,也是一个可被内嵌的数据库,可被在其运行的JVM进行垃圾回收。因此在

ActiveMQ使用持久化消息时建议将ActiveMQ和Derby部署至不同的JVM中。这样ActiveMQ和Derby将不再

竞争JVM资源。

1. JDBC消息存储的Scheme

     一共有三张表:参考:108-109

     ACTIVEMQ_MSGS:存储消息

     ACTIVEMQ_ACKS: 存储确认

     ACTIVEMQ_LOCK:Lock Table,用于确保只有一个ActiveMQ Broker在统一时间能访问DB.

2. JDBC消息存储的配置    

     Derby配置:

<beans>
	<broker brokerName="test-broker" persistent="true"
		xmlns="http://activemq.apache.org/schema/core">
		<persistenceAdapter>
			<jdbcPersistenceAdapter dataDirectory="activemq-data" />
		</persistenceAdapter>
	</broker>
</beans>

     MySQL配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<broker brokerName="test-broker" persistent="true"
		xmlns="http://activemq.apache.org/schema/core">
		<persistenceAdapter>
			<jdbcPersistenceAdapter dataSource="#mysql-ds" />
		</persistenceAdapter>
	</broker>
	<bean id="mysql-ds" class="org.apache.commons.dbcp.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="maxActive" value="200" />
		<property name="poolPreparedStatements" value="true" />
	</bean>
</beans>

     Oracle配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<broker brokerName="test-broker" persistent=true
		xmlns="http://activemq.apache.org/schema/core">
		<persistenceAdapter>
			<jdbcPersistenceAdapter dataSource="#oracle-ds" />
		</persistenceAdapter>
	</broker>
	<bean id="oracle-ds" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:AMQDB" />
		<property name="username" value="scott" />
		<property name="password" value="tiger" />
		<property name="maxActive" value="200" />
		<property name="poolPreparedStatements" value="true" />
	</bean>
</beans>

3. JDBC消息存储使用ActiveMQ journal

     配置journal使用JDBC:

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<broker brokerName="test-broker" xmlns="http://activemq.apache.org/schema/core">
		<persistenceFactory>
			<journalPersistenceAdapterFactory
				journalLogFiles="4" journalLogFileSize="32768" useJournal="true"
				useQuickJournal="true" dataSource="#derby-ds" dataDirectory="activemq-data" />
		</persistenceFactory>
	</broker>
	<bean id="derby-ds" class="org.apache.derby.jdbc.EmbeddedDataSource">
		<property name="databaseName" value="derbydb" />
		<property name="createDatabase" value="create" />
	</bean>
</beans>

    Journal提供了相当优秀的性能,相比与标准的JDBC消息存储,特别是当JDBC 数据库和ActiveMQ Broker

    处于同一台主机时。

    如果在一个Master/Slave配置的共享环境下,建议不要使用Journal;因为来自Master的消息在被提交给数

    据库前可被存储在本地,如果Master宕掉将引起消息丢失,因为事务Journal不能被复制。

    Memory消息存储时存储所有的消息在内存中。如果希望Broker存储有限的消息并且被Consumer快速的消

    费时可考虑该方案。

1. Memory消息存储配置   

     XML配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<broker brokerName="test-broker" persistent="false"


		xmlns="http://activemq.apache.org/schema/core">
		<transportConnectors>
			<transportConnector uri="tcp://localhost:61635" />
		</transportConnectors>
	</broker>
</beans>

     Java配置:

import org.apache.activemq.broker.BrokerService;

public void createEmbeddedBroker() throws Exception {
	
	BrokerService broker = new BrokerService();
	//configure the broker to use the Memory Store
	broker.setPersistent(false);


	//Add a transport connector
	broker.addConnector("tcp://localhost:61616");
	//now start the broker
	broker.start();
}   

相关推荐