activemq消息队列的使用及应用docker部署常见问题及注意事项

docker用https://hub.docker.com/r/rmohr/activemq/
配置在/data/docker/activemq/conf
重启命令:docker restart activemq
查看进程:docker ps | grep activemq

$ docker ps | grep activemq
927860512db9 rmohr/activemq:5.15.4-alpine
从上面可以看到版本是activemq:5.15.4-alpine

================

要改activemq的默认配置:
用持久化消息,开启事务模式,将临时文件限制尽可能的调大。
将prefetch设为1,每次处理1条消息,处理完再去取

自己发送消息的逻辑代码,要在方法里加上try catch,避免因程序逻辑错误导致重连才行

activemq.xml

如果帐号在配置文件目录下没有权限上传覆盖文件可以采用sudo vi activemq.xml的命令进行编辑。

1.设置预取限制,指定topic消费者的预取限制。
<policyEntry topic=">" > 改为
<policyEntry topic=">" topicPrefetch="1">
broker为该主题最多保存1000条消息,如果消息数目超过了1000,旧消息将被丢弃
<constantPendingMessageLimitStrategy limit="1000"/>

2.去掉非必需的通信协议(Client与Broker、Broker与Broker之间使用该协议进行通信),只留下TCP协议(61616是broker的监听端口)

3.增加
<subscriptionRecoveryPolicy>
<!--恢复最近30分钟内的信息-->
<timedSubscriptionRecoveryPolicy recoverDuration="1800000"/>
</subscriptionRecoveryPolicy>

4.持久化默认文件大小32M改成128MB
<kahaDB directory="${activemq.data}/kahadb" journalMaxFileLength="128mb"/>

topicPrefetch="10" 这个今天测试了没达到效果,还要再测试下改其他配置
都启动后再发送的消息几个客户端都可以消费

5.持久化消息
activemq上配置使用<broker persistent="true"
生产者代码设置持久化
//设置持久化
producer.setDeliveryMode(DeliveryMode.PERSISTENT);

开启事务模式
//支持事务sender设置为true,receiver则必须设置为Boolean.FALSE, Session.AUTO_ACKNOWLEDGE 才行
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);

6.run.sh要把ca=`docker rmi sendemail-service`
echo $ca
改成
docker rmi sendemail-service -f &
echo 'docker rmi success'

activemq用的是默认的账号密码admin,admin,从安全角度出发的话,如果外网可以访问到,activemq的账号密码也需要改掉的,生产者,消费者也要用对应的账号密码连接,需要同时改配置和代码

=======================
目录:
一:JMQ的两种消息模式
1.1:点对点的消息模式
1.2:订阅模式
二:点对点的实现代码
2.1:点对点的发送端
2.2:点对点的接收端
三:订阅/发布模式的实现代码
3.1:订阅模式的发送端
3.2:订阅模式的接收端
四:发送消息的数据类型
4.1:传递javabean对象

4.2:发送文件
五:ActiveMQ的应用
5.1:保证消息的成功处理
5.2:避免消息队列的并发
5.3:消息有效期的管理
5.4:过期消息,处理失败的消息如何处理
六:ActiveMQ的安全配置
6.1:管理后台的密码设置

=====================

生产者(发送消息)Java代码:

  1. public static void sendMessage(String data) {
  2. ConnectionFactory connectionFactory; // ConnectionFactory--连接工厂,JMS用它创建连接
  3. // Provider 的连接
  4. Connection connection = null; // Connection :JMS 客户端到JMS
  5. Session session; // Session: 一个发送或接收消息的线程
  6. Destination destination; // Destination :消息的目的地;消息发送给谁.
  7. MessageProducer producer; // MessageProducer:消息发送者
  8. // 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
  9. connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
  10. ActiveMQConnection.DEFAULT_PASSWORD, SendSms.mqconnection);
  11. try { // 构造从工厂得到连接对象
  12. connection = connectionFactory.createConnection();
  13. // 启动
  14. connection.start();
  15. // 获取操作连接,生产者事务的要设置为TRUE才行
  16. session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
  17. // 创建队列名称testQueue
  18. destination = session.createQueue("testQueue");
  19. // 得到消息生成者【发送者】
  20. producer = session.createProducer(destination);
  21. // 设置持久化
  22. producer.setDeliveryMode(DeliveryMode.PERSISTENT);
  23. // 构造消息,项目就是参数,或者方法获取
  24. TextMessage message = session.createTextMessage(data);
  25. System.out.println("send data to testQueue");
  26. producer.send(message);
  27. session.commit();
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. } finally {
  31. try {
  32. if (null != connection)
  33. connection.close();
  34. } catch (Throwable ignore) {
  35. }
  36. }
  37. }

