SpringBoot中activeMq的用法实例介绍
本篇内容主要讲解“SpringBoot中activeMq的用法实例介绍”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot中activeMq的用法实例介绍”吧!
SpringBoot 中使用activeMq的步骤:
1、pom中引入jar包:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <!-- <version>5.7.0</version> --> </dependency>
2、spring boot配置文件中配置activeMq
spring.activemq.broker-url=tcp://localhost:61616spring.activemq.user=adminspring.activemq.password=adminspring.activemq.in-memory=truespring.activemq.pool.enabled=false
3、Application 中添加对应的bean生成规则
package com.telligen.ascertain; import org.apache.activemq.ActiveMQConnectionFactory;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.jms.core.JmsTemplate; import javax.jms.ConnectionFactory; @SpringBootApplicationpublic class ApproveApplication { public static void main(String[] args) {SpringApplication.run(ApproveApplication.class, args);} @Beanpublic ConnectionFactory connectionFactory(){System.out.println("aaaaaaaaaaaaaaaaaaaaaa");ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();connectionFactory.setBrokerURL("tcp://localhost:61616");connectionFactory.setUserName("admin");connectionFactory.setPassword("admin");return connectionFactory;}@Beanpublic JmsTemplate genJmsTemplate(){System.out.println("aaaaaaaaaaaaaaaaaaaaaabbbbbbbbb");return new JmsTemplate(connectionFactory());}@Beanpublic JmsMessagingTemplate jmsMessageTemplate(){System.out.println("ccccccccccccc");return new JmsMessagingTemplate(connectionFactory());} }
4、发送消息
package com.telligen.ascertain.approve.common.util.network;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.stereotype.Component; @Component@Scope("singleton")public class ActiveMqUtil { private Logger logger = Logger.getLogger(ActiveMqUtil.class); @Autowired private JmsMessagingTemplate jmsMessagingTemplate; public void sendMsg(String destinationName ,String message){ logger.info("发送 消息到消息队列"); jmsMessagingTemplate.convertAndSend(destinationName,message); }}
遇到的问题:
jmsMessagingTemplate 注入不成功,spring初始化错误,异常如下,只要按照步骤三就可以了:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmsTemplate' defined in class path resource [org/springframework/boot/autoconfigure/jms/JmsAutoConfiguration$JmsTemplateConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.jms.core.JmsTemplate]: Factory method 'jmsTemplate' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.jms.core.JmsTemplate.setDeliveryDelay(J)Vat org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:590)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1256)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1105)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503)at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$112/1129944640.getObject(Unknown Source)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395)at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:139)at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)... 29 moreCaused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.jms.core.JmsTemplate]: Factory method 'jmsTemplate' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.jms.core.JmsTemplate.setDeliveryDelay(J)Vat org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582)... 47 moreCaused by: java.lang.NoSuchMethodError: org.springframework.jms.core.JmsTemplate.setDeliveryDelay(J)Vat java.lang.invoke.MethodHandleNatives.resolve(Native Method)at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:965)at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:990)at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1385)at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:1726)at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:442)at org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration$JmsTemplateConfiguration.mapTemplateProperties(JmsAutoConfiguration.java:91)at org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration$JmsTemplateConfiguration.jmsTemplate(JmsAutoConfiguration.java:83)at org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration$JmsTemplateConfiguration$$EnhancerBySpringCGLIB$$654aefcc.CGLIB$jmsTemplate$0(<generated>)at org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration$JmsTemplateConfiguration$$EnhancerBySpringCGLIB$$654aefcc$$FastClassBySpringCGLIB$$6b82ee57.invoke(<generated>)at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)at org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration$JmsTemplateConfiguration$$EnhancerBySpringCGLIB$$654aefcc.jmsTemplate(<generated>)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:483)at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)... 48 more
Springboot整合activemq时报jmsMessagingTemplate未加载错误
错误现象
Bean method ‘jmsMessagingTemplate' in ‘JmsAutoConfiguration.MessagingTemplateConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration did not match
网上解决方案
网上最多的方案是:application.properties中配置项的行尾有空格。检查没有,排除了这种可能。
第二种解决方案是:按如下方式将配置spring.activemq.pool.enabled改为false
spring.activemq.pool.enabled=false
启动springboot是不报错了,但是要发的消息也没进队列。至于为什么就不报错了,还没搞明白。如有高人请指点一二。
解决方案
查看ActiveMQConnectionFactoryConfiguration类,自动配置发现需要引入下面这个依赖:
<dependency> <groupId>org.messaginghub</groupId> <artifactId>pooled-jms</artifactId> <version>1.0.3</version> </dependency>
将依赖引入pom,再启动springboot,搞定!
到此,相信大家对“SpringBoot中activeMq的用法实例介绍”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341