Why I am getting Channel error when I am trying to hit the URL in RabbitMQ?

Viewed 47

I am new to RabbitMQ and I am facing a channel error. My program is quite simple and straightforward:

Configuration File:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
javainuse.rabbitmq.exchange=javainuse.exchange
javainuse.rabbitmq.queue=javainuse.queue
javainuse.rabbitmq.routingkey=javainuse.routingkey
server.port=8081

Below Class is RabbitMQConfig.java. This class contains following beans: Queue, DirectExchange, Binding and MessageConverter.

@Configuration
public class RabbitMQConfig {
    
    @Bean("myTemplate")
    public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        return rabbitTemplate; 
    }
}

Below Class is RabbitMQSender.java. It invokes myTemplate to send the message to the queue.

@Service
public class RabbitMQSender {
        
    public void send(Person person) {
        rabbitTemplate.convertAndSend(exchange, routingkey, person);
        System.out.println("Send msg = " + person);
    }
}

Below is controller, i.e., RabbitController.java. This is the only controller I have in my project.

public class RabbitController {
        
    @GetMapping(value = "/producer")
    public String producer(@RequestParam("empName") String empName, @RequestParam("empId") String empId) {
        
        person.setName(empName);
        person.setId(empId);
        rabbitMQSender.send(person);
        
        return "Message sent to the RabbitMQ JavaInUse Successfully";
    }
}

I have one Pojo, i.e., Person that has only two variables id and name. However, the queue is created while I try to hit the below URL:

http://localhost:8081/javainuse-rabbitmq/producer?empName=Ria&empId=emp003

log output

I believe this is a silly mistake. Please, help me in fixing the same.

1 Answers

I believe you have defined your Exchange bean like this:

@Bean
public DirectExchange exchange(@Value("{javainuse.rabbitmq.exchange}") String exchangeName) {
    // ...
}

You missed the $ sign in @Value. Try defining the bean like this:

@Bean
public DirectExchange exchange(@Value("${javainuse.rabbitmq.exchange}") String exchangeName) {
    // ...
}
Related