消费者(接收消息)Java代码:

  1. public static void receiverMessage() {
  2.  
  3. // ConnectionFactory :连接工厂,JMS 用它创建连接
  4. ConnectionFactory connectionFactory;
  5. // Connection :JMS 客户端到JMS Provider 的连接
  6. Connection connection = null;
  7. // Session: 一个发送或接收消息的线程
  8. Session session = null;
  9. // Destination :消息的目的地;消息发送给谁.
  10. Destination destination;
  11. // 消费者,消息接收者
  12. MessageConsumer consumer;
  13. connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
  14. ActiveMQConnection.DEFAULT_PASSWORD, SendSms.mqconnection);
  15. try {
  16. // 构造从工厂得到连接对象
  17. connection = connectionFactory.createConnection();
  18. // 启动
  19. connection.start();
  20. //receiver则必须设置为Boolean.FALSE, Session.AUTO_ACKNOWLEDGE 才行
  21. session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
  22. // 创建队列名称,需要跟sender的一致
  23. destination = session.createQueue("testQueue");
  24. consumer = session.createConsumer(destination);
  25. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  26. Date time = sdf.parse(sdf.format(new Date()));
  27. System.out.println("start listeningSmsQueueMessage " + time);
  28. while (true) {
  29. Date time2 = sdf.parse(sdf.format(new Date()));
  30. // 设置接收者接收消息的时间
  31. // TextMessage message = (TextMessage) consumer.receive(receivetime);
  32. TextMessage message = (TextMessage) consumer.receive();
  33. if (null != message) {
  34. System.out.println(time2 + "getSmsQueueMessage:" + message.getText());
  35. SendSms.sender(message.getText());
  36. } else {
  37. System.out.println("getSmsQueueMessage:null");
  38. }
  39. }
  40. } catch (Exception e) {
  41. System.out.println("receiverMessage error:"+e.getMessage());
  42. try {
  43. Thread.sleep(5000);
  44. } catch (InterruptedException e1) {
  45. System.out.println("InterruptedException error:"+e1.getMessage());
  46. }
  47. //SendSms.listeningSms();//这个是自己发送消息的逻辑代码,要在方法里加上try catch,避免因程序逻辑错误导致重连才行
  48. } finally {
  49. System.out.println("finally session close ");
  50. if(session != null){
  51. try {
  52. session.close();
  53. } catch (JMSException ignore) {
  54. System.out.println("session.close error:"+ignore.getMessage());
  55. }
  56. }
  57. System.out.println("finally connection close ");
  58. if (null != connection) {
  59. try {
  60. connection.close();
  61. } catch (JMSException ignore) {
  62. System.out.println("connection.close error:"+ignore.getMessage());
  63. }
  64. }
  65. }
  66. }

