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])'