ActiveMQ安装及使用实例

上一篇文章写了一个利用redis的list实现了一个简单的消息队列,这篇文章将介绍关于ActiveMQ的一点知识。
1.ActiveMQ概述
ActiveMQ是Apache软件基金下的一个开源软件,它遵循JMS规范(Java Message Service),是消息驱动中间件软件(MOM)。下面总结了一些特性,摘自他人博客:
⒈1.支持多种语言客户端,如:Java,C,C++,C#,Ruby,Perl,Python,PHP。应用协议有 OpenWire,Stomp REST,WS Notification,XMPP,AMQP。
1.⒉ 完全支持JMS1.1和J2EE1.4规范,它们包括同步和异步消息传递,一次和只有一次的消息传递,对于预订者的持久消息等等。依附于JMS规范意味着,不论JMS消息提供者是谁,同样的基本特性(持久化,XA消息,事务)都是有效的。
1.⒊ 对Spring的支持,ActiveMQ可以很容易内嵌到使用Spring的系统里面去。
1.⒋ 通过了常见J2EE服务器(如 Geronimo,JBoss 4,GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上。
1.⒌ ActiveMQ提供各种连接选择,包括HTTP,HTTPS,IP多点传送,SSL,STOMP,TCP,UDP,XMPP等。大量的连接协议支持使之具有更好的灵活性。很多现有的系统使用一种特定协议并且不能改变,所以一个支持多种协议的消息平台降低了使用的门槛。虽然连接很重要,但是和其他容器集成也同样重要。
1.6.ActiveMQ提供多种持久性方案可供选择,也可以完全按自己需求定制验证和授权。例如,ActiveMQ通过KahaDB提供自己的超快速消息持久方案(ultra-fast message persistence),但也支持标准的JDBC方案。ActiveMQ可以通过配置文件提供简单的验证和授权,也提供标准的JAAS登陆模块。
1.7.ActiveMQ是为开发者设计的。它并不需要专门的管理工具,因为它提供各种易用且强大的管理特性。有很多方法去监控ActiveMQ的各个方面,可以通过JMX使用JConsole或ActiveMQ web console;可以运行ActiveMQ消息报告;可以用命令行脚本;可以通过日志。
1.8.代理器集群(Broker clustering)—-为了利于扩展,多个ActiveMQ broker能够联合工作。这个方式就是network of brokers并且能支持多种拓扑结构;支持客户端-服务器,点对点。并且支持Ajax, 支持与Axis的整合


2.ActiveMQ安装
首先去http://activemq.apache.org/download.html 下载最新版本ActiveMQ (当前最新版本为 ActiveMQ 5.14.4), 解压apache-activemq-x-bin.zip(或者apache-activemq-x-bin.tar.gz)
目录如下:
+bin (windows下面的bat和unix/linux下面的sh)
+conf (activeMQ配置目录,包含最基本的activeMQ配置文件)
+data (默认是空的)
+docs (只有index.html)
+example (几个例子)
+lib (activemMQ使用到的lib)
+webapps(后台管理页面)
+webapps-demo(后台管理消息发送页面)
+activemq-all-5.8.0.jar (java开发的jar包)
-LICENSE.txt
-NOTICE.txt
-README.txt
-user-guide.html
你可以使用bin\activemq.bat(activemq)启动。
启动ActiveMQ以后,因为其内置了jetty,所以可以登陆:http://localhost:8161/admin/ 默认用户/密码为admin/admin查看当前服务状况,包括队列消息数量等信息
注意:
⒈ 这个仅仅是最基础的ActiveMQ的配置,很多地方都没有配置因此不要直接使用这个配置用于生存环境。
⒉ 有的时候由于端口被占用,导致ActiveMQ错误,ActiveMQ可能需要以下端口1099(JMX),61616(默认的TransportConnector)。
⒊ 如果没有物理网卡,MS的LoopBackAdpater Multicast会报一个错误
4.运行附带的示例程序
1、Queue消息示例:
* 启动Queue消息消费者
cd example
ant consumer
* 启动Queue消息生产者
cd example
ant producer
简要说明:生产者(producer)发消息,消费者(consumer)接消息,发送/接收2000个消息后自动关闭
2、Topic消息示例:
* 启动Topic消息消费者
cd example
ant topic-listener
* 启动Topic消息生产者
cd example
ant topic-publisher
简要说明:重复10轮,publisher每轮发送2000个消息,并等待获取listener的处理结果报告,然后进入下一轮发送,最后统计全局发送时间。
3、Queue消息和Topic消息发送之后,可以通过后台点击Queues和Topics查看发送消息具体信息。


3.ActiveMQ使用
前面说的废话已经够多了,但是若想要充分了解ActiveMQ,建议把这些理论知识多看看,结合实际。下面给出我自己在本机上测试的代码
3.1生产者实现类

package ActivityMessageQueue;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/** 
 * <p>创建人:刘星  创建日期:2017-3-7 上午11:03:21</p>
 * <p>功能描述:(手填)</p>
 * @version V1.0  
 */
public class PTPProducer implements Runnable{
    private static String userName = ActiveMQConnection.DEFAULT_USER;//用户名
    private static String password =ActiveMQConnection.DEFAULT_PASSWORD;//密码
    private static String url = "tcp://localhost:61616";//连接服务地址
    public void run(){
        //用于创建连接的链接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(userName,password,url);
        try{
            //从链接工厂中创建一个连接
            Connection connection = connectionFactory.createConnection();
            //启用连接
            connection.start();
            //创建当前连接的接受/发送消息线程
            Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            //创建消息队列(发送/接受消息)
            Queue messageQueue = session.createQueue("ActiveMessageQueue");
            //创建生产者对象,指定消息发送的目的地队列
            MessageProducer producer = session.createProducer(messageQueue);
            //指定不需要持久化模式
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            for(int i = 0; i < 100;i++){
                TextMessage message = session.createTextMessage("send message :"+i);
                System.out.println("发送消息:"+message.getText());
                producer.send(message);
            }
            //提交当前消息对象
            session.commit();
            //链接关闭
            connection.close();
        }catch(Exception e ){
            e.printStackTrace();
        }
    }
}

3.2消费队列实现类

package ActivityMessageQueue;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/** 
 * <p>创建人:刘星  创建日期:2017-3-7 上午11:20:41</p>
 * <p>功能描述:(消费消息队列实现类)</p>
 * @version V1.0  
 */
public class PTPComuser implements Runnable{
    private static String user = ActiveMQConnection.DEFAULT_USER;  
    private static String password =ActiveMQConnection.DEFAULT_PASSWORD;  
    private static String url = "tcp://localhost:61616";
    public void run(){
        try{
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user,password,url);
            Connection connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue("ActiveMessageQueue");
            MessageConsumer consumer = session.createConsumer(queue);
            while (true) {
                 TextMessage message = (TextMessage) consumer.receive();
                 System.out.println("获取到的消息内容为:" + message.getText()); 
             }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

3.3测试类

package ActivityMessageQueue;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/** 
 * <p>创建人:刘星  创建日期:2017-3-7 上午11:31:32</p>
 * <p>功能描述:(测试ActiveMQ实现)</p>
 * @version V1.0  
 */
public class ActiveMessageQueue {
    public static void main(String args[]){
        ExecutorService excuteService = Executors.newFixedThreadPool(10);
        PTPConsumer consumer1 = new PTPConsumer();
        PTPConsumer consumer2 = new PTPConsumer();
        PTPConsumer consumer4 = new PTPConsumer();
        PTPProducer ptpProducer = new PTPProducer();
        excuteService.execute(consumer1);
        excuteService.execute(consumer2);
        excuteService.execute(consumer4);
        excuteService.execute(ptpProducer);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("终止当前线程");
        excuteService.shutdown();
    }
}

部分控制台输出信息:
发送消息:send message :20
发送消息:send message :68
发送消息:send message :21
发送消息:send message :69
发送消息:send message :22
发送消息:send message :70
发送消息:send message :23
发送消息:send message :71
发送消息:send message :24
发送消息:send message :72
发送消息:send message :25
发送消息:send message :73
发送消息:send message :74
发送消息:send message :75
发送消息:send message :76
收到消息send message :5
收到消息send message :6
收到消息send message :7
收到消息send message :8
收到消息send message :9
收到消息send message :10
收到消息send message :11
收到消息send message :12
收到消息send message :13
收到消息send message :14
收到消息send message :15
收到消息send message :16
收到消息send message :17

说明:在IDE中开发时,需要导入ActiveMq-all.X的jar,或者在maven中配置,运行前确保url的服务在运行中


4.总结
上面写的主要是基于PTP点对点模式的使用,ActiveMQ也支持发布者/订阅者模式。其实参考上面一篇博文这里写链接内容就知道,虽然没有阅读源码实现,但万变不离其宗,其实现的根本原理肯定也是用redis的队列来做,只不过在其上封装的功能更加强大,比如完美支持spring,支持配置模式运行,还可以使用监听器的相关功能。相关功能有时间的话我再整理

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值