activemq.xml 配置内容:

  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8.  
  9. http://www.apache.org/licenses/LICENSE-2.0
  10.  
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. -->
  17. <!-- START SNIPPET: example -->
  18. <beans
  19. xmlns="http://www.springframework.org/schema/beans"
  20. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  22. http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
  23.  
  24. <!-- Allows us to use system properties as variables in this configuration file -->
  25. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  26. <property name="locations">
  27. <value>file:${activemq.conf}/credentials.properties</value>
  28. </property>
  29. </bean>
  30.  
  31. <!-- Allows accessing the server log -->
  32. <bean id="logQuery" class="io.fabric8.insight.log.log4j.Log4jLogQuery"
  33. lazy-init="false" scope="singleton"
  34. init-method="start" destroy-method="stop">
  35. </bean>
  36.  
  37. <!--
  38. The <broker> element is used to configure the ActiveMQ broker.
  39. -->
  40. <broker xmlns="http://activemq.apache.org/schema/core" persistent="true" brokerName="localhost" dataDirectory="${activemq.data}">
  41.  
  42. <destinationPolicy>
  43. <policyMap>
  44. <policyEntries>
  45. <policyEntry topic=">" topicPrefetch="1">
  46. <!-- The constantPendingMessageLimitStrategy is used to prevent
  47. slow topic consumers to block producers and affect other consumers
  48. by limiting the number of messages that are retained
  49. For more information, see:
  50.  
  51. http://activemq.apache.org/slow-consumer-handling.html
  52.  
  53. -->
  54. <pendingMessageLimitStrategy>
  55. <constantPendingMessageLimitStrategy limit="1000"/>
  56. </pendingMessageLimitStrategy>
  57.  
  58. <subscriptionRecoveryPolicy>
  59. <timedSubscriptionRecoveryPolicy recoverDuration="1800000"/>
  60. </subscriptionRecoveryPolicy>
  61.  
  62. </policyEntry>
  63. </policyEntries>
  64. </policyMap>
  65. </destinationPolicy>
  66.  
  67. <!--
  68. The managementContext is used to configure how ActiveMQ is exposed in
  69. JMX. By default, ActiveMQ uses the MBean server that is started by
  70. the JVM. For more information, see:
  71.  
  72. http://activemq.apache.org/jmx.html
  73. -->
  74. <managementContext>
  75. <managementContext createConnector="false"/>
  76. </managementContext>
  77.  
  78. <!--
  79. Configure message persistence for the broker. The default persistence
  80. mechanism is the KahaDB store (identified by the kahaDB tag).
  81. For more information, see:
  82.  
  83. http://activemq.apache.org/persistence.html
  84. -->
  85. <persistenceAdapter>
  86. <kahaDB directory="${activemq.data}/kahadb" journalMaxFileLength="128mb"/>
  87. </persistenceAdapter>
  88.  
  89. <!--
  90. The systemUsage controls the maximum amount of space the broker will
  91. use before disabling caching and/or slowing down producers. For more information, see:
  92. http://activemq.apache.org/producer-flow-control.html
  93. -->
  94. <systemUsage>
  95. <systemUsage>
  96. <memoryUsage>
  97. <memoryUsage percentOfJvmHeap="70" />
  98. </memoryUsage>
  99. <storeUsage>
  100. <storeUsage limit="100 gb"/>
  101. </storeUsage>
  102. <tempUsage>
  103. <tempUsage limit="50 gb"/>
  104. </tempUsage>
  105. </systemUsage>
  106. </systemUsage>
  107.  
  108. <!--
  109. The transport connectors expose ActiveMQ over a given protocol to
  110. clients and other brokers. For more information, see:
  111.  
  112. http://activemq.apache.org/configuring-transports.html
  113. -->
  114. <transportConnectors>
  115. <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
  116. <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
  117. <!-- <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
  118. <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
  119. <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
  120. <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/> -->
  121. </transportConnectors>
  122.  
  123. <!-- destroy the spring context on shutdown to stop jetty -->
  124. <shutdownHooks>
  125. <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
  126. </shutdownHooks>
  127.  
  128. </broker>
  129.  
  130. <!--
  131. Enable web consoles, REST and Ajax APIs and demos
  132. The web consoles requires by default login, you can disable this in the jetty.xml file
  133.  
  134. Take a look at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details
  135. -->
  136. <import resource="jetty.xml"/>
  137.  
  138. </beans>
  139. <!-- END SNIPPET: example -->

