首页 > 编程语言 > 基于SpringBoot中activeMq的JmsTemplate的实例
2021
12-10

基于SpringBoot中activeMq的JmsTemplate的实例

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:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true
spring.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;
 
@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、发送消息

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

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,搞定!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自学编程网。

编程技巧