ActiveMQ( 消息队列)
来源:互联网 发布:c语言实战项目开发 编辑:程序博客网 时间:2024/06/16 10:59
分析
- MQ : Message Q (消息队列中间件)
- ActiveMQ 是Apache旗下Java开发的开源消息中间件,完全支持
JMS 1.1规范: Java Message Service,J2ee1.4规范,ActiveMQ相当于JMS的实现, 只要支持JMS1.1规范的工具,用法一样。- 支持Spring,可以很容易的内嵌到Spring
。。。。。
特点
支持多语言和协议编写客户端。语言: Java, C, C++, C#, Ruby, Perl, Python, PHP。应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
- 支持通过JDBC和journal提供高速的消息持久化
- 完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务)
- 过了常见J2EE服务器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上
- 从设计上保证了高性能的集群,客户端-服务器,点对点
- 支持Ajax,支持与Axis的整合
- 可以很容易得调用内嵌JMS provider,进行测试
消息形式
- 点对点:即一个生产者和一个消费者一一对应
- 发布和订阅模式:即一个生产者产生消息并进行发送后,可以由多个消费者进行接收。
- 五种不同的消息正文格式,以及消息类型
- treamMessage – Java原始值的数据流
· MapMessage–一套名称-值对
· TextMessage–一个字符串对象
· ObjectMessage–一个序列化的 Java对象
· BytesMessage–一个字节的数据流
安装
- 注意ActiveMQ( 消息队列)的版本,有些版本方法会不一致
启动
./activemq startINFO: Loading '/soft/apache-activemq-5.12.0//bin/env'INFO: Using java '/soft/jdk1.7.0_55/bin/java'INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get detailsINFO: pidfile created : '/soft/apache-activemq-5.12.0//data/activemq.pid' (pid '12992')
关闭
./activemq stop
访问
<!-- 默认端口为8161-->http://192.168.25.136:8161/ <!-- 默认登录用户和密码 都为admin-->
<!-- 如果访问Quenues和Topics报405错误,原因:这台服务器的机器名和你的IP地址没有对上,需要修改host文件-->cd /etc/hostvim /etc/hosts127.0.0.1 localhost<!-- 还不行的话或者-->vim /etc/sysconfig/networkNETWORKING=yesHOSTNAME=localhost.localdomain <!-- 这才是真正的机器名字,再到hosts文件配置一下映射关系即可 -->重启服务即可
Demo
@Test public void testQueueProducer() throws Exception { //1、创建一个连接工厂对象,需要指定服务的ip及端口。 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.161:61616"); //2、使用工厂对象创建一个Connection对象。 Connection connection = connectionFactory.createConnection(); //3、开启连接,调用Connection对象的start方法。 connection.start(); //4、创建一个Session对象。 //第一个参数:是否开启事务。如果true开启事务,第二个参数无意义。一般不开启事务false。 //第二个参数:应答模式。自动应答或者手动应答。一般自动应答。 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //5、使用Session对象创建一个Destination对象。两种形式queue、topic,现在应该使用queue Queue queue = session.createQueue("test-queue"); //6、使用Session对象创建一个Producer对象。 MessageProducer producer = session.createProducer(queue); //7、创建一个Message对象,可以使用TextMessage。 /*TextMessage textMessage = new ActiveMQTextMessage(); textMessage.setText("hello Activemq");*/ TextMessage textMessage = session.createTextMessage("hello activemq"); //8、发送消息 producer.send(textMessage); //9、关闭资源 producer.close(); session.close(); connection.close(); } @Test public void testQueueConsumer() throws Exception { //创建一个ConnectionFactory对象连接MQ服务器 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.161:61616"); //创建一个连接对象 Connection connection = connectionFactory.createConnection(); //开启连接 connection.start(); //使用Connection对象创建一个Session对象 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //创建一个Destination对象。queue对象 Queue queue = session.createQueue("test-queue"); //使用Session对象创建一个消费者对象。 MessageConsumer consumer = session.createConsumer(queue); //接收消息 consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { //打印结果 TextMessage textMessage = (TextMessage) message; String text; try { text = textMessage.getText(); System.out.println(text); } catch (JMSException e) { e.printStackTrace(); } } }); //等待接收消息 System.in.read(); //关闭资源 consumer.close(); session.close(); connection.close(); }
阅读全文
0 0
- ActiveMQ实现消息队列
- ActiveMQ 消息队列
- 队列消息ActiveMq
- 深入浅出 消息队列 ActiveMQ
- 消息队列中间件ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ .
- 消息队列ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 深入浅出 消息队列 ActiveMQ
- 消息队列------ActiveMQ
- 消息队列 ActiveMQ
- 消息队列 ActiveMQ
- android布局之Linearlayout(线性布局)
- Android模拟返回Back操作
- AVL树的插入操作(旋转)图解
- 周志华《Machine Learning》学习笔记(14)--计算学习理论
- php基于ob_start(ob_gzhandler)实现网页压缩功能的方法
- ActiveMQ( 消息队列)
- 使用PHP中的递归思想求出斐波那契数列
- android studio 3.0 更新套路
- Poj2352-树状数组或线段树
- TortoiseSVN使用详细步骤
- CentOS7.3编译安装MariaDB10.2.6
- css3
- nuitka用法
- POJ 2247 Humble Numbers 笔记