3

I have spring boot project(2.1.0 Release) with Spring Cloud sleuth. Spring Sleuth's TracingConnectionFactoryBeanPostProcessor is overiding ConnectionFactory bean with LazyConnectionFactory Bean.As a result,I am hitting:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userCredentialsConnectionFactoryAdapter' is expected to be of type 'org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter' but was actually of type 'org.springframework.cloud.sleuth.instrument.messaging.LazyConnectionFactory'

Could anyone suggest a solution/workaround for this.

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sleuth.test</groupId>
    <artifactId>Jms-Test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Jms-Test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.M3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

        <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.6</version>
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

.

@Configuration
public class JmsConfig {

    @Bean
    public ActiveMQConnectionFactory connectionFactory(){
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL("tcp://localhost:61616");
        connectionFactory.setPassword("admin");
        connectionFactory.setUserName("admin");
        return connectionFactory;
    }   

    @Bean
    UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(ConnectionFactory conFact) {
        UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
        userCredentialsConnectionFactoryAdapter.setUsername("admin");
        userCredentialsConnectionFactoryAdapter.setPassword("admin");
        userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(conFact);
        return userCredentialsConnectionFactoryAdapter;
    }

    @Bean
    public JmsTemplate jmsTemplate(UserCredentialsConnectionFactoryAdapter conFactory){
        JmsTemplate template = new JmsTemplate();
        template.setConnectionFactory(conFactory);
        return template;
    }

@SpringBootApplication
public class JmsTestApplication {

    @Autowired(required=true)
    JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        SpringApplication.run(JmsTestApplication.class, args);
        new JmsTestApplication().sendMessage();

    }

    private  void sendMessage() {
        // TODO Auto-generated method stub
        jmsTemplate.convertAndSend("QUEUE.USER", "user");

    }
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
swathi manda
  • 101
  • 1
  • 7

1 Answers1

4
>@Bean
>public JmsTemplate jmsTemplate(UserCredentialsConnectionFactoryAdapter conFactory){

Don't inject the concrete type; inject the interface instead

@Bean
public JmsTemplate jmsTemplate(ConnectionFactory conFactory){

and qualify it if necessary with @Qualifier or use the correct bean name as the parameter name.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Thank you @Gary Russell for the idea.This works for the bean creation in the current Code.But later if I have to use UserCredentialsConnectionFactoryAdapter properties like the Host Manager etc .we are hitting issue as those properties are not present in the parent ConnectionFactory. – swathi manda Dec 10 '18 at 12:54
  • The `LazyConnectionFactory` simply wraps your connection factory (in it's `delagate` property), but it doesn't provide access to the delegate. If you need to reference it, you can also wrap the original in an object yourself so you can still access it where needed. – Gary Russell Dec 10 '18 at 14:00
  • Hi @GaryRussell Can you please elaborate the above comment? I am unable to understand. – user3145373 ツ Sep 02 '21 at 05:44