Skip to content

Commit

Permalink
feat : use postgresql as db, delombok and polish config
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Dec 26, 2024
1 parent 1cac5e3 commit 7277f15
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 63 deletions.
16 changes: 10 additions & 6 deletions boot-rabbitmq-thymeleaf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ Demonstrates a message producer and consumer setup with RabbitMQ, plus a simple

---

Source Code : https://sivalabs.in/2018/02/springboot-messaging-rabbitmq/
[Source Code](https://sivalabs.in/2018/02/springboot-messaging-rabbitmq/)

---

## Important links

* Home Page : http://localhost:8080
* RabbitMq : http://localhost:15672 (guest/guest default)

---

## Installing Rabbit MQ

Expand All @@ -22,8 +31,3 @@ Windows
https://www.rabbitmq.com/which-erlang.html
http://www.erlang.org/downloads
Video - https://www.youtube.com/watch?v=gKzKUmtOwR4

# Important links

Home Page : http://localhost:8080
RabbitMq : http://localhost:15672 (guest/guest default)
20 changes: 19 additions & 1 deletion boot-rabbitmq-thymeleaf/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: "3.2"
services:
rabbitmq:
container_name: rabbitmq
Expand All @@ -20,6 +19,25 @@ services:
retries: 10
networks:
- rabbitmq_go_net

postgresqldb:
image: postgres:17.2-alpine
hostname: postgresqldb
extra_hosts: [ 'host.docker.internal:host-gateway' ]
environment:
- POSTGRES_USER=appuser
- POSTGRES_PASSWORD=secret
- POSTGRES_DB=appdb
healthcheck:
test: ["CMD-SHELL", "pg_isready -U appuser -d appdb"]
interval: 10s
timeout: 5s
retries: 5
ports:
- "5432:5432"
networks:
- rabbitmq_go_net

networks:
rabbitmq_go_net:
driver: bridge
22 changes: 14 additions & 8 deletions boot-rabbitmq-thymeleaf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand All @@ -74,11 +79,6 @@
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
Expand All @@ -90,10 +90,11 @@
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<groupId>org.webjars</groupId>
<artifactId>popper.js</artifactId>
<version>2.11.7</version>
</dependency>

<!-- tag::tests[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -115,6 +116,11 @@
<artifactId>rabbitmq</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>

<!-- tag::tests[] -->
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.poc.boot.rabbitmq.config;

import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;

@EnableRabbit
@Configuration(proxyBeanMethods = false)
public class RabbitListenerConfig implements RabbitListenerConfigurer {

private final MessageHandlerMethodFactory messageHandlerMethodFactory;

public RabbitListenerConfig(MessageHandlerMethodFactory messageHandlerMethodFactory) {
this.messageHandlerMethodFactory = messageHandlerMethodFactory;
}

@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package com.poc.boot.rabbitmq.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -22,10 +18,8 @@
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.support.RetryTemplate;

@EnableRabbit
@Configuration
@Slf4j
public class RabbitMQConfig implements RabbitListenerConfigurer {
@Configuration(proxyBeanMethods = false)
public class RabbitMQConfig {

public static final String DLX_ORDERS_EXCHANGE = "DLX.ORDERS.EXCHANGE";

Expand All @@ -39,7 +33,7 @@ public class RabbitMQConfig implements RabbitListenerConfigurer {

private final RabbitTemplateConfirmCallback rabbitTemplateConfirmCallback;

public RabbitMQConfig(RabbitTemplateConfirmCallback rabbitTemplateConfirmCallback) {
RabbitMQConfig(RabbitTemplateConfirmCallback rabbitTemplateConfirmCallback) {
this.rabbitTemplateConfirmCallback = rabbitTemplateConfirmCallback;
}

Expand All @@ -57,10 +51,8 @@ DirectExchange ordersExchange() {

/* Binding between Exchange and Queue using routing key */
@Bean
Binding bindingMessages() {
return BindingBuilder.bind(ordersQueue())
.to(ordersExchange())
.with(ROUTING_KEY_ORDERS_QUEUE);
Binding bindingMessages(DirectExchange ordersExchange, Queue ordersQueue) {
return BindingBuilder.bind(ordersQueue).to(ordersExchange).with(ROUTING_KEY_ORDERS_QUEUE);
}

@Bean
Expand All @@ -83,8 +75,8 @@ Queue deadLetterQueue() {

/* Binding between Exchange and Queue for Dead Letter */
@Bean
Binding deadLetterBinding() {
return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange());
Binding deadLetterBinding(Queue deadLetterQueue, FanoutExchange deadLetterExchange) {
return BindingBuilder.bind(deadLetterQueue).to(deadLetterExchange);
}

/* Bean for rabbitTemplate */
Expand Down Expand Up @@ -116,15 +108,11 @@ MappingJackson2MessageConverter consumerJackson2MessageConverter() {
}

@Bean
MessageHandlerMethodFactory messageHandlerMethodFactory() {
MessageHandlerMethodFactory messageHandlerMethodFactory(
MappingJackson2MessageConverter consumerJackson2MessageConverter) {
DefaultMessageHandlerMethodFactory messageHandlerMethodFactory =
new DefaultMessageHandlerMethodFactory();
messageHandlerMethodFactory.setMessageConverter(consumerJackson2MessageConverter());
messageHandlerMethodFactory.setMessageConverter(consumerJackson2MessageConverter);
return messageHandlerMethodFactory;
}

@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

import com.poc.boot.rabbitmq.entities.TrackingState;
import com.poc.boot.rabbitmq.repository.TrackingStateRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

@Slf4j
@Component
@RequiredArgsConstructor
public class RabbitTemplateConfirmCallback implements RabbitTemplate.ConfirmCallback {

private static final Logger log = LoggerFactory.getLogger(RabbitTemplateConfirmCallback.class);
private final TrackingStateRepository trackingStateRepository;

public RabbitTemplateConfirmCallback(TrackingStateRepository trackingStateRepository) {
this.trackingStateRepository = trackingStateRepository;
}

@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
Assert.notNull(correlationData, () -> "correlationData can't be null");
Expand All @@ -27,6 +30,8 @@ public void confirm(CorrelationData correlationData, boolean ack, String cause)
log.debug(
"persisted correlationId in db : {}",
this.trackingStateRepository.save(
new TrackingState(null, correlationData.getId(), "processed")));
new TrackingState()
.setCorrelationId(correlationData.getId())
.setStatus("processed")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.poc.boot.rabbitmq.model.Order;
import com.poc.boot.rabbitmq.service.OrderMessageSender;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
Expand All @@ -12,14 +11,16 @@
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequiredArgsConstructor
public class MessageController {
class MessageController {

private final OrderMessageSender orderMessageSender;

MessageController(OrderMessageSender orderMessageSender) {
this.orderMessageSender = orderMessageSender;
}

@PostMapping("/sendMsg")
public String handleMessage(
@ModelAttribute Order order, RedirectAttributes redirectAttributes) {
String handleMessage(@ModelAttribute Order order, RedirectAttributes redirectAttributes) {
try {
this.orderMessageSender.sendOrder(order);
redirectAttributes.addFlashAttribute("message", "Order message sent successfully");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.poc.boot.rabbitmq.entities;

import jakarta.persistence.*;
import lombok.*;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Setter
@Getter
public class TrackingState {

@Id
Expand All @@ -19,4 +17,33 @@ public class TrackingState {
private String correlationId;

private String status = "processed";

public TrackingState() {}

public Long getId() {
return id;
}

public TrackingState setId(Long id) {
this.id = id;
return this;
}

public String getCorrelationId() {
return correlationId;
}

public TrackingState setCorrelationId(String correlationId) {
this.correlationId = correlationId;
return this;
}

public String getStatus() {
return status;
}

public TrackingState setStatus(String status) {
this.status = status;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@

import com.poc.boot.rabbitmq.model.Order;
import com.poc.boot.rabbitmq.repository.TrackingStateRepository;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpRejectAndDontRequeueException;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;

@Slf4j
@Configuration(proxyBeanMethods = false)
public class OrderMessageListener {

private static final Logger log = LoggerFactory.getLogger(OrderMessageListener.class);
private final TrackingStateRepository trackingStateRepository;

public OrderMessageListener(TrackingStateRepository trackingStateRepository) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.poc.boot.rabbitmq.model.Order;
import com.poc.boot.rabbitmq.service.OrderMessageSender;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
Expand All @@ -15,13 +14,18 @@
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class OrderMessageSenderImpl implements OrderMessageSender {

private final RabbitTemplate templateWithConfirmsEnabled;

private final ObjectMapper objectMapper;

public OrderMessageSenderImpl(
RabbitTemplate templateWithConfirmsEnabled, ObjectMapper objectMapper) {
this.templateWithConfirmsEnabled = templateWithConfirmsEnabled;
this.objectMapper = objectMapper;
}

@Override
public void sendOrder(Order order) throws JsonProcessingException {
// this.rabbitTemplate.convertAndSend(RabbitConfig.QUEUE_ORDERS, order);
Expand Down
Loading

0 comments on commit 7277f15

Please sign in to comment.