一、队列模式

生产者

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.ActiveMQConnectionFactory; public class AppProducer {
public static final String url = "tcp://127.0.0.1:61616";
public static final String queueName = "queue-test"; public static void main(String[] args) throws JMSException{
//1. 创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( url); //2. 创建Connection
Connection connection = connectionFactory.createConnection(); //3. 启动链接
connection.start(); //4. 创建会话
Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE); //5. 创建一个目标
Destination destination = session.createQueue( queueName); //6. 创建一个生产者
MessageProducer producer = session.createProducer( destination); for( int i=; i<; i++){
//7. 创建消息
TextMessage textMessage = session.createTextMessage( "test" + i);
//8. 发布消息
producer.send( textMessage); System.out.println( "发送消息" + textMessage.getText());
} //9. 关闭链接
connection.close();
} }

消费者

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class AppConsumer {
public static final String url = "tcp://127.0.0.1:61616";
public static final String queueName = "queue-test"; public static void main(String[] args) throws JMSException{
//1. 创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( url); //2. 创建Connection
Connection connection = connectionFactory.createConnection(); //3. 启动链接
connection.start(); //4. 创建会话
Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE); //5. 创建一个目标
Destination destination = session.createQueue( queueName); //6. 创建一个消费者
MessageConsumer consumer = session.createConsumer( destination); //7. 创建一个监听器
consumer.setMessageListener( new MessageListener() {
@Override
public void onMessage(Message message) {
TextMessage textMessage = ( TextMessage) message;
try{
System.out.println( "0接收消息" + textMessage.getText());
}catch( JMSException e){
e.printStackTrace();
} }
}); //8. 关闭链接
//connection.close();
}
}

二、主题模式

生产者

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.ActiveMQConnectionFactory; public class AppProducer {
public static final String url = "tcp://127.0.0.1:61616";
public static final String topicName = "topic-test"; public static void main(String[] args) throws JMSException{
//1. 创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( url); //2. 创建Connection
Connection connection = connectionFactory.createConnection(); //3. 启动链接
connection.start(); //4. 创建会话
Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE); //5. 创建一个目标
Destination destination = session.createTopic( topicName); //6. 创建一个生产者
MessageProducer producer = session.createProducer( destination); for( int i=0; i<100; i++){
//7. 创建消息
TextMessage textMessage = session.createTextMessage( "test" + i);
//8. 发布消息
producer.send( textMessage); System.out.println( "发送消息" + textMessage.getText());
} //9. 关闭链接
connection.close();
} }

消费者

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class AppConsumer {
public static final String url = "tcp://127.0.0.1:61616";
public static final String topicName = "topic-test"; public static void main(String[] args) throws JMSException{
//1. 创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( url); //2. 创建Connection
Connection connection = connectionFactory.createConnection(); //3. 启动链接
connection.start(); //4. 创建会话
Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE); //5. 创建一个目标
Destination destination = session.createTopic( topicName); //6. 创建一个消费者
MessageConsumer consumer = session.createConsumer( destination); //7. 创建一个监听器
consumer.setMessageListener( new MessageListener() {
@Override
public void onMessage(Message message) {
TextMessage textMessage = ( TextMessage) message;
try{
System.out.println( "0接收消息" + textMessage.getText());
}catch( JMSException e){
e.printStackTrace();
} }
}); //8. 关闭链接
//connection.close();
}
}

三、activeMQ的maven依赖

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>

activeMQ队列模式和主题模式的Java实现的更多相关文章

  1. (八)RabbitMQ消息队列-通过Topic主题模式分发消息

    原文:(八)RabbitMQ消息队列-通过Topic主题模式分发消息 前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过 ...

  2. RabbitMQ (七) 订阅者模式之主题模式 ( topic )

    主题模式和路由模式很像 路由模式是精确匹配 主题模式是模糊匹配 依然先通过管理后台添加一个交换机. 生产者 public class Producer { private const string E ...

  3. ActiveMQ队列、主题模式区别

    1.ActiveMQ队列模式如下图,生产者创建消息到消息中间件,再“均分给消费者”. 2.ActiveMQ主题模式如下图,生产者创建消息到消息中间件,消费者会接受到订阅的主题中所有的消息.在主题模式下 ...

  4. ActiveMQ的两种消息模式,主题、队列

    1.开发的模式流程如下: 2.队列模式Queue 如果生产者产生了100条消息,那么两个消费同时在的话,会分工合作来接收这100条消息.就是每个消费者接收到50条来处理. 3.主题模式topic 如果 ...

  5. RabbitMQ六种队列模式-主题模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...

  6. ActiveMQ--模式(队列模式/主题模式)

    两种模式:队列模式/主题模式 pom.xml <dependency> <groupId>org.apache.activemq</groupId> <art ...

  7. RabbitMQ消息队列(八)-通过Topic主题模式分发消息(.Net Core版)

    前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过routingkey来匹配消息的模式已经有一定了解那fanout也很好 ...

  8. 队列模式&主题模式

    # RabbitMQ 消息中间件 **Advanced Message Queuing Protocol (高级消息队列协议** The Advanced Message Queuing Protoc ...

  9. Java并发(基础知识)—— 阻塞队列和生产者消费者模式

    1.阻塞队列                                                                                        Blocki ...

随机推荐

  1. pycharm配置Git托管

    利用Pycharm和github管理代码转载https://www.cnblogs.com/feixuelove1009/p/5955332.html git教程--廖雪峰git教程  转载https ...

  2. Entrez Direct

    安装 cd ~/bin/bashperl -MNet::FTP -e \'$ftp = new Net::FTP("ftp.ncbi.nlm.nih.gov", Passive = ...

  3. North American Invitational Programming Contest (NAIPC) 2016

    (待补) A. Fancy Antiques 爆搜. B. Alternative Bracket Notation C. Greetings! D. Programming Team 0/1分数规划 ...

  4. 笔记-python-装饰器

    笔记-python-装饰器 1.  装饰器 装饰器的实质是返回的函数对象的函数,其次返回的函数对象是可以调用的,搞清楚这两点后,装饰器是很容易理解的. 1.1.  相关概念理解 首先,要理解在Pyth ...

  5. 用 Tensorflow 建立 CNN

    稍稍乱入的CNN,本文依然是学习周莫烦视频的笔记. 还有 google 在 udacity 上的 CNN 教程. CNN(Convolutional Neural Networks) 卷积神经网络简单 ...

  6. angular用$sce服务来过滤HTML标签

    angular js的强大之处之一就是他的数据双向绑定这一牛B功能,我们会常常用到的两个东西就是ng-bind和针对form的ng-model.但在我们的项目当中会遇到这样的情况,后台返回的数据中带有 ...

  7. cf984e Elevator

    ref我好菜啊 #include <iostream> #include <cstring> #include <cstdio> #include <cmat ...

  8. Intellij IDEA快捷键大全

    Intellij IDEA快捷键大全 Intellij IDEA这个工具有些方面确实比较优秀,使用了一段时间的IntelliJ IDEA,感觉这个JAVA IDE非常好用!比如javascript自动 ...

  9. Java并发之(1):volatile关键字(TIJ21-21.3.3 21.3.4)

    Java并发Java服务器端编程的一项必备技能. ** 1 简介    volatile是java中的一个保留关键字,它在英语中的含义是易变的,不稳定的.volatile像final.static等其 ...

  10. Excel动画教程50例(二)

    Excel动画教程50例(二) 16.用好Excel的“搜索函数” 17.在Excel中插入超级链接 18.在Excel中打印指定页面 19.在Excel中直接编辑“宏” 20.用窗体调用“宏” 21 ...