Skip to content

Commit

Permalink
using rabbitTemplateCustomizer
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Dec 26, 2024
1 parent 7277f15 commit b575f9e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 32 deletions.
8 changes: 0 additions & 8 deletions boot-rabbitmq-thymeleaf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,6 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.autoconfigure.amqp.RabbitTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.support.RetryTemplate;

@Configuration(proxyBeanMethods = false)
public class RabbitMQConfig {
Expand Down Expand Up @@ -79,22 +76,13 @@ Binding deadLetterBinding(Queue deadLetterQueue, FanoutExchange deadLetterExchan
return BindingBuilder.bind(deadLetterQueue).to(deadLetterExchange);
}

/* Bean for rabbitTemplate */
@Bean
RabbitTemplate templateWithConfirmsEnabled(
final ConnectionFactory connectionFactory,
final Jackson2JsonMessageConverter producerJackson2MessageConverter) {
final RabbitTemplate templateWithConfirmsEnabled = new RabbitTemplate(connectionFactory);
RetryTemplate retryTemplate = new RetryTemplate();
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
backOffPolicy.setInitialInterval(500);
backOffPolicy.setMultiplier(10.0);
backOffPolicy.setMaxInterval(10_000);
retryTemplate.setBackOffPolicy(backOffPolicy);
templateWithConfirmsEnabled.setRetryTemplate(retryTemplate);
templateWithConfirmsEnabled.setMessageConverter(producerJackson2MessageConverter);
templateWithConfirmsEnabled.setConfirmCallback(rabbitTemplateConfirmCallback);
return templateWithConfirmsEnabled;
RabbitTemplateCustomizer rabbitTemplateCustomizer(
Jackson2JsonMessageConverter producerJackson2MessageConverter) {
return rabbitTemplate -> {
rabbitTemplate.setMessageConverter(producerJackson2MessageConverter);
rabbitTemplate.setConfirmCallback(rabbitTemplateConfirmCallback);
};
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
@Service
public class OrderMessageSenderImpl implements OrderMessageSender {

private final RabbitTemplate templateWithConfirmsEnabled;
private final RabbitTemplate rabbitTemplate;

private final ObjectMapper objectMapper;

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

Expand All @@ -32,7 +31,7 @@ public void sendOrder(Order order) throws JsonProcessingException {

String orderJson = this.objectMapper.writeValueAsString(order);
String correlationId = UUID.randomUUID().toString();
this.templateWithConfirmsEnabled.convertAndSend(
this.rabbitTemplate.convertAndSend(
RabbitMQConfig.ORDERS_QUEUE,
getRabbitMQMessage(orderJson),
new CorrelationData(correlationId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ spring.rabbitmq.listener.simple.retry.multiplier=2
spring.rabbitmq.listener.simple.retry.max-interval=2s
spring.rabbitmq.listener.simple.acknowledge-mode=auto
spring.rabbitmq.listener.simple.observation-enabled=true
spring.rabbitmq.template.retry.enabled=true
spring.rabbitmq.template.retry.multiplier=5

spring.mvc.problemdetails.enabled=true
spring.threads.virtual.enabled=true
Expand Down

0 comments on commit b575f9e

Please sign in to comment.