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內容如下
-
<!--
-
Licensed to the Apache Software Foundation (ASF) under one or more
-
contributor license agreements. See the NOTICE file distributed with
-
this work for additional information regarding copyright ownership.
-
The ASF licenses this file to You under the Apache License, Version 2.0
-
(the "License"); you may not use this file except in compliance with
-
the License. You may obtain a copy of the License at
-
-
http://www.apache.org/licenses/LICENSE-2.0
-
-
Unless required by applicable law or agreed to in writing, software
-
distributed under the License is distributed on an "AS IS" BASIS,
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-
See the License for the specific language governing permissions and
-
limitations under the License.
-
-->
-
<!-- START SNIPPET: example -->
-
<beans
-
xmlns=
"http://www.springframework.org/schema/beans"
-
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.xsd
-
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
-
-
<!-- Allows us to use system properties as variables in this configuration file -->
-
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
-
<property name="locations">
-
<value>file:${activemq.conf}/credentials.properties
</value>
-
</property>
-
</bean>
-
-
<!--
-
The <broker> element is used to configure the ActiveMQ broker.
-
-->
-
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost">
-
<!--
-
Configure message persistence for the broker. The default persistence
-
mechanism is the KahaDB store (identified by the kahaDB tag).
-
For more information, see:
-
-
http://activemq.apache.org/persistence.html
-
-->
-
<persistenceAdapter>
-
<jdbcPersistenceAdapter dataDirectory="${activemq.data}" dataSource="#mysql-ds"/>
-
</persistenceAdapter>
-
-
<plugins>
-
<!-- Configure authentication; Username, passwords and groups -->
-
<simpleAuthenticationPlugin>
-
<users>
-
<authenticationUser username="admin" password="admin" groups="users,admins"/>
-
</users>
-
</simpleAuthenticationPlugin>
-
</plugins>
-
-
<!--
-
The transport connectors expose ActiveMQ over a given protocol to
-
clients and other brokers. For more information, see:
-
-
http://activemq.apache.org/configuring-transports.html
-
-->
-
<transportConnectors>
-
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
-
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
-
</transportConnectors>
-
-
<!-- destroy the spring context on shutdown to stop jetty -->
-
<shutdownHooks>
-
<bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
-
</shutdownHooks>
-
-
</broker>
-
-
<!-- MySql DataSource Sample Setup -->
-
<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="root"/>
-
<property name="password" value="sa"/>
-
<property name="maxActive" value="200"/>
-
<property name="poolPreparedStatements" value="true"/>
-
</bean>
-
-
<!--
-
Enable web consoles, REST and Ajax APIs and demos
-
The web consoles requires by default login, you can disable this in the jetty.xml file
-
-
Take a look at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details
-
-->
-
<import resource="jetty.xml"/>
-
-
</beans>
-
<!-- END SNIPPET: example -->