Apache ActiveMQ是Apache软件基金会所研发的开放源码消息中间件;由于ActiveMQ是一个纯Java程式,因此只需要操作系统支援Java虚拟机,ActiveMQ便可执行。

支持Java消息服务 (JMS) 1.1 版本
Spring Framework
集群 (Clustering)
支持的编程语言包括:C、C++、C#、Delphi、Erlang、Adobe Flash、Haskell、Java、JavaScript、Perl、PHP、Pike、Python和Ruby [1]
协议支持包括:OpenWire、REST、STOMP、WS-Notification、XMPP以及AMQP

好,我们先写个demo来试试 ActiveMQ的效果.

首先我们要下载ActiveMQ,下载地址:

http://www.apache.org/dyn/closer.cgi?path=/activemq/apache-activemq/5.8.0/apache-activemq-5.8.0-bin.zip

解压后,在x:/apache-activemq-5.8.0-bin/bin 目录下执行 activemq.bat即可启动 ActiveMQ,

由于ActiveMQ内置了Jetty web服务器,当ActiveMQ启动成功后,可以通过:http://localhost:8161/admin/访问ActiveMQ的控制台,默认的用户名和密码是:admin。

至此ActiveMQ 服务已经启动了,接下来我们先将x:/apache-activemq-5.8.0-bin/activemq-all-5.8.0.jar拷贝到你的classpath目录下,利用Java写个demo来尝试一下这个消息中间件。

一、显示发送者的对象

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session; import org.apache.activemq.ActiveMQConnectionFactory; public class Sender { public static void main(String[] args) throws Exception {
aa();
}
public static void aa() throws Exception{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); Connection connection = connectionFactory.createConnection();
connection.start(); Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("Test.foo"); MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
for(int i=0; i<100; i++) {
int id = i+1;
ObjectMessage message = session.createObjectMessage();
message.setObject(new User(id, "张三"+id, "123456"));
producer.send(message);
}
System.out.println("ok...");
session.commit();
session.close();
connection.close();
}
}

二、接受者对象

import java.util.concurrent.TimeUnit;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session; import org.apache.activemq.ActiveMQConnectionFactory; public class Receiver { public static void aa() throws Exception{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); Connection connection = connectionFactory.createConnection();
connection.start(); final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("Test.foo"); MessageConsumer consumer = session.createConsumer(destination);
//listener 方式
consumer.setMessageListener(new MessageListener() { public void onMessage(Message msg) {
ObjectMessage message = (ObjectMessage) msg;
//TODO something....
try {
User user = (User) message.getObject();
System.out.println("收到消息:"+user.getName());
} catch (JMSException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
session.commit();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} });
TimeUnit.MINUTES.sleep(1); session.close();
connection.close();
} public static void main(String[] args) throws Exception{
aa();
}
}

  

 import java.io.Serializable;

 public class User implements Serializable {

     private int id;

     private String name;

     private String pwd;

     public User(int id,String name,String pwd){
this.id=id;
this.name=name;
this.pwd=pwd;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} }

先运行发送者,然后运行接收者即可。。。。

Apache ActiveMQ消息中间件的基本使用的更多相关文章

  1. 消息队列MQ - Apache ActiveMQ

    Apache ActiveMQ是Apache软件基金会所研发的开放源码消息中间件:由于ActiveMQ是一个纯Jave程式,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行. 1.que ...

  2. spring boot整合activemq消息中间件

    spring boot整合activemq消息中间件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  3. ActiveMQ消息中间件的作用以及应用场景

    ActiveMQ消息中间件的作用以及应用场景 一.ActiveMQ简介 ActiveMQ是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ是一个完全支持JMS1.1和J2EE1.4 ...

  4. Apache ActiveMQ漏洞笔记

    0x00 简介 Apache ActiveMQ是美国阿帕奇(Apache)软件基金会所研发的一套开源的消息中间件,它支持Java消息服务.集群.Spring Framework等. 0x01 环境搭建 ...

  5. ActiveMQ消息中间件Producer和Consumer

    ActiveMQ消息中间件Producer和Consumer 原创jethai2015-08-18 18:08:56评论(0)1480人阅读   生产者代码: 1 2 3 4 5 6 7 8 9 10 ...

  6. Apache ActiveMQの版本更迭和Apache ActiveMQの故障转移

    本文描述apache activemq 版本更迭的原因以及Apache ActiveMQのThe Failover Transport new features in 5.2.0  1.对信息的传输/ ...

  7. apache activemq的重连

    1.activemq的重连机制 maxReconnectAttempts -1 | 0 From version 5.6 onwards: -1 is default and means retry ...

  8. apache activemq 学习笔记

    0.activemq的概念 activemq实现了jms(java Message server),用于接收,发送,处理消息的开源消息总线. 1.activemq和jms的区别 jms说白了就是jav ...

  9. How to Setup Replicated LevelDB Persistence in Apache ActiveMQ 5.9--转载

    原文地址:https://simplesassim.wordpress.com/2013/11/03/how-to-setup-replicated-leveldb-persistence-in-ap ...

随机推荐

  1. using(){},Close(),Dispose()的区别

    //用Close(),Dispose()方式关闭连接 string connString = "Data Source=(local);Initial Catalog=Linq;Integr ...

  2. 使用jeesite org.springframework.beans.NotReadablePropertyException: Invalid property 'tfxqCmsAccount.id' of bean class

    一对多 对子表添加时在form表单 path="tfxqCmsAccount.id"页面报错,对比了下其他可行的,发现其自动生成的子类少了个构造方法 加上 public TfxqC ...

  3. Batch file Functions

    Quote from: http://ss64.com/nt/syntax-functions.html Batch file Functions Packaging up code into a d ...

  4. PHP & Javascript 如何对字符串中包含html标签进行编码 整理

    为什么要对字符串编码? 某些字符串中包含html标签,不编码,页面输出就乱了. PHP下怎么对字符串编码? htmlentities vs htmlspecialchars htmlentities ...

  5. linux环境下安装php扩展

    本文只提供源码安装的方法(已安装pcntl为例) 其他方法请参考:http://doc3.workerman.net/appendices/install-extension.html 1.利用php ...

  6. php date('Y')

    date('Y')默认是y-12-01 date('Y-01')!!!才是我需要的

  7. IE6背景图片闪动问题

    在IE6中,当JS触发事件时或者hover的时候,如果网速过慢 IE6背景图片重新加载会闪一下. 好的一个解决方案是 <!--[if IE 6]><script> try{do ...

  8. Uploadify 上传失败

    更改Apache的php.ini的配置 In my php.ini (created custom ini file in public_html) would this solve this pro ...

  9. pdo如何防止 sql注入

    我们使用传统的 mysql_connect .mysql_query方法来连接查询数据库时,如果过滤不严,就有SQL注入风险,导致网站被攻击,失去控制.虽然可以用 mysql_real_escape_ ...

  10. 大量字段表单在PHP便捷处理分享

    关于程序开发中的表单批量提交策略很多时候一个表单太多的字段,如何能够高效获取表单字段,也为如何提神开发的效率和统一性? 比如一个系统的某个有26个字段,那么我用表单的名称用26个a到z的字母, 你是选 ...