ActiveMQ是apache的一个开源消息引擎。可以作为即通引擎或者消息中间件引擎。

准备

下载ActiveMQ

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

进入\bin\win64双击InstallService.bat安装为系统服务。然后启动这个系统服务

访问 http://localhost:8161/admin/queues.jsp 可以看到消息队列列表 账号密码默认就是admin admin,配置在conf/jetty-realm.properties中

然后将根目录下的activemq-all-5.13.2.jar引入项目,就可以进行开发了

首先对ActiveMQ进行简单封装

 package activeMQStu;

 import org.apache.activemq.ActiveMQConnectionFactory;

 import javax.jms.*;
import java.util.Arrays; /**
* Created by lvyahui on 2016/4/23.
*/
public class ActiveMQServer { private String user;
private String pass;
private String url; private Session session;
private Connection connection;
private ActiveMQConnectionFactory connectionFactory; public ActiveMQConnectionFactory getConnectionFactory() {
return connectionFactory;
} public ActiveMQServer(String user, String pass, String url) throws JMSException {
this.user = user;
this.pass = pass;
this.url = url; this.connectionFactory = new ActiveMQConnectionFactory(this.user, this.pass, this.url);
/*
* 必须指明哪些包的类是可以序列化的
* 否则会报错:ActiveMQ Serializable class not available to broker. Reason: ClassNotFoundException
* 参考:http://activemq.apache.org/objectmessage.html
* */
connectionFactory.setTrustedPackages(Arrays.asList("activeMQStu"));
this.connection = connectionFactory.createConnection();
connection.start();
} public Session getSession() throws JMSException {
if (session == null) {
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
}
return session;
} public Queue createQueue(String name) throws JMSException {
return getSession().createQueue(name);
} public void close() throws JMSException {
if (session != null) {
session.commit();
session.close();
}
if (connection != null) {
connection.close();
}
} }

消息

 package activeMQStu;

 import java.io.Serializable;

 /**
* Created by lvyahui on 2016/4/23.
*/
public class User implements Serializable { private String username ;
private String password ;
private String salt ; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getSalt() {
return salt;
} public void setSalt(String salt) {
this.salt = salt;
} @Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", salt='" + salt + '\'' +
'}';
}
}

消息生产者

 package activeMQStu.queue;

 import activeMQStu.ActiveMQServer;
import activeMQStu.User;
import org.apache.activemq.command.ActiveMQObjectMessage; import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue; /**
* Created by lvyahui on 2016/4/23.
*/
public class UserProducer { private MessageProducer producer; public UserProducer(ActiveMQServer activeMQServer, String queueName) throws JMSException {
Queue queue = activeMQServer.createQueue(queueName);
producer = activeMQServer.getSession().createProducer(queue);
} public void produce(User user) throws JMSException {
ObjectMessage objectMessage = new ActiveMQObjectMessage();
objectMessage.setObject(user);
producer.send(objectMessage);
}
}

发送消息

 package activeMQStu;

 import activeMQStu.queue.UserProducer;
import org.apache.activemq.ActiveMQConnection; import javax.jms.JMSException; /**
* Created by lvyahui on 2016/4/23.
*/
public class ProduceApp {
public static void main(String[] args) throws JMSException {
ActiveMQServer activeMQServer = new ActiveMQServer(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616"
);
UserProducer producer = new UserProducer(activeMQServer,"queue.devlyh");
for(int i =0 ;i < 100;i++){
User user = new User();
user.setUsername("lvyahui".concat(String.valueOf(i)));
user.setPassword("admin888" + i);
user.setSalt("salt"+i);
producer.produce(user);
}
activeMQServer.close();
}
}

运行成功后再页面可以看到有100条消息进入了名字为queue.devlyh的队列中

消息接受者

 package activeMQStu.queue;

 import activeMQStu.ActiveMQServer;
import activeMQStu.User; import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.ObjectMessage; /**
* Created by lvyahui on 2016/4/23.
*/
public class UserConsumer {
private MessageConsumer consumer; public UserConsumer(ActiveMQServer activeMQServer,String queueName) throws JMSException {
this.consumer = activeMQServer.getSession()
.createConsumer(activeMQServer.createQueue(queueName));
} public User consume() throws JMSException {
ObjectMessage objectMessage = (ObjectMessage) consumer.receive();
User user = (User) objectMessage.getObject();
return user;
}
}

接收消息

 package activeMQStu;

 import activeMQStu.queue.UserConsumer;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.JMSException;
import java.util.Arrays; /**
* Created by lvyahui on 2016/4/23.
*/
public class ConsumeApp {
public static void main(String[] args) throws JMSException {
ActiveMQServer activeMQServer = new ActiveMQServer(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616"
); // ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) activeMQServer.getConnectionFactory(); UserConsumer consumer = new UserConsumer(activeMQServer,"queue.devlyh");
while(true){
User user = consumer.consume();
System.out.println(user);
}
}
}

