JMS + ActiveMQ 简单的demo
来源:互联网 发布:假丝绸出口数据 编辑:程序博客网 时间:2024/05/19 02:28
JMS两个主要概念:
消息中介
消息目标:
1、队列
2、主题
两种传递模式:
点对点消息传递模型:每个消息都有一个发送者和一个消费者
发送者-〉队列-〉接受者
发布-订阅者消息传递模型:一条订阅信息,可以发送给多个订阅者
发布者-〉主题-〉订阅者们
JMS优点:
不用等待
面向消息
位置独立
确保投送
消息接收者:
备注:
消息中介
消息目标:
1、队列
2、主题
两种传递模式:
点对点消息传递模型:每个消息都有一个发送者和一个消费者
发送者-〉队列-〉接受者
发布-订阅者消息传递模型:一条订阅信息,可以发送给多个订阅者
发布者-〉主题-〉订阅者们
JMS优点:
不用等待
面向消息
位置独立
确保投送
Apache-ActiveMQ:开源消息中介
先下载ActiveMQ,在网上找的版本是5.11.1,然后在bin中启动activeMQ.(备注:启动之前需要配置好环境变量,并且确定JDK的版本。我这边用的是64位的JDK)
启动后的照片如下:
访问的端口是:http://localhost:8161/ 可以直接进入admin界面,用户名和密码都是admin,界面如下:
经常会查看queues,topics,即为消息队列和订阅者发布者。
如果想要停掉服务在控制台需要按ctrl+shift+c,ctrl+c ,Y 。我这边是这样停的,也有人说去掉中间的ctrl+c。
编写代码如下:
消息生产者:
package com.liang.test;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;/** * 消息的发布者(发送者) */public class JMSProducer {private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; //默认连接用户名private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;//默认连接密码private static final String URL=ActiveMQConnection.DEFAULT_BROKER_URL;//默认连接地址private static final int SIZE=11; //发送的消息数量public static void main(String[] args) {ConnectionFactory factory;//连接工厂Connection connection;//连接Session session;//会话 接受或者发送消息的线程Destination destination;//消息的目的地MessageProducer messageProducer;//消息生产者factory=new ActiveMQConnectionFactory(USERNAME, PASSWORD, URL);//实例化连接工厂try {connection=factory.createConnection();//通过连接工厂获取连接connection.start();//启动连接session=connection.createSession(true, Session.AUTO_ACKNOWLEDGE);//创建session//destination = session.createQueue("testQueue1");//point to point//创建消息队列destination=session.createTopic("testTopic1");//topic //创建消息队列messageProducer=session.createProducer(destination);//创建消息生产者sendMessage(session, messageProducer);//发送消息session.commit();} catch (JMSException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 发送消息 */public static void sendMessage(Session session,MessageProducer messageProducer){for (int i = 0; i < SIZE; i++) {try {TextMessage message=session.createTextMessage("消息发布者,发布消息"+i);//创建一条文本消息messageProducer.send(message);//通过消息生产者发出消息} catch (JMSException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
消息接收者:
import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageConsumer;import javax.jms.Session;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class JMSConsumer1 {private static final String USERNAME=ActiveMQConnection.DEFAULT_USER;private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;private static final String URL=ActiveMQConnection.DEFAULT_BROKER_URL;public static void main(String[] args) {ConnectionFactory connectionFactory;Connection connection;Session session;Destination destination;MessageConsumer consumer;connectionFactory=new ActiveMQConnectionFactory(USERNAME, PASSWORD, URL);try {connection=connectionFactory.createConnection();connection.start();session=connection.createSession(true, Session.AUTO_ACKNOWLEDGE);// destination = session.createQueue("testQueue1"); //point to pointdestination=session.createTopic("testTopic1"); //topicconsumer=session.createConsumer(destination);/*while (true) { TextMessage textMessage = (TextMessage) messageConsumer.receive(100000); if(textMessage != null){ System.out.println("收到的消息:" + textMessage.getText()); }else { break; } }*/consumer.setMessageListener(new Listener1());} catch (JMSException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;public class Listener1 implements MessageListener {@Overridepublic void onMessage(Message message) {try {System.out.println("订阅者1:"+((TextMessage)message).getText());} catch (JMSException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
备注:
我测试的时候,消息队列的接收方是一直启动的。而发送方是发送完就停掉了。订阅者和发布者也是一样。要先启动订阅者,在启动发布者。
0 0
- JMS + ActiveMQ 简单的demo
- SpringMVC+JMS(ActiveMQ)的小Demo
- 一个简单的JMS实例(activeMQ)
- JMS(三)ActiveMQ简单的HerlloWord示例
- JMS(二)--ActiveMQ简单的HelloWorld实例
- JMS(三)--ActiveMQ简单的HelloWorld实例
- JMS Activemq实战例子demo
- activeMQ jms 简单实现
- jms之ActiveMQ简单入门
- JMS之ActiveMQ简单实现
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 3、深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- JMS学习(3):--ActiveMQ简单的HelloWorld实例
- 远程连接服务器
- 获取url中的参数值
- 录音文件保存失败
- jquery eazyUI的下拉树异步实现
- 用JS实现省市区三级联动 (数据从数据库查出)
- JMS + ActiveMQ 简单的demo
- [leetcode] 390. Elimination Game
- 安卓中GridView填充简单的文字和图片,笔记!
- python的取整函数:向上取整,向下取整,四舍五入取整
- Dubbo实现RPC调用使用入门
- 导出有索引大表的步骤(sql方法)。
- Java多线程综述
- Python 多层装饰器
- python实现log日志