• activeMq将mqtt发布订阅转成消息队列


    1、activemq.xml置文件新增如下内容

    2、mqttx测试发送:

    主题(配置的模糊匹配,为了并发):VirtualTopic/device/sendData/12312

    3、mqtt接收的结果

    4、程序处理

    1. package com
    2. import cn.hutool.core.date.DateUtil;
    3. import cn.hutool.core.util.NumberUtil;
    4. import cn.hutool.core.util.StrUtil;
    5. import cn.hutool.json.JSONUtil;
    6. import org.apache.activemq.ActiveMQMessageAudit;
    7. import org.apache.activemq.command.ActiveMQBytesMessage;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.jms.annotation.JmsListener;
    10. import org.springframework.stereotype.Component;
    11. import javax.jms.Message;
    12. import java.nio.charset.Charset;
    13. import java.util.Date;
    14. @Component
    15. public class MessageHandler {
    16. @JmsListener(destination = "deviceQueue.receiveDate", containerFactory = "queueListener", concurrency = "1-3")
    17. public void deviceMessage(ActiveMQBytesMessage message) {
    18. System.out.println(StrUtil.str(message.getContent().getData(), Charset.defaultCharset()));
    19. System.out.println("###################" + message + "###################");
    20. }
    21. }

     控制台打印

     全量的: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. http://www.apache.org/licenses/LICENSE-2.0
    9. Unless required by applicable law or agreed to in writing, software
    10. distributed under the License is distributed on an "AS IS" BASIS,
    11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. See the License for the specific language governing permissions and
    13. limitations under the License.
    14. -->
    15. <!-- START SNIPPET: example -->
    16. <beans
    17. xmlns="http://www.springframework.org/schema/beans"
    18. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    19. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    20. http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
    21. <!-- Allows us to use system properties as variables in this configuration file -->
    22. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    23. <property name="locations">
    24. <value>file:${activemq.conf}/credentials.properties</value>
    25. </property>
    26. </bean>
    27. <!--
    28. The <broker> element is used to configure the ActiveMQ broker.
    29. -->
    30. <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}">
    31. <destinationPolicy>
    32. <policyMap>
    33. <policyEntries>
    34. <policyEntry topic=">" >
    35. <!-- The constantPendingMessageLimitStrategy is used to prevent
    36. slow topic consumers to block producers and affect other consumers
    37. by limiting the number of messages that are retained
    38. For more information, see:
    39. http://activemq.apache.org/slow-consumer-handling.html
    40. -->
    41. <pendingMessageLimitStrategy>
    42. <constantPendingMessageLimitStrategy limit="1000"/>
    43. </pendingMessageLimitStrategy>
    44. </policyEntry>
    45. </policyEntries>
    46. </policyMap>
    47. </destinationPolicy>
    48. <!--
    49. The managementContext is used to configure how ActiveMQ is exposed in
    50. JMX. By default, ActiveMQ uses the MBean server that is started by
    51. the JVM. For more information, see:
    52. http://activemq.apache.org/jmx.html
    53. -->
    54. <managementContext>
    55. <managementContext createConnector="false"/>
    56. </managementContext>
    57. <!--
    58. Configure message persistence for the broker. The default persistence
    59. mechanism is the KahaDB store (identified by the kahaDB tag).
    60. For more information, see:
    61. http://activemq.apache.org/persistence.html
    62. -->
    63. <persistenceAdapter>
    64. <kahaDB directory="${activemq.data}/kahadb"/>
    65. </persistenceAdapter>
    66. <!--
    67. The systemUsage controls the maximum amount of space the broker will
    68. use before disabling caching and/or slowing down producers. For more information, see:
    69. http://activemq.apache.org/producer-flow-control.html
    70. -->
    71. <systemUsage>
    72. <systemUsage>
    73. <memoryUsage>
    74. <memoryUsage percentOfJvmHeap="70" />
    75. </memoryUsage>
    76. <storeUsage>
    77. <storeUsage limit="100 gb"/>
    78. </storeUsage>
    79. <tempUsage>
    80. <tempUsage limit="50 gb"/>
    81. </tempUsage>
    82. </systemUsage>
    83. </systemUsage>
    84. <!--
    85. The transport connectors expose ActiveMQ over a given protocol to
    86. clients and other brokers. For more information, see:
    87. http://activemq.apache.org/configuring-transports.html
    88. -->
    89. <transportConnectors>
    90. <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
    91. <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
    92. <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
    93. <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
    94. <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
    95. <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
    96. </transportConnectors>
    97. <!-- destroy the spring context on shutdown to stop jetty -->
    98. <shutdownHooks>
    99. <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
    100. </shutdownHooks>
    101. <!-- 消息订阅 -->
    102. <destinationInterceptors>
    103. <virtualDestinationInterceptor>
    104. <virtualDestinations>
    105. <compositeTopic name="VirtualTopic.device.sendData.*">
    106. <forwardTo>
    107. <queue physicalName="deviceQueue.receiveDate" />
    108. </forwardTo>
    109. </compositeTopic>
    110. </virtualDestinations>
    111. </virtualDestinationInterceptor>
    112. </destinationInterceptors>
    113. <plugins>
    114. <simpleAuthenticationPlugin>
    115. <users>
    116. <authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users,admins"/>
    117. <!--<authenticationUser username="user" password="password" groups="users"/>
    118. <authenticationUser username="guest" password="password" groups="guests"/> -->
    119. </users>
    120. </simpleAuthenticationPlugin>
    121. </plugins>
    122. </broker>
    123. <!--
    124. Enable web consoles, REST and Ajax APIs and demos
    125. The web consoles requires by default login, you can disable this in the jetty.xml file
    126. Take a look at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details
    127. -->
    128. <import resource="jetty.xml"/>
    129. </beans>
    130. <!-- END SNIPPET: example -->

  • 相关阅读:
    工业数据采集方案:有何作用和特点?
    定量数据和定性数据
    Pytorch实战教程(二十七)-基于ResNet模型实现猫狗分类
    大模型时代,AI如何成为数实融合的驱动力?
    漏刻有时物联网环境态势感知大数据(设备列表、动态折线图)
    Pyecharts绘图教程(2)—— 绘制多种折线图(Line)参数说明+代码实战
    Stable Diffusion插件:StyleSelectorXL 之七十七种绘画风格任君选择
    ACM-BCB2019 | SMILES-BERT:基于大规模无监督预训练的分子属性预测模型
    Java实现二叉树两个节点最近公共祖先
    深入理解Java并发锁
  • 原文地址:https://blog.csdn.net/qq_26408545/article/details/136183186