基于ActiveMQ的点对点收发消息
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的点对点收发消息的更多相关文章
- ActiveMQ利用ajax收发消息
准备工作: 后台需要导包: activemq-all.jar activemq-web.jar jetty-all.jar 如果是maven项目: pom.xml <dependency> ...
- ActiveMQ消息队列从入门到实践(4)—使用Spring JMS收发消息
Java消息服务(Java Message Service ,JMS)是一个Java标准,定义了使用消息代理的通用API .在JMS出现之前,每个消息代理都有私有的API,这就使得不同代理之间的消息代 ...
- ActiveMQ点对点的消息发送案例
公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...
- 【ActiveMQ】ActiveMQ在Windows的安装,以及点对点的消息发送案例
公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...
- ActiveMQ学习笔记(5)——使用Spring JMS收发消息
摘要 ActiveMQ学习笔记(四)http://my.oschina.net/xiaoxishan/blog/380446 中记录了如何使用原生的方式从ActiveMQ中收发消息.可以看出,每次 ...
- 通过集群的方式解决基于MQTT协议的RabbitMQ消息收发
在完成了基于AMQP协议的RabbitMQ消息收发后,我们要继续实现基于MQTT协议的RabbitMQ消息收发. 由于C#的RabbitMQ.Client包中只实现了基于AMQP协议的消息收发功能的封 ...
- 17 个方面,综合对比 Kafka、RabbitMQ、RocketMQ、ActiveMQ 四个分布式消息队列
原文:https://mp.weixin.qq.com/s/lpsQ3dEZHma9H0V_mcxuTw 一.资料文档 二.开发语言 三.支持的协议 四.消息存储 五.消息事务 六.负载均衡 七.集群 ...
- 综合对比 Kafka、RabbitMQ、RocketMQ、ActiveMQ 四个分布式消息队列
来源:http://t.cn/RVDWcfe 一.资料文档 Kafka:中.有kafka作者自己写的书,网上资料也有一些.rabbitmq:多.有一些不错的书,网上资料多.zeromq:少.没有专门写 ...
- ActiveMQ的几种消息持久化机制
为了避免意外宕机以后丢失信息,需要做到重启后可以恢复消息队列,消息系统一般都会采用持久化机制. ActiveMQ的消息持久化机制有JDBC,AMQ,KahaDB和LevelDB,无论使用哪种持久化方式 ...
随机推荐
- svn2git使用小记
Github强烈推荐使用svn2git工具将svn repository转成git repository: https://help.github.com/articles/importing-fro ...
- jbpm4.4 demo1
package cn.itcast.a_helloworld; import java.util.List; import org.jbpm.api.Configuration; import org ...
- 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 ...
- Python+Django+SAE系列教程17-----authauth (认证与授权)系统1
通过session,我们能够在多次浏览器请求中保持数据,接下来的部分就是用session来处理用户登录了. 当然,不能仅凭用户的一面之词,我们就相信,所以我们须要认证. 当然了,Django 也提供了 ...
- STM8S TIM4库函数应用
void TIM4_TimerInit(u8 Timer4Time) { assert_param(IS_TIM4TIMERTIME_OK(Timer4Time)); TIM4_DeInit();// ...
- 我的Android开发相关文章
Pro Android学习笔记: Pro Android学习笔记(一零七):2D动画(2):layout渐变动画 2014.7.25 Pro Android学习笔记(一零六):2D动画(1):fram ...
- 【转】开发者分享如何创造一款优秀的iOS游戏——2013-08-25 17
http://game.dapps.net/gamedev/experience/889.html 创造出<Temple Run>的夫妻团队在高峰时期每天能够获得"好几万&quo ...
- gdb调试程序
一.准备好内容vim test3.c 输入如下即可 #include <stdio.h> int func(int n) { int sum=0,i; f ...
- [RAC] oracle rac 后台进程
一.RAC后台进程 LMON:LOCK Monitor Processes 也被称为Global enqueue service monitor 监控整个集群状况,维护GCS的内存结构 监控非正常终止 ...
- ERROR 1114 (HY000): The table 'adv_date_tmp' is full(Mysql临时表应用)
场景:需要对现在数据库的数据进行批量的进行is_del=1的操作,但是遇到一个问题,在执行sql的时候发现sql不能在查询特定表的时候再嵌套查询来做update的操作,经过讨论,后续我们想到用临时表的 ...