服务器之家

服务器之家 > 正文

基于SpringBoot中activeMq的JmsTemplate的实例

时间:2021-10-29 11:20     来源/作者:xiaoluo5238

SpringBoot 中使用activeMq的步骤:

1、pom中引入jar包:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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

?
1
2
3
4
5
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

3、Application 中添加对应的bean生成规则

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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());
    }
 
}

4、发送消息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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初始化错误,异常如下,只要按照步骤三就可以了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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

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

?
1
spring.activemq.pool.enabled=false

启动springboot是不报错了,但是要发的消息也没进队列。至于为什么就不报错了,还没搞明白。如有高人请指点一二。

解决方案

查看ActiveMQConnectionFactoryConfiguration类,自动配置发现需要引入下面这个依赖:

?
1
2
3
4
5
<dependency>
       <groupId>org.messaginghub</groupId>
       <artifactId>pooled-jms</artifactId>
       <version>1.0.3</version>
</dependency>

将依赖引入pom,再启动springboot,搞定!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/xiaoluo5238/article/details/81202642

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部