`

ActiveMQ

 
阅读更多
ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。

ActiveMQ特性列表

1. 多种语言和协议编写客户端。语言: Java, C, C++, C#, Ruby, Perl, Python, PHP。应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
2. 完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务)
3. 对Spring的支持,ActiveMQ可以很容易内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性
4. 通过了常见J2EE服务器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上
5. 支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
6. 支持通过JDBC和journal提供高速的消息持久化
7. 从设计上保证了高性能的集群,客户端-服务器,点对点
8. 支持Ajax
9. 支持与Axis的整合
10. 可以很容易得调用内嵌JMS provider,进行测试

1:下载 ActiveMQ 5.6.0 Release

http://activemq.apache.org/download.html

放到d盘



2:运行apache-activemq服务:双击 activemq.bat



3:效果



4:所需jar包



5:spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL">
            <value>tcp://localhost:61616?wireFormat.maxInactivityDuration=0</value>
        </property>
    </bean>
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory">
            <ref bean="connectionFactory"/>
        </property>
    </bean>
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0">
            <value>MessageQueue</value>
        </constructor-arg>
    </bean>
</beans>

这时主要是配置activemq服务信息与实现springjms的对应接口

6:消息产生者
package test;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.MessageCreator;

/**
 * 消息产生者
 * User: liuwentao
 * Time: 12-6-14 上午11:31
 */
public class MyMessageCreator implements MessageCreator {
    public int n = 0;
    private static String str1 = "这个是第 ";
    private static String str2 = " 个测试消息!";
    private String str = "";
    @Override
    public Message createMessage(Session paramSession) throws JMSException {
        System.out.println("MyMessageCreator  n=" + n);
        if (n == 9) {
            //在这个例子中表示第9次调用时,发送结束消息
            return paramSession.createTextMessage("end");
        }
        str = str1 + n + str2;
        return paramSession.createTextMessage(str);
    }
}


7:发送消息方
package test;
import javax.jms.Destination;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
/**
 * 发送消息方
 * User: liuwentao
 * Time: 12-6-14 上午11:29
 */
public class MessageSender extends Thread {
    public static void main(String args[]) throws Exception {
        String[] configLocations = new String[] {"test/applicationContext.xml"};
        ApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
        JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
        Destination destination = (Destination) context.getBean("destination");
        for (int i = 1; i < 100; i++) {
            System.out.println("发送 i=" + i);
            //消息产生者
            MyMessageCreator myMessageCreator = new MyMessageCreator();
            myMessageCreator.n = i;
            jmsTemplate.send(destination, myMessageCreator);
            sleep(10000);//10秒后发送下一条消息
        }
    }
}

8:消息接收方
package test;
import javax.jms.Destination;
import javax.jms.TextMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
/**
 * 消息接收方
 * User: liuwentao
 * Time: 12-6-14 上午11:32
 */
public class MessageReciver{
    public static void main(String args[]) throws Exception {
        String[] configLocations = new String[] {"test/applicationContext.xml"};
        ApplicationContext context = new ClassPathXmlApplicationContext(configLocations);

        JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
        Destination destination = (Destination) context.getBean("destination");

        TextMessage msg = null;
        //是否继续接收消息
        boolean isContinue = true;
        while (isContinue) {
            msg = (TextMessage) jmsTemplate.receive(destination);
            System.out.println("收到消息 :" + msg.getText());
            if (msg.getText().equals("end")) {
                isContinue = false;
                System.out.println("收到退出消息,程序要退出!");
            }
        }
        System.out.println("程序退出了!");
    }
}


9:测试

运行 发送方和接收方 (顺序随便) main文件,效果如下:





注:即使 接收方启动晚,或者 发送方关闭了, 接收方都会正常接收完所有数据

  • 大小: 22.1 KB
  • 大小: 8.3 KB
  • 大小: 15.3 KB
  • 大小: 33.2 KB
  • 大小: 14.4 KB
  • 大小: 14.4 KB
  • 大小: 11.3 KB
分享到:
评论
3 楼 javatozhang 2014-09-15  
为何不把测试代码也放上呢?谢谢!
2 楼 tbs005 2014-04-14  
简介清晰,非常感谢~!
1 楼 fypop 2012-07-03  
很好,学习了. MQ还真不错. 作为web的中间件.
Spring也很给力, 比写纯java方便多了.
顶一个!!!!

相关推荐

    springboot-nettysocketio +netty+activeMq在线客服系统

    springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot...

    spring 整合activemq实现自定义动态消息队列

    百度spring整合activemq 发现几乎都只是在xml文件配置固定的消息队列而且太麻烦。并没有根据需求进行动态生成主题和队列。本文档参考了纯粹的activemq java代码和百度上的demo,很简洁的实现了动态消息队列的生成和...

    zabbix 3.4 监控 Activemq 自动发现模板

    用zabbix 自动发现实现activemq 监控pending consumers activemq_scan.sh #!/bin/bash activemq() { MQ_IP=(10.10.11.208:8161) for g in ${MQ_IP[@]} do port=($(curl -uadmin:admin http://${g}/admin/queues.jsp...

    activemq-core-5.7.0-API文档-中文版.zip

    赠送jar包:activemq-core-5.7.0.jar; 赠送原API文档:activemq-core-5.7.0-javadoc.jar; 赠送源代码:activemq-core-5.7.0-sources.jar; 包含翻译后的API文档:activemq-core-5.7.0-javadoc-API文档-中文...

    apache-activemq-5.15.0-bin.tar.7z

    MQ是消息中间件,是一种在分布式系统中应用程序借以传递消息的媒介,常用的有ActiveMQ,RabbitMQ,kafka。ActiveMQ是Apache下的开源项目,完全支持JMS1.1和J2EE1.4规范的JMS Provider实现。 特点: 1、支持多种...

    ActiveMQ高并发处理方案

    ActiveMQ高并发处理方案ActiveMQ高并发处理方案 超级字数补丁超级字数补丁

    activeMQ示例 activeMQ demo,java分布式技术

    本教程旨在帮助activeMQ初学者入门,通过本示例,能完全理解activeMQ的基本概念,为分布式应用打下基础。 本示例中,使用maven管理,完美解决各种依赖问题,不需要自行配置,导入项目等待eclipse自行下载jar包后即可...

    activeMQ收发工具.rar

    activeMQ的测试工具,用于发送和接收activeMQ消息,jar包形式的,安装完jdk之后用java -jar xxx.jar命令运行

    MQ之ActiveMQ.mmap

    自己做的尚硅谷周阳老师ActiveMQ课程脑图,其中自己所用做案例的环境搭建都是基于docker与老师课程不一样。脑图内容涵盖视频的99%的笔记,含有自己编写的代码文件,外加了自己对一些问题的测试与回答。 消息中间件...

    ActiveMQ 之Spring结合实例

    包括1、ActiveMQ java实例 2、ActiveMQ Spring结合实例 3、代码亲测,无问题。 4、资源分5分绝对值 注意:请先安装ActiveMQ 服务。

    activemq-protobuf-1.1-API文档-中文版.zip

    赠送jar包:activemq-protobuf-1.1.jar; 赠送原API文档:activemq-protobuf-1.1-javadoc.jar; 赠送源代码:activemq-protobuf-1.1-sources.jar; 包含翻译后的API文档:activemq-protobuf-1.1-javadoc-API文档-...

    apache-activemq-5.11.2

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的...

    apache-activemq Linux版本

    apache-activemq Linux版本

    ActiveMQ部署方案分析对比

    构建高可用的ActiveMQ系统在生产环境中是非常重要的,单点的ActiveMQ作为企业应用无法满足高可用和集群的需求,所以ActiveMQ提供 了master-slave、broker cluster等多种部署方式,但通过分析多种部署方式之后我认为...

    ActiveMQ in Action pdf英文版+源代码

    ActiveMQ in Action pdf英文原版加源代码压缩包。 Apache ActiveMQ in Action is a thorough, practical guide to implementing message-oriented systems in Java using ActiveMQ. The book lays out the core of ...

    activemq, Apache ActiveMQ镜像.zip

    activemq, Apache ActiveMQ镜像 欢迎来到 Apache ActiveMQis是一个高性能的Apache 2.0许可以消息代理和 JMS 1.1实现。正在启动要帮助你入门,请尝试以下链接:入门http://activemq.apache.org/version-

    Spring集成ActiveMQ配置

    Spring 集 成ActiveMQ 配置 异步RPC框架 Missian ActiveMq-JMS简单实例使用tomcat

    activemq-protobuf-1.1-API文档-中英对照版.zip

    赠送jar包:activemq-protobuf-1.1.jar; 赠送原API文档:activemq-protobuf-1.1-javadoc.jar; 赠送源代码:activemq-protobuf-1.1-sources.jar; 包含翻译后的API文档:activemq-protobuf-1.1-javadoc-API文档-...

    springboot集成activemq实现消息接收demo

    springboot集成activemq实现消息接收demo

Global site tag (gtag.js) - Google Analytics