执行效果如下

基于ActiveMQ的点对点收发消息的更多相关文章

  1. ActiveMQ利用ajax收发消息

    准备工作: 后台需要导包: activemq-all.jar activemq-web.jar jetty-all.jar 如果是maven项目: pom.xml <dependency> ...

  2. ActiveMQ消息队列从入门到实践(4)—使用Spring JMS收发消息

    Java消息服务(Java Message Service ,JMS)是一个Java标准,定义了使用消息代理的通用API .在JMS出现之前,每个消息代理都有私有的API,这就使得不同代理之间的消息代 ...

  3. ActiveMQ点对点的消息发送案例

    公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...

  4. 【ActiveMQ】ActiveMQ在Windows的安装,以及点对点的消息发送案例

    公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...

  5. ActiveMQ学习笔记(5)——使用Spring JMS收发消息

      摘要 ActiveMQ学习笔记(四)http://my.oschina.net/xiaoxishan/blog/380446 中记录了如何使用原生的方式从ActiveMQ中收发消息.可以看出,每次 ...

  6. 通过集群的方式解决基于MQTT协议的RabbitMQ消息收发

    在完成了基于AMQP协议的RabbitMQ消息收发后,我们要继续实现基于MQTT协议的RabbitMQ消息收发. 由于C#的RabbitMQ.Client包中只实现了基于AMQP协议的消息收发功能的封 ...

  7. 17 个方面,综合对比 Kafka、RabbitMQ、RocketMQ、ActiveMQ 四个分布式消息队列

    原文:https://mp.weixin.qq.com/s/lpsQ3dEZHma9H0V_mcxuTw 一.资料文档 二.开发语言 三.支持的协议 四.消息存储 五.消息事务 六.负载均衡 七.集群 ...

  8. 综合对比 Kafka、RabbitMQ、RocketMQ、ActiveMQ 四个分布式消息队列

    来源:http://t.cn/RVDWcfe 一.资料文档 Kafka:中.有kafka作者自己写的书,网上资料也有一些.rabbitmq:多.有一些不错的书,网上资料多.zeromq:少.没有专门写 ...

  9. ActiveMQ的几种消息持久化机制

    为了避免意外宕机以后丢失信息,需要做到重启后可以恢复消息队列,消息系统一般都会采用持久化机制. ActiveMQ的消息持久化机制有JDBC,AMQ,KahaDB和LevelDB,无论使用哪种持久化方式 ...

随机推荐

  1. 安装、配置JDK的步骤

    1.配置环境变量,打开我的电脑--属性--高级--环境变量,新建系统变量JAVA_HOME .变量值:jdk的目录,比如d:/java.选择“系统变量”中变量名为“Path”的环境变量双击该变量,把J ...

  2. 【开发工具 - Git】之Git版本回退

    这篇博客主要记录了关于 查看记录.版本回退.添加标签.删除文件 的操作 1.查看文件修改情况: 可以通过 git diff a.java查看a.java文件自从上次提交后的修改情况,如果自从上次提交之 ...

  3. RSA客户端js加密服务器C#解密(含源码)

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  4. Oracle Linux 6.3下安装Oracle 11g R2(11.2.0.3)

    本文主要描写叙述了在Oracle Linux 6.3下安装Oracle 11gR2(11.2.0.3).从Oracle 11g開始,Oracle官方站点不再提供其Patch的下载链接,须要使用Meat ...

  5. Android - TextView Ellipsize属性

    Android - TextView Ellipsize属性 本文地址: http://blog.csdn.net/caroline_wendy android:ellipsize属性: If set ...

  6. Chapter 1 Securing Your Server and Network(6):为SQL Server訪问配置防火墙

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/38082123,专题文件夹:http://blog.csdn.net/dba_huangz ...

  7. 【转】cocos2d-x 模仿计时器效果,动态增加分数——2013-08-25 16

    http://www.cocos2dev.com/?p=90 游戏中要用到分数是动态增加的,而不是瞬间加上去的. bool HelloWorld::init() { if ( !CCLayer::in ...

  8. 通过NSURLProtocol拦截HTTP转HTTPS来整合SPDY的记录

    众所周知,iOS 9.0之后苹果引入ATS限制,苹果也推荐尽量不要使用HTTP通讯了,毕竟是很不安全的.而国内各个有(wu)节操的运营商也会经常篡改请求HTTP请求.所以如果可能,在不影响性能的情况下 ...

  9. Linux Ubuntu上架设FTP

    操作系统:ubuntu (GNU/Linux) 为了在机子上架设ftp服务器,我们需要安装ftp服务器软件.Linux下具有代表性的ftp服务器软件有Wu-FTP,ProFTP和Vsftp.Wu-FT ...

  10. YII中的表单挂件

    利用助手(widget)在页面实现表单 控制器中 <?php class YiiFormController extends Controller { public function actio ...