Goutham Palani
Goutham Palani

Reputation: 17

Spring boot websocket is not connecting correctly to rabbitmq host

I have a rabbitmq running in my ec2 instance. I have two springboot app one pushes messages to a queue. I used the connectionFactory for the first one to push into queue and it works fine

connectionFactory.setUri("amqp://guest:guest@ec2host:5672/%2F");

The second one tries to connect to the rabbitmq stomp port, something like this

registry.setApplicationDestinationPrefixes("/app");
    registry.enableStompBrokerRelay("/topic").setRelayHost("ec2host").setRelayPort(61613).setClientLogin("guest")
    .setClientPasscode("guest");

When I run the code, it runs but keeps throwing this error message

Attempting to connect to: [localhost:5672]
Consumer raised exception, processing can restart if the connection factory supports it. 
Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: 
Connection refused (Connection refused)
2020-06-06 22:34:10.030  INFO 19749 --- [tContainer#0-85] 
o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@71f987d1: tags=[[]], 
channel=null, acknowledgeMode=AUTO local queue size=0
2020-06-06 22:34:10.030  INFO 19749 --- [tContainer#0-86] o.s.a.r.c.CachingConnectionFactory

I don't know why it is trying to connect localhost:5672 though I save set host to ec2.

Class that listens to the Queue

public class MessageRedirect {
@Autowired
private SimpMessageSendingOperations messagingTemplate;

@RabbitListener(queues = "upload-queue")
@SendTo("/{institution}/notification")
public String sendNotification(String notification) throws JsonMappingException, JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    Notification not = objectMapper.readValue(notification, Notification.class);
    //messagingTemplate.convertAndSend("/topic/"+not.getInstitution(), not.getMessage());
    messagingTemplate.convertAndSend("/topic/"+not.getInstitution() + ".student", not.getMessage());
    System.out.println("SENDING MESSAGE TO  upload-queue" + "/"+ not.getInstitution()+"/notification");
    return "NOTIFICATION SENT";
}

}

Upvotes: 0

Views: 400

Answers (1)

Goutham Palani
Goutham Palani

Reputation: 17

The @RabbitListener(queues = "upload-queue") by default tries to connect to localhost. I just configured with my ec2host and it worked

@Bean
public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties config) throws Exception{
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.getRabbitConnectionFactory().setUri("amqp://guest:guest@ec2host/%2F");
    return  connectionFactory;

Upvotes: 1

Related Questions