{"id":1489,"date":"2023-03-24T22:47:34","date_gmt":"2023-03-24T14:47:34","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1489"},"modified":"2023-04-28T20:30:22","modified_gmt":"2023-04-28T12:30:22","slug":"spring-boot-integration-with-activemq","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/24\/spring-boot-integration-with-activemq\/","title":{"rendered":"Spring Boot\u96c6\u6210ActiveMQ"},"content":{"rendered":"<h2>in-memory ActiveMQ<\/h2>\n<h3>\u6dfb\u52a0\u4f9d\u8d56<\/h3>\n<pre><code class=\"language-xml\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.springframework.boot\/spring-boot-starter-activemq --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-activemq&lt;\/artifactId&gt;\n    &lt;version&gt;2.3.2.RELEASE&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p><!-- more --><\/p>\n<h3>\u4fee\u6539ActiveMQ\u914d\u7f6e<\/h3>\n<p>application.properties\u914d\u7f6e\uff1a<\/p>\n<pre><code>spring.activemq.in-memory=true\nspring.activemq.pool.enabled=false<\/code><\/pre>\n<p>\u5907\u6ce8\uff1a\u5b9e\u9645\u4e0a\u8fd9\u4e24\u4e2a\u662f\u9ed8\u8ba4\u503c\uff0c\u4e0d\u914d\u7f6e\u60c5\u51b5\u4e0b\u5373\u662f\u5982\u6b64<\/p>\n<h3>\u6dfb\u52a0Producer\u7c7b<\/h3>\n<pre><code class=\"language-java\">@RestController\npublic class Producer {\n    @Autowired\n    private JmsMessagingTemplate jmsMessagingTemplate;\n\n    @Autowired\n    private Queue queue;\n\n    @RequestMapping(&quot;\/sendMsg&quot;)\n    public void send(String msg) {\n        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);\n    }\n}<\/code><\/pre>\n<p>\u4f7f\u7528rest\u670d\u52a1\u65b9\u5f0f\u6765\u89e6\u53d1\u53d1\u9001\u6d88\u606f\u7684\u65b9\u6cd5<\/p>\n<h3>\u6dfb\u52a0Consumer\u7c7b<\/h3>\n<pre><code class=\"language-java\">@Component\npublic class Consumer {\n\n    @JmsListener(destination = &quot;sample.queue&quot;)\n    public void receiveQueue(String text) {\n        System.out.println(text);\n    }\n}<\/code><\/pre>\n<p><code>JmsListener<\/code>\u662f<code>spring-jms<\/code>\u63d0\u4f9b\u7684\u4e00\u4e2a\u6ce8\u89e3\uff0c\u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2aJms\u7684\u6d88\u606f\u76d1\u542c\u5b9e\u4f8b\uff0c\u4e5f\u5c31\u662f\u4e00\u4e2a\u5f02\u6b65\u7684\u6d88\u8d39\u8005<\/p>\n<h3>\u6dfb\u52a0JMS\u7684\u6ce8\u89e3\u626b\u63cf<\/h3>\n<pre><code class=\"language-java\">@SpringBootApplication\n@EnableJms\npublic class ActivemqDemoApplication {\n    @Bean\n    public Queue queue() {\n        return new ActiveMQQueue(&quot;sample.queue&quot;);\n    }\n\n    public static void main(String[] args) {\n        SpringApplication.run(ActivemqDemoApplication.class, args);\n    }\n}<\/code><\/pre>\n<p><code>@EnableJms<\/code>\u4f1a\u542f\u52a8jms\u7684\u6ce8\u89e3\u626b\u63cf\uff0c\u76f8\u5f53\u4e8e<code>&lt;jms:annotation-d riven\/&gt;<\/code><\/p>\n<h3>\u542f\u52a8\u53ca\u6d4b\u8bd5<\/h3>\n<p>\u5728\u6d4f\u89c8\u5668\u8f93\u5165\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:8080\/sendMsg?msg=HelloActiveMQ\">http:\/\/localhost:8080\/sendMsg?msg=HelloActiveMQ<\/a><\/p>\n<h2>\u8fde\u63a5\u5916\u90e8\u7684ActiveMQ<\/h2>\n<h3>\u4fee\u6539\u914d\u7f6e<\/h3>\n<p>\u5b98\u65b9\u7684\u5b9e\u4f8b\u7528\u7684\u662f<code>in-memory<\/code>\u7684ActiveMQ\u3002\u7136\u800c\u6211\u4eec\u5b9e\u9645\u4f7f\u7528\u7684\u65f6\u5019\u90fd\u662f\u8fde\u63a5\u5916\u90e8\u5171\u7528\u7684ActiveMQ\u670d\u52a1\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u4fee\u6539\u4e00\u4e0b\u914d\u7f6e\uff0c\u6765\u4f7f\u7528\u5916\u90e8\u7684ActiveMQ\u670d\u52a1\u3002<\/p>\n<pre><code>spring.activemq.broker-url=tcp:\/\/localhost:61616\n#spring.activemq.broker-url=failover:(tcp:\/\/localhost:61616,tcp:\/\/localhost:61617)\nspring.activemq.close-timeout=5000\nspring.activemq.in-memory=false\nspring.activemq.pool.enabled=true\nspring.activemq.pool.max-connections=100\nspring.activemq.send-timeout=3000<\/code><\/pre>\n<p>\u8fd9\u91cc\u88ab\u6ce8\u91ca\u6389\u7684<code>spring.activemq.broker-url<\/code>\u662f\u5bf9\u5e94ActiveMQ\u96c6\u7fa4\u65f6\u5019\u7684<code>broker-url<\/code>\u914d\u7f6e\u3002<\/p>\n<p>\u5982\u679c\u672a\u627e\u5230<code>activemq-pool<\/code>\u4f9d\u8d56\u5305\uff0c\u5219\u81ea\u884c\u6dfb\u52a0\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.apache.activemq\/activemq-pool --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.activemq&lt;\/groupId&gt;\n    &lt;artifactId&gt;activemq-pool&lt;\/artifactId&gt;\n    &lt;version&gt;5.16.0&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h3>\u542f\u52a8\u670d\u52a1<\/h3>\n<p>\u5f53\u6ca1\u6709\u542f\u52a8ActiveMQ\u65f6\uff0cSpring Boot\u542f\u52a8\u5931\u8d25\uff1a<\/p>\n<pre><code>***************************\nAPPLICATION FAILED TO START\n***************************\n\nDescription:\n\nField jmsMessagingTemplate in com.example.demo.activemq.Producer required a bean of type &#039;org.springframework.jms.core.JmsMessagingTemplate&#039; that could not be found.\n    - Bean method &#039;jmsMessagingTemplate&#039; not loaded because Ancestor org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration did not match\n\nAction:\n\nConsider revisiting the conditions above or defining a bean of type &#039;org.springframework.jms.core.JmsMessagingTemplate&#039; in your configuration.<\/code><\/pre>\n<p>\u4f46\u662f\u5931\u8d25\u7684\u6d88\u606f\u63d0\u793a\u5f88\u8be1\u5f02\uff0c\u544a\u8bc9\u7684\u662f\u627e\u4e0d\u5230<code>JmsMessagingTemplate<\/code>\uff0c\u800c\u4e0d\u662f\u63d0\u793a\u8fde\u63a5\u4e0d\u4e0aActiveMQ<\/p>\n<p>\u6309\u7167\u63d0\u793a\u4fe1\u606f\u91cc\u9762\u8bf4\u7684\u8bbe\u7f6e<code>debug=true<\/code>\uff0c\u80fd\u591f\u6253\u5370\u51fa<code>auto-configure<\/code>\u4e2d\u76f8\u5173\u7c7b\u7684\u5339\u914d\u4fe1\u606f\uff0c\u4f46\u662f\u4ecd\u7136\u4e0d\u80fd\u76f4\u63a5\u5b9a\u4f4d\u5230\u662fActiveMQ\u670d\u52a1\u8fde\u63a5\u4e0d\u4e0a\u3002<\/p>\n<p>\u5982\u679c\u9047\u5230\u4e0a\u8ff0\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u68c0\u67e5ActiveMQ\u670d\u52a1\u662f\u5426\u80fd\u591f\u8fde\u63a5\u3002<\/p>\n<h3>\u4e00\u4e2ajmsTemplate\u652f\u6301queue\u548ctopic<\/h3>\n<p>\u4e00\u4e2ajmsTemplate\u652f\u6301<code>queue<\/code>\u548c<code>topic<\/code>\u7684\u6d88\u606f\u53d1\u9001\uff0c\u53ea\u9700\u8981\u6307\u5b9a\u4e0d\u540c\u7684<code>destination<\/code>\u5373\u53ef\u3002<\/p>\n<p>\u4f46\u662f\u6d88\u606f\u7684\u63a5\u6536\uff0c\u4e5f\u5c31\u662f<code>@JmsListener<\/code>\u5982\u679c\u4e0d\u6307\u5b9a\u72ec\u7acb\u7684<code>containerFactory<\/code>\u7684\u8bdd\u662f\u53ea\u80fd\u6d88\u8d39<code>queue<\/code>\u6d88\u606f\u7684\uff0c\u5982\u679c\u8981\u80fd\u591f\u540c\u65f6\u63a5\u6536<code>topic<\/code>\u6d88\u606f\uff0c\u9700\u8981\u7ed9<code>topic<\/code>\u5bf9\u5e94\u7684<code>@JmsListener<\/code>\u589e\u52a0<code>containerFactory<\/code>\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u9700\u8981\u7ed9topic\u5b9a\u4e49\u72ec\u7acb\u7684JmsListenerContainer\n@Bean\npublic JmsListenerContainerFactory&lt;?&gt; jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {\n    DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();\n    bean.setPubSubDomain(true);\n    bean.setConnectionFactory(activeMQConnectionFactory);\n    return bean;\n}\n\n@JmsListener(destination = &quot;sample.topic&quot;, containerFactory=&quot;jmsListenerContainerTopic&quot;)\npublic void receiveTopic(String text) {\n    System.out.println(text);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>in-memory ActiveMQ \u6dfb\u52a0\u4f9d\u8d56 &lt;!&#8211; https:\/\/mvnrepository.c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[365],"class_list":["post-1489","post","type-post","status-publish","format-standard","hentry","category-spring-boot","tag-activemq"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1489"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1489\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}