1. 程式人生 > >ActiveMQ基本教程 ActiveMQ持久化 ActiveMQ安全

ActiveMQ基本教程 ActiveMQ持久化 ActiveMQ安全

一:快速上手

1:官方網站下載最新版本,當前最新為5.9.0

2:解壓後,開啟cmd,進入bin目錄,執行:activemq,即可啟動。(linux下,輸入nohup activemq &)

注意看打出的啟動日誌。

Loading message broker from: xbean:activemq.xml,這個檔案是主要的配置檔案。

Using Persistence Adapter: KahaDBPersistenceAdapter,這是一個activemq專用的訊息儲存器,速度很快的。

Listening for connections at: tcp://collonn-PC:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600,這是activemq監聽的其中一個埠。

ActiveMQ WebConsole available at http://localhost:8161/,這是activemq基於頁面的控制檯。

3:開啟瀏覽器,輸入http://localhost:8161/,選擇Manage ActiveMQ broker using the old console,用舊的方式檢視和控制activemq更方便。

二:快速進階

1:進入到activemq_install_dir/config目錄,有以下幾個重要檔案

(1)activemq.xml,在此檔案中你可以配置activemq的很多東西,比如將訊息持久化到資料庫等。

(2)credentials.properties,一些密碼,多用於生產和消費的密碼認證。

(3)jetty.xml,activemq內建了jetty應用伺服器。

(4)jetty-realm.properties,activemq控制檯登陸密碼。

三:持久化訊息到MySQL

1:activemq_install_dir/examples/config目錄下有好多示例配置檔案可以做參考,如持久化到資料庫,安全相關等。

2:將mysql驅動拷貝到activemq_install_dir/lib目錄下,並在資料庫中建立一個空的資料空的資料庫,名稱為activemq。

3:修改activemq.xml檔案,如下

四:讓activemq更安全,認證後才能生產和消費。

在activemq.xml檔案中加入以下,注意直接加在transportConnectors節點上面即可,檔案中引用的密碼來自於activemq_install_dir/config/credentials.properties檔案,activemq.xml完整內容如下:

五:最終的activemq.xml內容如下


  
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <!-- START SNIPPET: example -->
  16. <beans
  17. xmlns= "http://www.springframework.org/schema/beans"
  18. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  19. xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  20. http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
  21. <!-- Allows us to use system properties as variables in this configuration file -->
  22. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  23. <property name="locations">
  24. <value>file:${activemq.conf}/credentials.properties </value>
  25. </property>
  26. </bean>
  27. <!--
  28. The <broker> element is used to configure the ActiveMQ broker.
  29. -->
  30. <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost">
  31. <!--
  32. Configure message persistence for the broker. The default persistence
  33. mechanism is the KahaDB store (identified by the kahaDB tag).
  34. For more information, see:
  35. http://activemq.apache.org/persistence.html
  36. -->
  37. <persistenceAdapter>
  38. <jdbcPersistenceAdapter dataDirectory="${activemq.data}" dataSource="#mysql-ds"/>
  39. </persistenceAdapter>
  40. <plugins>
  41. <!-- Configure authentication; Username, passwords and groups -->
  42. <simpleAuthenticationPlugin>
  43. <users>
  44. <authenticationUser username="admin" password="admin" groups="users,admins"/>
  45. </users>
  46. </simpleAuthenticationPlugin>
  47. </plugins>
  48. <!--
  49. The transport connectors expose ActiveMQ over a given protocol to
  50. clients and other brokers. For more information, see:
  51. http://activemq.apache.org/configuring-transports.html
  52. -->
  53. <transportConnectors>
  54. <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
  55. <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
  56. </transportConnectors>
  57. <!-- destroy the spring context on shutdown to stop jetty -->
  58. <shutdownHooks>
  59. <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
  60. </shutdownHooks>
  61. </broker>
  62. <!-- MySql DataSource Sample Setup -->
  63. <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  64. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  65. <property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/>
  66. <property name="username" value="root"/>
  67. <property name="password" value="sa"/>
  68. <property name="maxActive" value="200"/>
  69. <property name="poolPreparedStatements" value="true"/>
  70. </bean>
  71. <!--
  72. Enable web consoles, REST and Ajax APIs and demos
  73. The web consoles requires by default login, you can disable this in the jetty.xml file
  74. Take a look at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details
  75. -->
  76. <import resource="jetty.xml"/>
  77. </beans>
  78. <!-- END SNIPPET: example -->