Home > front end >  RabbitMQ connection factory not auto injecting or converting message to JSON
RabbitMQ connection factory not auto injecting or converting message to JSON

Time:09-18

Im setting up the connection factory for my rabbitmq application. Ive got the beans created in the config, however for some reason the ConnectionFactory that ive passed into the RabbitTemplate doesnt seem to be creating the bean automatically as it should.

the error its saying is Could not autowire. No beans of 'ConnectionFactory' type found. However this bean should be created automatically. It works if i create a bean of ConnectionFactory manually, but struggling to understand why it doesnt work without it.

package com.example.redistorabbit;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class RedisToRabbitApplication {

public static void main(String[] args) {
    SpringApplication.run(RedisToRabbitApplication.class, args);
}

public static final String TOPIC_EXCHANGE_NAME = "covid-exchange";
public static final String QUEUE_NAME = "cases-entity";

@Bean
Queue queue(){
    return new Queue(QUEUE_NAME,false);
}

@Bean
TopicExchange exchange(){
    return new TopicExchange(TOPIC_EXCHANGE_NAME);
}

@Bean
Binding binding(Queue queue, TopicExchange topicExchange){
    return BindingBuilder.bind(queue).to(topicExchange).with("#");
}

@Bean
public RabbitTemplate publishingRabbitTemplate(final ConnectionFactory connectionFactory) {
    final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
    rabbitTemplate.setMessageConverter(jsonConverter());
    rabbitTemplate.setChannelTransacted(true);
    return rabbitTemplate;
}

@Bean
public Jackson2JsonMessageConverter jsonConverter() {
    return new Jackson2JsonMessageConverter();
}

}

another issue i seem to have is even though i have the Jackson2JsonMessageConverter when the lisetener received and prints the message it is showing as bytes instead of a json message:

@Component
@RequiredArgsConstructor
public class Receiver {

private final CasesRepository casesRepository;

private static final Logger log = LoggerFactory.getLogger(Receiver.class);

@RabbitListener(queues = RedisToRabbitApplication.QUEUE_NAME)
public void consumeMessage(final Message message) {
    log.info("received: "   message.toString());
}
}

received: (Body:'[B@1b68f46c(byte[91])'

CodePudding user response:

Have you tried Injecting the Spring Object Mapper into the Jackson Message converter?

@Bean
public Jackson2JsonMessageConverter jsonConverter(ObjectMapper objectMapper) {
    return new Jackson2JsonMessageConverter(objectMapper);
}

CodePudding user response:

It seems the issue is with the version of spring-boot-autoconfiguration dependency, i could see the bean in the version for 2.7.3, in the JacksonAutoConfiguration class, however Intellij was still saying no bean was found. I added the following dependency:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>1.5.15.RELEASE</version>
    </dependency>

and both ConnectionFactory and ObjectMapper beans have been created automatically

  • Related