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 + "'");
}
}
I've run your code, as Reveson said you missed some configuration in your
application.yml. do it like that tutorial:and you have the wrong package name in your
Rabbitmq4Application.javaclass, change this:to this :
I've run Rabbitmq using docker-compose file like this:
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.