activemq消息队列的使用及应用docker部署常见问题及注意事项的更多相关文章

  1. JAVA的设计模式之观察者模式----结合ActiveMQ消息队列说明

    1----------------------观察者模式------------------------------ 观察者模式:定义对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的 ...

  2. ActiveMQ消息队列从入门到实践(4)—使用Spring JMS收发消息

    Java消息服务(Java Message Service ,JMS)是一个Java标准,定义了使用消息代理的通用API .在JMS出现之前,每个消息代理都有私有的API,这就使得不同代理之间的消息代 ...

  3. C#实现ActiveMQ消息队列

    本文使用C#实现ActiveMQ消息队列功能. 一.首先需要导入两个包,分别是:Apache.NMS 和 Apache.NMS.ActiveMQ 二.创建Winform程序实现生产者功能. 三.Pro ...

  4. SpringBoot集成ActiveMq消息队列实现即时和延迟处理

    原文链接:https://blog.csdn.net/My_harbor/article/details/81328727 一.安装ActiveMq 具体安装步骤:自己谷歌去 二.新建springbo ...

  5. ActiveMQ 消息队列服务

      1 ActiveMQ简介 1.1 ActiveMQ是什么 ActiveMQ是一个消息队列应用服务器(推送服务器).支持JMS规范. 1.1.1 JMS概述 全称:Java Message Serv ...

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

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

  7. ActiveMQ消息队列的使用及应用

    这里就不说怎么安装了,直接解压出来就行了. 谢绝转载,作者保留所有权力 目录:  一:JMQ的两种消息模式 1.1:点对点的消息模式 1.2:订阅模式 二:点对点的实现代码 2.1:点对点的发送端 2 ...

  8. Spring整合ActiveMq消息队列

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

  9. JAVAEE——宜立方商城08:Zookeeper+SolrCloud集群搭建、搜索功能切换到集群版、Activemq消息队列搭建与使用

    1. 学习计划 1.solr集群搭建 2.使用solrj管理solr集群 3.把搜索功能切换到集群版 4.添加商品同步索引库. a) Activemq b) 发送消息 c) 接收消息 2. 什么是So ...

随机推荐

  1. python-面向对象-07_继承

    继承 目标 单继承 多继承 面向对象三大特性 封装 根据 职责 将 属性 和 方法 封装 到一个抽象的 类 中 继承 实现代码的重用,相同的代码不需要重复的编写 多态 不同的对象调用相同的方法,产生不 ...

  2. 使用python脚本实现统计日志文件中的ip访问次数

    使用python脚本实现统计日志文件中的ip访问次数,注意此脚本只适用ip在每行开头的日志文件,需要的朋友可以参考下 适用的日志格式: 106.45.185.214 - - [06/Aug/2014: ...

  3. Win版:Adobe 全系列软件模拟授权注册破解工具 AMT Emulator V0.9.2

    http://www.lookae.com/amtemulator-092/ 下载地址 https://files.cnblogs.com/files/simadi/AMT0.9.rar 支持软件:W ...

  4. 【leetcode】部分思路整理

    题目: 求一个树的最小深度. 思路: 思路一:递归     若为空树返回0:     若左子树为空,则返回右子树的最小深度+1:(加1是因为要加上根这一层,下同)     若右子树为空,则返回左子树的 ...

  5. NYOJ 方案数量

    1.递归求解(直接递归会超时,要用备忘录法) # include<iostream> # include<stdio.h> #include <map> using ...

  6. char varchar

    对于字符类型的有:char:固定长度,存储ANSI字符,不足的补英文半角空格.nchar:固定长度,存储Unicode字符,不足的补英文半角空格varchar:可变长度,存储ANSI字符,根据数据长度 ...

  7. [LeetCode] 339. Nested List Weight Sum_Easy tag:DFS

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  8. [Git/GitHub] Tutorial 1. Git download and commit first project

    1. Install at https://git-scm.com/downloads 2. Set up your name and email $ git config --global user ...

  9. python-列表解析、字典解析、集合解析

    列表解析.字典解析.集合解析 列表解析 生成一个列表 nums = [1, 3, 9] list_gen = [num**2 for num in nums if x <= 5] # [1, 9 ...

  10. android SDK打包app

    SDK  软件开发工具包(Software Development Kit) JDK  开发工具包(Java Developer's Kit) 1.搜索java jdk 进入官网 http://www ...