本篇内容主要讲解“SpringBoot中activeMq的用法实例介绍”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot中activeMq的用法实例介绍”吧!
<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>
spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin spring.activemq.in-memory=true spring.activemq.pool.enabled=false
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; @SpringBootApplication public class ApproveApplication { public static void main(String[] args) { SpringApplication.run(ApproveApplication.class, args); } @Bean public ConnectionFactory connectionFactory(){ System.out.println("aaaaaaaaaaaaaaaaaaaaaa"); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); connectionFactory.setBrokerURL("tcp://localhost:61616"); connectionFactory.setUserName("admin"); connectionFactory.setPassword("admin"); return connectionFactory; } @Bean public JmsTemplate genJmsTemplate(){ System.out.println("aaaaaaaaaaaaaaaaaaaaaabbbbbbbbb"); return new JmsTemplate(connectionFactory()); } @Bean public JmsMessagingTemplate jmsMessageTemplate(){ System.out.println("ccccccccccccc"); return new JmsMessagingTemplate(connectionFactory()); } }
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)V at 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 more Caused 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)V at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582) ... 47 more Caused by: java.lang.NoSuchMethodError: org.springframework.jms.core.JmsTemplate.setDeliveryDelay(J)V at 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
错误现象
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的用法实例介绍”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。