Unable to get rabbitmq sender to print out message when using RabbitMQ tut1 tutorial

35 Views Asked by At

I have a link to my github repo for this project here: https://github.com/luiz-miotto/rabbitmqTut1v4

I've been attempting to learn how to use RabbitMQ with Spring AMQP using the tutorial provided by RabbitMQ here: https://www.rabbitmq.com/tutorials/tutorial-one-spring-amqp.html

The issue I'm running into is that I get no print out and no communication from my sender to my receiver class when running the project.

To run this I run the two jar commands in terminal: java -Dserver.port=8889 -jar target/rabbitmq4-0.0.1-SNAPSHOT.jar --spring.profiles.active=hello-world,sender

and

java -Dserver.port=8888 -jar target/rabbitmq4-0.0.1-SNAPSHOT.jar --spring.profiles.active=hello-world,receiver

Running the jars tells me that they are Ready and runnig and that the The following 2 profiles are active: "hello-world", "receiver"

However, I get print out to my terminal nor do I see anything in my RabbitMQ UI (I have RabbitMQ running, in case anyone asks)

I'm suspecting that the issue is related to @Profile somehow since I can't get a print out for the receiver or sender classess but I'm not sure.

I tried adding logging to the sender method in my Tut1Config class to see if that was even being called and I have not seen any of that get logged out.

package com.example.rabbitmq4.tut1;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.EnableScheduling;


@Profile({"tut1","hello-world"})
@Configuration
public class Tut1Config {

    @Bean
    public Queue hello(){
        return new Queue("hello");
    }

    @Profile("receiver")
    @Bean
    public Tut1Receiver receiver(){
        return new Tut1Receiver();
    }

    @Profile("sender")
    @Bean
    public Tut1Sender sender(){
        System.out.println("this shit working for sender?");
        return new Tut1Sender();
    }
}
package com.example.rabbitmq4;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ConfigurableApplicationContext;

public class RabbitAmqpTutorialsRunner implements CommandLineRunner {

    @Value("${tutorial.client.duration:0}")
    private int duration;

    @Autowired
    private ConfigurableApplicationContext ctx;

    @Override
    public void run(String... arg0) throws Exception {
        System.out.println("Ready ... running for " + duration + "ms");
        Thread.sleep(duration);
        ctx.close();
    }
}

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;



public class Tut1Sender {
    @Autowired
    private RabbitTemplate template;

    @Autowired
    private Queue queue;


    @Scheduled(fixedDelay = 1000, initialDelay = 500)
    public void send() {
        String message = "Hello World!";
        this.template.convertAndSend(queue.getName(), message);
        System.out.println(" [x] Sent '" + message + "'");
    }
}
1

There are 1 best solutions below

0
Mojtaba On

I've run your code, as Reveson said you missed some configuration in your application.yml. do it like that tutorial:

spring:
  profiles:
    active: usage_message

logging:
  level:
    org: ERROR

tutorial:
  client:
    duration: 10000

and you have the wrong package name in your Rabbitmq4Application.java class, change this:

package com.example.rabbitmq4.tut1;

to this :

package com.example.rabbitmq4;

I've run Rabbitmq using docker-compose file like this:

version: "3.2"
services:
rabbitmq:
  image: rabbitmq:3-management-alpine
  container_name: "rabbitmq"
  ports:
    - 5672:5672
    - 15672:15672
  volumes:
    - ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
    - ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
  networks:
    - rabbitmq
  restart: unless-stopped

networks:
  rabbitmq:
    driver: bridge

I've seen messages in the terminal and Rabbitmq managementUI. But notice when a message is read by an application from a queue it will be deleted from that queue. Because you got print in the terminal you have the queue but if you want to see messages in Rabbitmq managementUI you can run only the sender.