如何创建缓存连接工厂的 bean 以发布到 tibco ems 队列

问题描述

我正在尝试创建一个用于发布到 tibco ems queue 的 cachingconnection 工厂 bean。 下面是我编写的用于创建 bean 的代码库。

    import javax.jms.JMSException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jms.connection.CachingConnectionFactory;
    import com.tibco.tibjms.TibjmsQueueConnectionFactory;
    
    @Configuration
    public class BeanConfiguration {
    
        @Autowired
        ConfigurationProperty config;
    
        @Bean
        public TibjmsQueueConnectionFactory tibcoConnection() {
            TibjmsQueueConnectionFactory tibco = new TibjmsQueueConnectionFactory();
            try {
                tibco.setServerUrl(config.getJmsUrl());
                tibco.setUserName(config.getTibcoUsername());
                tibco.setUserPassword(config.getTibcopaswd());
                
            } catch (JMSException e) {
                e.printstacktrace();
            }
            return tibco;
        }
        @Bean
         public CachingConnectionFactory connectionFactory()
         {
            CachingConnectionFactory cachingConnection= new CachingConnectionFactory(tibcoConnection());
            cachingConnection.setSessionCacheSize(10);
            return cachingConnection;
         }
    
    }

下面提到了错误

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.jms.connection.CachingConnectionFactory]: Factory method 'connectionFactory' threw exception; nested exception is java.lang.IllegalStateException: @Bean method BeanConfiguration.tibcoConnection called as bean reference for type [com.tibco.tibjms.TibjmsQueueConnectionFactory] but overridden by non-compatible bean instance of type [org.springframework.cloud.sleuth.instrument.messaging.LazyConnectionFactory]. Overriding bean of same name declared in: class path resource [config/BeanConfiguration.class]

解决方法

我们需要为 tibjmsQueueConnectionfactory 创建方法,并且缓存connectionfactory的返回类型应该更新为connectionfactory。

@配置 公共类 BeanConfiguration {

@Autowired
ConfigurationProperty config;


private TibjmsQueueConnectionFactory tibcoConnection() {
    TibjmsQueueConnectionFactory tibco = new TibjmsQueueConnectionFactory();
    try {
        tibco.setServerUrl(config.getJmsUrl());
        tibco.setUserName(config.getTibcoUsername());
        tibco.setUserPassword(config.getTibcoPaswd());
        
    } catch (JMSException e) {
        e.printStackTrace();
    }
    return tibco;
}
@Bean
 public ConnectionFactory connectionFactory()
 {
    CachingConnectionFactory cachingConnection= new CachingConnectionFactory(tibcoConnection());
    cachingConnection.setSessionCacheSize(10);
    return cachingConnection;
 }

}