1 簡介php
activeMQ是一個徹底支持JMS1.1 和J2EE規範的JMS Provider實現; 儘管規範出臺已是好久的事情了,但JMS在當今的J2EE應用中仍然扮演着特殊的地位;html
特性列表java
多種語言和協議編寫客戶端;python
語 言:java、C、C++、C#、Ruby、Perl、python、php;mysql
應用協議:openWire、Stomp、REST、WSNotification、XMPP、AMQP;linux
徹底支持:JMS1.1和J2EE 1.4規範(持久化、XA消息、事務)nginx
對Spring的支持、ActiveMQ能夠很容易內嵌到使用Spring的系統裏面去,並且也支持Spring2.0的特性;web
經過了常見J2EE服務器(如Geronimo,JBoss 4, GlassFish,WebLogic)的測試,其中經過JCA 1.5 resource adaptors的配置,可讓ActiveMQ能夠自動的部署到任何兼容J2EE 1.4 商業服務器上 支持多種傳送協議:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA 支持經過JDBC和journal提供高速的消息持久化 從設計上保證了高性能的集羣,客戶端-服務器,點對點 spring
支持Ajaxsql
支持與Axis的整合
能夠很容易得調用內嵌JMS provider,進行測試
ActiveMQ的競爭者
1、其餘開源JMS provider(資料來自java-source.net)
jbossmq(jboss 4)
jboss messaging(jboss 5)
http://wenku.baidu.com/view/e9ea05dc7f1922791688e8f2.html
二、準備工做
軟件包位置:/applition/tools
安裝的位置:/applition
說明:本次實驗是在同一臺機器上作的mysql數據庫
IP地址 |
服務 |
192.168.1.177 |
apache-activemq-5.6.0-bin.tar.gz |
192.168.1.177 |
jdk-1.7u |
三、activeMQ安裝
3.1 安裝jdk(java環境)
rpm -ivh jdk-7u67-linux-x64.rpm Preparing... ########################################### [100%] 1:jdk ########################################### [100%] Unpacking JAR files... rt.jar... jsse.jar... charsets.jar... tools.jar... localedata.jar... jfxrt.jar... #設置JAVA環境變量 提示:rpm安裝後JDK默認是在/usr/java/目錄下,先找到以後複製路徑並寫入到/profile # vim /etc/profile # → 最底部,添加java環境便可 export JAVA_HOME=/usr/java/jdk1.7.0_67 export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin
3.2 下載安裝activeMQ
3.2.1 下載activeMQ安裝包,並拷貝 到/activeMQ目錄下
下載地址:http://repository.apache.org/download.html
3.2.2 解壓文件到運行的目錄
cd /applition/tools tar fxz apache-activemq-5.6.0-bin.tar.gz -C /applition/
3.2.3 修改activeMQ運行文件的權限
cd /applition/apache-activemq-5.6.0/bin chmod 755 activemq
3.2.4 運行activeMQ服務
運行方式有3種:
1)普通啓動./activemq start
2)啓動並指定日誌文件./activemq start & >>/tmp/smlog
3)後臺啓動方式 nohup ./activemq start & >>/tmp/smlog
前2種方式在命令行窗口關閉時 或者 ctrl+c 時致使進程退出;
採用後臺啓動方式則能夠避免這個種狀況;
3.2.5 檢查服務啓動是否正確
ActiveMQ默認採用61616端口提供JMS服務,使用8160端口提供管理控制檯服務,執行如下命令以便檢驗是否已經成功啓動ActiveMQ服務;
# ps -ef |grep mq root 24214 1 85 18:13 pts/0 00:00:08 /usr/bin/java -Xms1G -Xmx1G-Djava.util.logging.config.file=logging.properties -Dcom.sun.management.jmxremote-Djava.io.tmpdir=/applition/apache-activemq-5.6.0/tmp-Dactivemq.classpath=/applition/apache-activemq-5.6.0/conf;-Dactivemq.home=/applition/apache-activemq-5.6.0-Dactivemq.base=/applition/apache-activemq-5.6.0 -Dactivemq.conf=/applition/apache-activemq-5.6.0/conf-Dactivemq.data=/applition/apache-activemq-5.6.0/data -jar /applition/apache-activemq-5.6.0/bin/run.jarstart # 查看61616端口是否打開: # netstat -lanpt |grep 61616 tcp 0 0 :::61616 :::* LISTEN 24214/java
檢查是否已經啓動:
查看控制檯輸出或者日誌文件
直接訪問activemq的管理頁面:http://192.168.16.177:8161/admin/
訪問web頁面activeMQ須要安裝apache或nginxweb服務便可
3.2.6 如何關閉服務
若是開啓方式是使用(1)或(2),則直接ctrl+c 或者關閉對應的終端便可
若是開啓的方式(3),則要殺掉進程PID才行
ps -ef |grep activemq kill `ps -ef|grep activemq`
四、activeMQ如何作持久化mysql數據庫
4.1 添加mysql數據源
打開ActiveMQ安裝目錄下的conf/activemq.xml文件,添加MySQL數據源; 默認狀況下ActiveMQ使用的是kahaDB存儲,所以要註釋掉KahaDB配置,改成mysql配置 以下所示:
<!-- <persistenceAdapter> <kahaDBdirectory="${activemq.data}/kahadb"/> </persistenceAdapter> --> <persistenceAdapter> <jdbcPersistenceAdapter dataSource="#MySQL-DS"useDatabaseLock="false" /> </persistenceAdapter> # 說明: 咱們講要使用名稱爲「MySQL-DS」做爲mysql數據源;
4.2 配置mysql數據源
在</broker>節點後面,增長MySQL數據源配置:
<bean id="MySQL-DS"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"> <property name="driverClassName"value="com.mysql.jdbc.Driver"/> <propertyname="url" value="jdbc:mysql://127.0.0.1:3306/activemq?useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="activemq"/> <property name="password" value="123123"/> <property name="poolPreparedStatements"value="true"/> </bean>
提示說明:
此處配置相似於spring的Bean配置,id要與上面的保持一致才行;
4.3 配置完成後,將mysql驅動包(mysql-connector-java-5.1.22-bin.jar包)加入到activeMQ的lib目錄下,並從新啓動activeMQ;
[root@liubl bin]# find / -name "mysql-connector-java-5.1.17.jar" /usr/share/java/mysql-connector-java-5.1.17.jar
查看activeMQ數據庫會有三張表,activemq_acks、activemq_lock,activemq_msgs
數據表activemq_msgs即爲持久化消息表;
4.3.1 登入mysql建立activemq受權信息
mysql> createdatabase activemq default charset=UTF8; Query OK, 1 row affected (0.00 sec) mysql> GRANTALL PRIVILEGES ON activemq.* TO 'activemq'@'%' IDENTIFIED BY '123123'; Query OK, 0 rows affected (0.00 sec) mysql> grantall privileges on activemq.* to 'activemq'@'localhost' identified by '123123'; Query OK, 0 rows affected (0.00 sec)
4.3.2 重啓activeMQ後,查看是否已建立出 上面提到的3個表信息
[root@liubl ~]# ps -ef |grep mq|grep -v grep root 28412 1 0Jul09 pts/1 00:01:39/usr/java/jdk1.7.0_67/bin/java -Xms1G -Xmx1G-Djava.util.logging.config.file=logging.properties -Dcom.sun.management.jmxremote-Djava.io.tmpdir=/applition/activeMQ/apache-activemq-5.6.0/tmp-Dactivemq.classpath=/applition/activeMQ/apache-activemq-5.6.0/conf;-Dactivemq.home=/applition/activeMQ/apache-activemq-5.6.0-Dactivemq.base=/applition/activeMQ/apache-activemq-5.6.0-Dactivemq.conf=/applition/activeMQ/apache-activemq-5.6.0/conf-Dactivemq.data=/applition/activeMQ/apache-activemq-5.6.0/data -jar/applition/activeMQ/apache-activemq-5.6.0/bin/run.jarstart [root@localhost activeMQ]# ps -ef |grep mq |grep -v grep |awk '{print $2}' 28412 [root@liubl ~]# kill -9 28412 [root@liubl ~]# nohup ./apache-activemq-5.6.0/bin/activemq start & #提示: #若想重啓activeMQ 必須找到進程殺掉後,在啓動便可 #查看數據庫activemq下自動增長的3個表是否存在 mysql> show tables; +--------------------+ | Tables_in_activemq | +--------------------+ | ACTIVEMQ_ACKS | | ACTIVEMQ_LOCK | | ACTIVEMQ_MSGS | +--------------------+ 3 rows in set (0.00 sec)
4.3.3 最後,添加到開機自啓動文件
echo " nohup ./apache-activemq-5.6.0/bin/activemq start &">>/etc/rc.local
4.3.4 activeMQ持久消息到mysql數據庫全配置文件以下:
<!-- Licensed tothe Apache Software Foundation (ASF) under one or more contributorlicense agreements. See the NOTICE filedistributed with this work foradditional information regarding copyright ownership. The ASFlicenses this file to You under the Apache License, Version 2.0 (the"License"); you may not use this file except in compliance with theLicense. You may obtain a copy of theLicense at http://www.apache.org/licenses/LICENSE-2.0 Unle***equired by applicable law or agreed to in writing, software distributedunder the License is distributed on an "AS IS" BASIS, WITHOUTWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See theLicense for the specific language governing permissions and limitationsunder the License. --> <!-- START SNIPPET: 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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/schema/corehttp://activemq.apache.org/schema/core/activemq-core.xsd"> <!-- Allowsus to use system properties as variables in this configuration file --> <beanclass="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. --> <brokerxmlns="http://activemq.apache.org/schema/core"brokerName="localhost" dataDirectory="${activemq.data}"> <!-- Forbetter performances use VM cursor and small memory limit. Formore information, see: http://activemq.apache.org/message-cursors.html Also,if your producer is "hanging", it's probably due to producer flowcontrol. Formore information, see: http://activemq.apache.org/producer-flow-control.html --> <destinationPolicy> <policyMap> <policyEntries> <policyEntry topic=">"producerFlowControl="true" memoryLimit="1mb"> <pendingSubscriberPolicy> <vmCursor /> </pendingSubscriberPolicy> </policyEntry> <policyEntry queue=">"producerFlowControl="true" memoryLimit="1mb"> <!-- Use VM cursor for better latency For more information, see: http://activemq.apache.org/message-cursors.html <pendingQueuePolicy> <vmQueueCursor/> </pendingQueuePolicy> --> </policyEntry> </policyEntries> </policyMap> </destinationPolicy> <!-- ThemanagementContext is used to configure how ActiveMQ is exposed in JMX. By default, ActiveMQ uses theMBean server that is started by theJVM. For more information, see: http://activemq.apache.org/jmx.html --> <managementContext> <managementContextcreateConnector="false"/> </managementContext> <!-- Configure message persistence for the broker. The default persistence mechanism is the KahaDB store (identified by the kahaDB tag). Formore information, see: http://activemq.apache.org/persistence.html --> <persistenceAdapter> <jdbcPersistenceAdapterdataSource="#MySQL-DS"/> </persistenceAdapter> <!-- ThesystemUsage controls the maximum amount of space the broker will usebefore slowing down producers. For more information, see: http://activemq.apache.org/producer-flow-control.html Ifusing ActiveMQ embedded - the following limits could safely be used: <systemUsage> <systemUsage> <memoryUsage> <memoryUsage limit="20 mb"/> </memoryUsage> <storeUsage> <storeUsage limit="1 gb"/> </storeUsage> <tempUsage> <tempUsage limit="100 mb"/> </tempUsage> </systemUsage> </systemUsage> --> <systemUsage> <systemUsage> <memoryUsage> <memoryUsage limit="64 mb"/> </memoryUsage> <storeUsage> <storeUsage limit="100 gb"/> </storeUsage> <tempUsage> <tempUsage limit="50 gb"/> </tempUsage> </systemUsage> </systemUsage> <!-- Thetransport connectors expose ActiveMQ over a given protocol to clients and other brokers. For more information, see: http://activemq.apache.org/configuring-transports.html --> <transportConnectors> <transportConnector name="openwire"uri="tcp://0.0.0.0:61616"/> → 默認端口要更 </transportConnectors> </broker> <!-- MySQL DataSource --> <bean id="MySQL-DS"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"> <propertyname="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url"value="jdbc:mysql://127.0.0.1:3306/activemq?useUnicode=true&characterEncoding=UTF-8"/> <property name="username"value="activemq"/> <property name="password"value="123123"/> <propertyname="poolPreparedStatements" value="true"/> </bean> <!-- Enable webconsoles, REST and Ajax APIs and demos Take alook at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details --> <importresource="jetty.xml"/> </beans> <!-- END SNIPPET: example -->