ActiveMQ的持久化(数据库)

  • 格式:doc
  • 大小:119.50 KB
  • 文档页数:11

利用消息队列的异步策略,可以从很大程序上缓解程序的压力,但是,如果MQ 所在的机器down机了,又如果队列中的数据不是持久的就会发生数据丢失,后果是可想而知的,所以消息的持久化是不可不讨论的话题。

1) 关于ActiveMQ消息队列的持久化,主要是在ActiveMQ的配置文件中设置(看粗体部分).Java代码1.<!--2. Licensed to the Apache Software Foundation (ASF) under oneor more3. contributor license agreements. See the NOTICE file distributed with4. this work for additional information regarding copyright ownership.5. The ASF licenses this file to You under the Apache License,Version 2.06. (the "License"); you may not use this file except in compliance with7. the License. You may obtain a copy of the License at8.9. /licenses/LICENSE-2.010.11. Unless required by applicable law or agreed to in writing,software12. distributed under the License is distributed on an "AS IS"BASIS,13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14. See the License for the specific language governing permissions and15. limitations under the License.16.-->17.<!-- START SNIPPET: example -->18.<beans19. xmlns="/schema/beans"20. xmlns:amq="/schema/core"21. xmlns:xsi="/2001/XMLSchema-instance"22. xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-2.0 .xsd23. /schema/core http://activemq.apache.org/schema/core/activemq-core.xsd24. /camel/schema/spring http://activem/camel/schema/spring/camel-spring.xsd">25.26. <!-- Allows us to use system properties as variables in this configuration file -->27. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">28. <property name="locations">29. <value>file:///${activemq.base}/conf/credentials.properties</value>30. </property>31. </bean>32.33. <broker xmlns="/schema/core" brokerName="localhost" dataDirectory="${activemq.base}/data">34.35. <!-- Destination specific policies using destination names or wildcards -->36. <destinationPolicy>37. <policyMap>38. <policyEntries>39. <policyEntry queue=">" memoryLimit="5mb"/>40. <policyEntry topic=">" memoryLimit="5mb">41. <!-- you can add other policies too suchas these42. <dispatchPolicy>43. <strictOrderDispatchPolicy/>44. </dispatchPolicy>45. <subscriptionRecoveryPolicy>46. <lastImageSubscriptionRecoveryPolicy/>47. </subscriptionRecoveryPolicy>48. -->49. </policyEntry>50. </policyEntries>51. </policyMap>52. </destinationPolicy>53.54. <!-- Use the following to configure how ActiveMQ is exposed in JMX -->55. <managementContext>56. <managementContext createConnector="false"/>57. </managementContext>58.59. <!-- The store and forward broker networks ActiveMQ will listen to -->60. <networkConnectors>61. <!-- by default just auto discover the other brokers -->62. <networkConnector name="default-nc" uri="multicast://default"/>63. <!-- Example of a static configuration:64. <networkConnector name="host1 and host2" uri="static://(tcp://host1:61616,tcp://host2:61616)"/>65. -->66. </networkConnectors>67.68. <persistenceAdapter>69. <amqPersistenceAdapter syncOnWrite="false" directory="${activemq.base}/data" maxFileLength="20 mb"/>70. </persistenceAdapter>71.72. <!-- Use the following if you wish to configure the journal with JDBC -->73. <!--74. <persistenceAdapter>75. <journaledJDBC dataDirectory="${activemq.base}/data" dataSource="#postgres-ds"/>76. </persistenceAdapter>77. -->78.79. <!-- Or if you want to use pure JDBC without a journal-->80.81. <SPAN style="COLOR: #ff6600"> <SPAN style="COLOR: #000000"><persistenceAdapter>82. <jdbcPersistenceAdapter dataSource="#mysql-ds"/>83. </persistenceAdapter></SPAN></SPAN>84.85.86. <sslContext>87. <sslContext keyStore="file:${activemq.base}/conf/broker.ks" keyStorePassword="password" trustStore="file:${activem q.base}/conf/broker.ts" trustStorePassword="password"/>88. </sslContext>89.90. <!-- The maximum about of space the broker will use before slowing down producers -->91. <systemUsage>92. <systemUsage>93. <memoryUsage>94. <memoryUsage limit="20 mb"/>95. </memoryUsage>96. <storeUsage>97. <storeUsage limit="1 gb" name="foo"/>98. </storeUsage>99. <tempUsage>100. <tempUsage limit="100 mb"/>101. </tempUsage>102. </systemUsage>103. </systemUsage>104.105.106. <!-- The transport connectors ActiveMQ will listen to -->107. <transportConnectors>108. <transportConnector name="openwire" uri="tcp:/ /localhost:61616" discoveryUri="multicast://default"/> 109. <transportConnector name="ssl" uri="ssl://loca lhost:61617"/>110. <transportConnector name="stomp" uri="stomp:// localhost:61613"/>111. <transportConnector name="xmpp" uri="xmpp://lo calhost:61222"/>112. </transportConnectors>113.114. </broker>115.116. <!--117. ** Lets deploy some Enterprise Integration Patterns in side the ActiveMQ Message Broker118. ** For more details see119. **120. ** /enterprise-integration-p atterns.html122. <camelContext id="camel" xmlns="http://activemq.apache .org/camel/schema/spring">123.124. <!-- You can use a <package> element for each root package to search for Java routes -->125. <package>org.foo.bar</package>126.127. <!-- You can use Spring XML syntax to define the r outes here using the <route> element -->128. <route>129. <from uri="activemq:example.A"/>130. <to uri="activemq:example.B"/>131. </route>132. </camelContext>133.134. <!--135. ** Lets configure some Camel endpoints136. **137. ** /camel/components.html 138. -->139.140. <!-- configure the camel activemq component to use the current broker -->141. <bean id="activemq" class="org.apache.activemq.camel.c omponent.ActiveMQComponent" >142. <property name="connectionFactory">143. <bean class="org.apache.activemq.ActiveMQConnect ionFactory">144. <property name="brokerURL" value="vm://localho st?create=false&amp;waitForStart=10000" />145. <property name="userName" value="${ ername}"/>146. <property name="password" value="${activemq.pa ssword}"/>147. </bean>148. </property>149. </bean>150.151.152.153. <!-- Uncomment to create a command agent to respond to message based admin commands on the ActiveMQ.Agent topic -->155. <commandAgent xmlns="/schema /core" brokerUrl="vm://localhost" username="${ername }" password="${activemq.password}"/>156. -->157.158.159. <!-- An embedded servlet engine for serving up the Adm in console -->160. <jetty xmlns="/schemas/jetty/1.0">161. <connectors>162. <nioConnector port="8161"/>163. </connectors>164.165. <handlers>166. <webAppContext contextPath="/admin" resourceBa se="${activemq.base}/webapps/admin" logUrlOnStart="true"/> 167. <webAppContext contextPath="/demo" resourceBas e="${activemq.base}/webapps/demo" logUrlOnStart="true"/> 168. <webAppContext contextPath="/fileserver" resou rceBase="${activemq.base}/webapps/fileserver" logUrlOnStart="tr ue"/>169. </handlers>170. </jetty>171.172. <!-- This xbean configuration file supports all the s tandard spring xml configuration options -->173.174. <!-- Postgres DataSource Sample Setup -->175. <!--176. <bean id="postgres-ds" class="org.postgresql.ds.PGPool ingDataSource">177. <property name="serverName" value="localhost"/> 178. <property name="databaseName" value="activemq"/> 179. <property name="portNumber" value="0"/>180. <property name="user" value="activemq"/>181. <property name="password" value="activemq"/> 182. <property name="dataSourceName" value="postgres"/>183. <property name="initialConnections" value="1"/> 184. <property name="maxConnections" value="10"/> 185. </bean>186. -->187.188. <!-- MySql DataSource Sample Setup -->189.190.<SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000"> <bean id="mysql-ds" class="mons.dbcp.B asicDataSource" destroy-method="close">191. <property name="driverClassName" value="com.mysql.jd bc.Driver"/>192. <property name="url" value="jdbc:mysql://localhost:3 306/activemq?relaxAutoCommit=true"/>193. <property name="username" value="root"/>194. <property name="password" value=""/>195. <property name="maxActive" value="200"/>196. <property<STRONG> </STRONG>name="poolPreparedStateme nts" value="true"/>197. </bean></SPAN>198.199.200. <!-- Oracle DataSource Sample Setup -->201. <!--202. <bean id="oracle-ds" class="mons.dbcp.Ba sicDataSource" destroy-method="close">203. <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>204. <property name="url" value="jdbc:oracle:thin:@localh ost:1521:AMQDB"/>205. <property name="username" value="scott"/>206. <property name="password" value="tiger"/>207. <property name="maxActive" value="200"/>208. <property name="poolPreparedStatements" value="true"/>209. </bean>210. -->211.212. <!-- Embedded Derby DataSource Sample Setup --> 213. <!--214. <bean id="derby-ds" class="org.apache.derby.jdbc.Embed dedDataSource">215. <property name="databaseName" value="derbydb"/> 216. <property name="createDatabase" value="create"/> 217. </bean>218. -->219.220.</beans>221.<!-- END SNIPPET: example -->改动部分主要是设置了mysql的datasource声明, 还有就是采用mysql作为persistenceAdapter,并声明如下。