Last active
February 8, 2021 20:28
-
-
Save Kaspic/b6c91a71a1fcbaef24134bc71e1d3146 to your computer and use it in GitHub Desktop.
https://kennay-kermani.medium.com/simple-pub-sub-implementation-with-spring-boot-docker-and-rabbitmq-4ed7461de239 - Simple Pub-Sub implementation with Spring Boot, Docker, and RabbitMQ tutorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.rabbitmq.listener.simple.acknowledge-mode=manual |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.consumer.event; | |
import com.example.publisher.config.RabbitMQConfig; | |
import com.rabbitmq.client.Channel; | |
import org.springframework.amqp.rabbit.annotation.RabbitListener; | |
import org.springframework.amqp.support.AmqpHeaders; | |
import org.springframework.messaging.handler.annotation.Header; | |
import org.springframework.stereotype.Component; | |
import java.io.IOException; | |
@Component | |
public class DemoEventConsumer { | |
@RabbitListener(queues = RabbitMQConfig.QK_EXAMPLE_QUEUE) | |
public void onMessageReceived(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException { | |
System.out.println("Message received!: " + message); | |
try{ | |
channel.basicAck(tag, false); | |
}catch (Exception e) { | |
channel.basicNack(tag, false, true); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.publisher.event; | |
import com.example.publisher.config.RabbitMQConfig; | |
import org.springframework.amqp.rabbit.core.RabbitTemplate; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import java.text.SimpleDateFormat; | |
@Controller | |
public class DemoEventController { | |
private final RabbitTemplate rabbitTemplate; | |
public DemoEventController(RabbitTemplate rabbitTemplate) { | |
this.rabbitTemplate = rabbitTemplate; | |
} | |
@PostMapping("/event") | |
ResponseEntity<Void> postEventMessage() { | |
final String timeNowMessage = String.format("%s - %s", "ExampleMessage", getTimeNowRepresentation()); | |
rabbitTemplate.convertAndSend(RabbitMQConfig.QK_EXAMPLE_QUEUE, timeNowMessage); | |
return new ResponseEntity<>(HttpStatus.OK); | |
} | |
private String getTimeNowRepresentation() { | |
long now = System.currentTimeMillis(); | |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss"); | |
return simpleDateFormat.format(now); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker run --rm -it --hostname demo-tutorial-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.publisher.config; | |
import org.springframework.amqp.core.Queue; | |
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | |
import org.springframework.amqp.rabbit.core.RabbitTemplate; | |
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; | |
import org.springframework.amqp.support.converter.MessageConverter; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
public class RabbitMQConfig { | |
public static final String QK_EXAMPLE_QUEUE = "exampleQueue"; | |
@Bean | |
public RabbitTemplate jsonRabbitTemplate(ConnectionFactory connectionFactory) { | |
RabbitTemplate template = new RabbitTemplate(connectionFactory); | |
template.setMessageConverter(jsonConverter()); | |
return template; | |
} | |
@Bean | |
public MessageConverter jsonConverter() { | |
return new Jackson2JsonMessageConverter(); | |
} | |
@Bean | |
public Queue exampleQueue() { | |
return new Queue(QK_EXAMPLE_QUEUE); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -X POST http://localhost:8080/event |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment