基于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,无论使用哪种持久化方式 ...
随机推荐
- 重要常用的Lunix命令
lunix 命令大全: http://man.linuxde.net/ 复制文件/文件夹 复制到本地 文件夹:scp -r work@www.abc.com:/home/work/project / ...
- 分布式助手Zookeeper(一)
分布式助手Zookeeper(一)博客分类: Zookeeper Zookeeper最早是Hadoop的一个子项目,主要为Hadoop生态系统中一些列组件提供统一的分布式协作服务,在2010年10 ...
- 误删/tmp导致hadoop无法启停, jsp无法查看的解决方法
问题描述 我的hadoop版本是hadoop-cdh4.2.0,由于误删了/tmp目录(不是hadoop.tmp.dir设定的那个目录),在Namenode,SecondaryNamenode和Dat ...
- sigar监控
相关参照博客: http://liningjustsoso.iteye.com/blog/1254584 http://blog.csdn.net/aoxida/article/category/12 ...
- [Webpack 2] Ensure all source files are included in test coverage reports with Webpack
If you’re only instrumenting the files in your project that are under test then your code coverage r ...
- Kinect for Windows V2和V1对照开发___深度数据获取并用OpenCV2.4.10显示
V1深度分辨率:320x240 V2深度分辨率:512x424 1. 打开深度图像帧的方式 对于V1: hr = m_PNuiSensor->NuiImageStreamOpen( NUI_I ...
- 进程控制之waitid函数
Single UNIX Specification的XSI扩展包括了另一个取进程终止状态的函数--waitid,此函数类似于waitpid,但提供了更多的灵活性. #include <sys/w ...
- air2调用本地exe的文章
流传了两种配置app.xml的方法,分别是: <supportedProfiles>extendedDesktop</supportedProfiles> <suppo ...
- C++ notes for beginners
作者:马 岩(Furzoom) (http://www.cnblogs.com/furzoom/)版权声明:本文的版权归作者与博客园共同所有.转载时请在明显地方注明本文的详细链接,未经作者同意请不要删 ...
- UPDATE---修改表中数据
UPDATE table_name SET column1=value1,column2=value2,... [WHERE conditions]; 例: UPDATE userinfo SET n ...