一、队列模式

生产者

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. mui的选项卡js选中指定项

    dom结构:在一定条件下想默认选中第二个选项卡 <div id="segmentedControl" class="mui-segmented-control mu ...

  2. 最短路径:HDU2006-一个人的旅行(多个起点,多个终点)

    题目: 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. 解决Uva网站打开慢的问题

    https://blog.csdn.net/richenyunqi/article/details/80990535

  4. Debug调试文件

    在debug.h中设置g_debug_switch即可控制调试级别. /* debug.c */ #include "debug.h" const char *get_log_le ...

  5. ZOJ Monthly, January 2018 训练部分解题报告

    A是水题,此处略去题解 B - PreSuffix ZOJ - 3995 (fail树+LCA) 给定多个字符串,每次询问查询两个字符串的一个后缀,该后缀必须是所有字符串中某个字符串的前缀,问该后缀最 ...

  6. vrpie在Visio Studio 中无法调试的问题

    最近这这几天一直在研究vrpie,之前不能调试,一调试就出问题,打开那个生成的htm文件是没问题的,最初的解决方法是不通过调试来打可那个htm页面,但是这样比较麻烦,因为经常需要和服务器交互,就只能用 ...

  7. loj2057 「TJOI / HEOI2016」游戏

    记横联通是一块横着的没有硬石头的地,把他们编号.竖联通同理. 对于一个空地,将其横联通编号和竖联通编号连边,二分图匹配,最大匹配为答案. #include <iostream> #incl ...

  8. loj2043 「CQOI2016」K 远点对

    k-d tree 裸题------ #include <algorithm> #include <iostream> #include <cstdio> using ...

  9. 【 Sqrt(x) 】cpp

    题目: Implement int sqrt(int x). Compute and return the square root of x. 代码: class Solution { public: ...

  10. PHP 获取上一个页面的url

    php $_SERVER["HTTP_REFERER"]变量可以获取上一个或前一个页面的URL地址. 比如有一个a.php页面,这个页面上有一个链接指向b.php页面,如果我们在a ...