1 简介

    activeMQ是一个完全支持JMS1.1 和J2EE规范的JMS Provider实现; 尽管规范出台已经是很久的事情了,但JMS在当今的J2EE应用中仍然扮演着特殊的地位;

特性列表

多种语言和协议编写客户端;

语    言:java、C、C++、C#、Ruby、Perl、python、php;

应用协议:openWire、Stomp、REST、WSNotification、XMPP、AMQP;

完全支持:JMS1.1和J2EE 1.4规范(持久化、XA消息、事务)

    对Spring的支持、ActiveMQ可以很容易内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性;

    通过了常见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提供高速的消息持久化  从设计上保证了高性能的集群,客户端-服务器,点对点 

支持Ajax

支持与Axis的整合

可以很容易得调用内嵌JMS  provider,进行测试

ActiveMQ的竞争者

1、其他开源JMS provider(资料来自java-source.net)

    jbossmq(jboss 4)

    jboss messaging(jboss 5)

http://wenku.baidu.com/view/e9ea05dc7f1922791688e8f2.html


2、准备工作

软件包位置:/applition/tools

安装的位置:/applition

说明:本次实验是在同一台机器上做的mysql数据库

IP地址

服务

192.168.1.177

apache-activemq-5.6.0-bin.tar.gz

192.168.1.177

jdk-1.7u


3、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服务即可

wKioL1WwRWXjMZ36AAHa0MIlYuM455.jpg

3.2.6 如何关闭服务

    如果开启方式是使用(1)或(2),则直接ctrl+c 或者关闭对应的终端即可

    如果开启的方式(3),则要杀掉进程PID才行

ps -ef |grep activemq
kill `ps -ef|grep activemq`

4、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数据源;

wKioL1WwRnDTkLo3AADyg19-PEM117.jpg

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&amp;characterEncoding=UTF-8"/>
       <property name="username" value="activemq"/>
       <property name="password" value="123123"/>
       <property name="poolPreparedStatements"value="true"/>
    </bean>

wKiom1WwRP2h2eZOAAJeVDQLyCg590.jpg

提示说明:

    此处配置类似于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&amp;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 -->