activeMQ队列模式和主题模式的Java实现
一、队列模式
生产者
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实现的更多相关文章
- (八)RabbitMQ消息队列-通过Topic主题模式分发消息
原文:(八)RabbitMQ消息队列-通过Topic主题模式分发消息 前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过 ...
- RabbitMQ (七) 订阅者模式之主题模式 ( topic )
主题模式和路由模式很像 路由模式是精确匹配 主题模式是模糊匹配 依然先通过管理后台添加一个交换机. 生产者 public class Producer { private const string E ...
- ActiveMQ队列、主题模式区别
1.ActiveMQ队列模式如下图,生产者创建消息到消息中间件,再“均分给消费者”. 2.ActiveMQ主题模式如下图,生产者创建消息到消息中间件,消费者会接受到订阅的主题中所有的消息.在主题模式下 ...
- ActiveMQ的两种消息模式,主题、队列
1.开发的模式流程如下: 2.队列模式Queue 如果生产者产生了100条消息,那么两个消费同时在的话,会分工合作来接收这100条消息.就是每个消费者接收到50条来处理. 3.主题模式topic 如果 ...
- RabbitMQ六种队列模式-主题模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...
- ActiveMQ--模式(队列模式/主题模式)
两种模式:队列模式/主题模式 pom.xml <dependency> <groupId>org.apache.activemq</groupId> <art ...
- RabbitMQ消息队列(八)-通过Topic主题模式分发消息(.Net Core版)
前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过routingkey来匹配消息的模式已经有一定了解那fanout也很好 ...
- 队列模式&主题模式
# RabbitMQ 消息中间件 **Advanced Message Queuing Protocol (高级消息队列协议** The Advanced Message Queuing Protoc ...
- Java并发(基础知识)—— 阻塞队列和生产者消费者模式
1.阻塞队列 Blocki ...
随机推荐
- 用iTerm快速链接远程服务器
通常情况下,iTerm2访问远程Linux使用ssh ssh <用户名>@<ip> 然后输入访问的密码即可.当然还有的时候需要指定访问端口. ssh -p <端口号> ...
- FLASH、SDRAM
1.flash: 闪存,掉电之后里面的存储数据不会丢失,在嵌入式系统中用作存储Bootloader以及操作系统或者程序代码或者直接当硬盘使用(U盘).一般主要使用的FLASH有NOR flash和NA ...
- LCS及方案数(DP)
Description 对于一个序列
- Spring---浅谈IOC
概念 IOC(Inversion of Control 控制反转)是spring的核心,贯穿始终.所谓IOC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系. 传统 ...
- 4 Template层- HTML转义
1.HTML转义 Django对字符串进行自动HTML转义,如在模板中输出如下值: 视图代码: def index(request): return render(request, 'temtest/ ...
- SpringMvc路径参数和url的两种实现方式
我们经常采用的SpringMvc路径参数经常的操作是在url后面采用?参数名=值1&参数名2=值2这种方式实现 RequestMapping的作用: 1)当作用在controller时,我们通 ...
- mongoTemplate聚合操作Demo
package com.tangzhe.mongodb.mongotemplate; import com.mongodb.BasicDBObject; import com.mongodb.DBOb ...
- synchronized同步方法和同步代码块的区别
同步方法默认使用this或者当前类做为锁. 同步代码块可以选择以什么来加锁,比同步方法更精确,我们可以选择只有会在同步发生同步问题的代码加锁,而并不是整个方法. 同步方法使用synchronized修 ...
- leetcode 【 Swap Nodes in Pairs 】python 实现
题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...
- Python常见数据类型及操作
基础数据类型 什么是数据类型? 我们人类可以很容易的分清数字与字符的区别,但计算机并不能,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,“汉”是文字,否则它是分不清1和‘汉’ ...