消息队列的作用

为什么使用ActiveMQ,不使用其他工具

下载安装包并启动

http://localhost:8161/admin/ (账号:admin:admin)

Java实现步骤:

// 1.创建连接工厂对象(ConnectionFactory)
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(URL);
// 2.创建连接对象(Connection)
Connection connection = connectionFactory.createConnection();
// 3.启动连接
connection.start();
// 4.创建session会话,第一参数表示启用事务处理,第二个参数表示启动哪种应答模式,这里启用的是自动应答
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 5.创建目的地(queue/topic)
Queue queue = session.createQueue(QUEUE_NAME);
// 6.创建生产者/消费者
MessageProducer producer = session.createProducer(queue);
// 7.生产/消费消息
producer.send(message);

生产者代码:

public class MQProducer {
private static final String URL = "tcp://localhost:61616";
private static final String QUEUE_NAME = "queue-test";
private static final String TOPIC_NAME = "topic-test"; public static void main(String[] args) throws JMSException {
ConnectionFactory ConnectionFactory = new ActiveMQConnectionFactory(URL);
Connection connection = ConnectionFactory.createConnection();
connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue destination = session.createQueue(QUEUE_NAME);
// Topic destination = session.createTopic(TOPIC_NAME); // Topic消息
MessageProducer producer = session.createProducer(destination);
for(int i = 0; i < 10; i++) {
TextMessage message = session.createTextMessage("Message" + i);
producer.send(message);
System.out.println("Sent message" + i);
} producer.close();
session.close();
connection.close();
}
}

消费者代码:

public class MQConsumer {
private static final String URL = "tcp://localhost:61616";
private static final String QUEUE_NAME = "queue-test";
private static final String TOPIC_NAME = "topic-test"; public static void main(String[] args) throws JMSException, InterruptedException { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(URL);
Connection connection = connectionFactory.createConnection(); // 消费者1 : Queue & Topic Consumer
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue destination = session.createQueue(QUEUE_NAME); // Topic destination = session.createTopic(TOPIC_NAME);
MessageConsumer consumer = session.createConsumer(destination); // 消费者2 : Topic Subscriber
// connection.setClientID("client-test");
// connection.start();
// Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Topic destination = session.createTopic(TOPIC_NAME);
// TopicSubscriber consumer = session.createDurableSubscriber(destination, "subscription-test"); // 接收消息1 : MessageListener异步接收消息
consumer.setMessageListener(new MessageListener(){
@Override
public void onMessage(Message message) {
try {
System.out.println("Received " + ((TextMessage) message).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
Thread.sleep(10000); // 主线程等待一段时间后结束 // 接收消息2 : receive同步阻塞等待消息
// for(int i = 0; i < 10; i++) {
// TextMessage message = (TextMessage) consumer.receive();
// System.out.println("Received " + message.getText());
// } consumer.close();
session.close();
connection.close();
}
}

Producer consumer receive messageListener

topic

queue

JMS

消息可靠性之持久化  JDBC

topic持久化

事务

签收

点对点发布订阅

spring整合activemq

zookeeper+replicated levelDB

异步投递

延迟投递

定时投递

重试机制

死信队列

防止重复调用

ActiveMQ基础的更多相关文章

  1. ActiveMQ基础教程----简单介绍与基础使用

    概述 ActiveMQ是由Apache出品的,一款最流行的,能力强劲的开源消息总线.ActiveMQ是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,它非常快速,支持多 ...

  2. ActiveMQ基础使用

    概述 ActiveMQ是由Apache出品的,一款最流行的,能力强劲的开源消息总线.ActiveMQ是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,它非常快速,支持多 ...

  3. ActiveMQ基础教程(四):.net core集成使用ActiveMQ消息队列

    接上一篇:ActiveMQ基础教程(三):C#连接使用ActiveMQ消息队列 这里继续说下.net core集成使用ActiveMQ.因为代码比较多,所以放到gitee上:https://gitee ...

  4. ActiveMQ基础教程(三):C#连接使用ActiveMQ消息队列

    接上一篇:ActiveMQ基础教程(二):安装与配置(单机与集群) 安装部署好集群环境:192.168.209.133:61616,192.168.209.134:61616,192.168.209. ...

  5. 成小胖学习ActiveMQ·基础篇

    过了个春节,回到公司的成小胖变成了成大胖.但是你们千万别以为他那个大肚子里面装的都是肥肉,里面的墨水也多了不少嘞,毕竟成小胖利用春节的半个月时间专心学习并研究了 ActiveMQ,嘿嘿……这不,为了检 ...

  6. ActiveMQ基础教程(二):安装与配置(单机与集群)

    因为本文会用到集群介绍,因此准备了三台虚拟机(当然读者也可以使用一个虚拟机,然后使用不同的端口来模拟实现伪集群): 192.168.209.133 test1 192.168.209.134 test ...

  7. ActiveMQ基础教程(一):认识ActiveMQ

    ActiveMQ是Apache软件基金会所研发开源的消息中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信. 现在的消息队列有不少,RabbitMQ.Kafka.RocketMQ,Z ...

  8. ActiveMQ基础简介

    1. 什么是ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现 ...

  9. ActiveMQ基础01——Linux下载安装ActiveMQ

    1.下载 下载地址:http://activemq.apache.org/ 点击按钮 下载Linux下最新版安装包,点击即可下载 2.安装ActiveMQ 将之前下载的安装包上传到linux当中,一般 ...

随机推荐

  1. 【剑指offer】1+….+n,不能使用相关关键字

    题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 分析:可以使用递归! class Soluti ...

  2. Gin上传文件到MongoDB gridfs

    目录 上传图片 下载图片 上传图片 func imagePost(c *gin.Context) { image, err := c.FormFile("file") if err ...

  3. SQL Join连接大小表在前在后的重要性(小表在前提高执行效率)

    引用地址:https://blog.csdn.net/qq_30349961/article/details/82662550 http://blog.sina.com.cn/s/blog_6ff05 ...

  4. js调用浏览器下载

    $scope.Download = function (url) { var save_link = document.createElementNS("http://www.w3.org/ ...

  5. 01、MySQL_简介

    数据库概念 数据库(Database)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库. 数据库:存储数据的仓库 数据库分类 网络数据库 网络数据库是指把数据库技术引入到计算机网络系 ...

  6. nginx-ingress之server-snippet用法

    apiVersion: extensions/v1beta1 kind: Ingress metadata: annotations: nginx.ingress.kubernetes.io/serv ...

  7. Android EventBus使用大全

    添加依赖 implementation 'org.greenrobot:eventbus:3.1.1' public class HuaDongActivity extends Activity { ...

  8. php文字转语音

    使用百度接口 https://ai.baidu.com/docs#/TTS-Online-PHP-SDK/top 使用PHP SDK开发骤如下: 1.在官方网站下载php SDK压缩包. 2.将下载的 ...

  9. 【故障处理】ORA-19809错误处理

    [故障处理]ORA-19809错误处理 一.1  BLOG文档结构图       一.2  前言部分 一.2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它 ...

  10. zookeeper的安装使用

    转载从:https://blog.csdn.net/shenlan211314/article/details/6170717 一.zookeeper 介绍 ZooKeeper 是一个为分布式应用所设 ...