Skip to content

Commit de32208

Browse files
committed
implement customer producer with rabbitmq
1 parent d07371b commit de32208

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

customer/src/main/java/com/hrk/customer/CustomerApplication.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
66
import org.springframework.cloud.openfeign.EnableFeignClients;
77

8-
@SpringBootApplication
8+
@SpringBootApplication(
9+
scanBasePackages = {
10+
"com.hrk.customer",
11+
"com.hrk.amqp"
12+
}
13+
)
914
@EnableEurekaClient
1015
@EnableFeignClients(
1116
basePackages = "com.hrk.clients"
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.hrk.customer;
22

3+
import com.hrk.amqp.RabbitMQMessageProducer;
34
import com.hrk.clients.fraud.FraudCheckResponse;
45
import com.hrk.clients.fraud.FraudClient;
5-
import com.hrk.clients.notification.NotificationClient;
66
import com.hrk.clients.notification.NotificationRequest;
77
import org.springframework.stereotype.Service;
8-
import org.springframework.web.client.RestTemplate;
98

109
@Service
11-
public record CustomerService(CustomerRepository repository, FraudClient fraudClient, NotificationClient notificationClient) {
10+
public record CustomerService(CustomerRepository repository, FraudClient fraudClient,
11+
RabbitMQMessageProducer rabbitMQMessageProducer) {
1212
public void registerCustomer(CustomerRegistrationRequest request) {
1313
Customer customer = Customer.builder()
1414
.firstName(request.firstName())
@@ -24,12 +24,11 @@ public void registerCustomer(CustomerRegistrationRequest request) {
2424
if (fraudCheckResponse.isFraudster())
2525
throw new IllegalStateException("fraudster");
2626

27-
notificationClient.sendNotification(
28-
new NotificationRequest(
29-
customer.getId(),
30-
customer.getEmail(),
31-
String.format("Hi %s welcome to HRK Academy...", customer.getFirstName())
32-
)
27+
NotificationRequest notificationRequest = new NotificationRequest(
28+
customer.getId(),
29+
customer.getEmail(),
30+
String.format("Hi %s welcome to HRK Academy...", customer.getFirstName())
3331
);
32+
rabbitMQMessageProducer.publish(notificationRequest, "internal.exchange", "internal.notification.routing-key");
3433
}
3534
}

customer/src/main/resources/application.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ spring:
2222
zipkin:
2323
base-url: http://localhost:9411
2424

25+
rabbitmq:
26+
addresses: localhost:5672
27+
2528
eureka:
2629
client:
2730
service-url:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.hrk.notification.rabbitmq;
2+
3+
import com.hrk.clients.notification.NotificationRequest;
4+
import com.hrk.notification.NotificationService;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.amqp.rabbit.annotation.RabbitListener;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
@Slf4j
11+
public record NotificationConsumer(NotificationService notificationService) {
12+
13+
@RabbitListener(queues = "${rabbitmq.queue.notification}")
14+
public void consumer(NotificationRequest notificationRequest){
15+
log.info("Consumed {} from queue", notificationRequest);
16+
notificationService.send(notificationRequest);
17+
}
18+
}

0 commit comments

Comments
 (0)