Skip to content

Commit

Permalink
Added listener for message
Browse files Browse the repository at this point in the history
  • Loading branch information
a-dubaj committed Mar 30, 2022
1 parent ec1ada0 commit 4835d0d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 43 deletions.
37 changes: 37 additions & 0 deletions src/main/java/com/andrzejdubaj/rabbitmqdemoapp/RabbitMQConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.andrzejdubaj.rabbitmqdemoapp;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

private static final String MY_QUEUE = "MyQueue";

@Bean
Queue myQueue() {
return new Queue(MY_QUEUE, true);
}

@Bean
ConnectionFactory connectionFactory() {
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory("localhost");
cachingConnectionFactory.setUsername("guest");
cachingConnectionFactory.setPassword("quest");
return cachingConnectionFactory;
}

@Bean
MessageListenerContainer messageListenerContainer() {
SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
simpleMessageListenerContainer.setConnectionFactory(connectionFactory());
simpleMessageListenerContainer.setQueues(myQueue());
simpleMessageListenerContainer.setMessageListener(new RabbitMQMessageListener());
return simpleMessageListenerContainer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.andrzejdubaj.rabbitmqdemoapp;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

public class RabbitMQMessageListener implements MessageListener {

@Override
public void onMessage(Message message){
System.out.println("message = [" + new String(message.getBody()) + "]");
}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
package com.andrzejdubaj.rabbitmqdemoapp;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RabbitMqDemoAppApplication implements CommandLineRunner {

@Autowired
private RabbitTemplate rabbitTemplate;
public class RabbitMqDemoAppApplication {

public static void main(String[] args) {
SpringApplication.run(RabbitMqDemoAppApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
SimpleMessage simpleMessage = new SimpleMessage();
simpleMessage.setName("FirstMessage");
simpleMessage.setDescription("simpleDescription");
rabbitTemplate.convertAndSend("TestExchange", "testRouting", simpleMessage);
}
}
28 changes: 0 additions & 28 deletions src/main/java/com/andrzejdubaj/rabbitmqdemoapp/SimpleMessage.java

This file was deleted.

0 comments on commit 4835d0d

Please sign in to comment.