Skip to content

Commit d07371b

Browse files
committed
implement listener for notification module & test it
1 parent 72ba2e3 commit d07371b

File tree

4 files changed

+92
-4
lines changed

4 files changed

+92
-4
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.hrk.amqp;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.amqp.core.AmqpTemplate;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
@Slf4j
9+
public record RabbitMQMessageProducer(AmqpTemplate amqpTemplate) {
10+
11+
public void publish(Object payload, String exchange, String routingKey) {
12+
log.info("Publishing to {} using routingKey: {}. payload: {}", exchange, routingKey, payload);
13+
amqpTemplate.convertAndSend(exchange, routingKey, payload);
14+
log.info("Published to {} using routingKey: {}. payload: {}", exchange, routingKey, payload);
15+
}
16+
}

notification/src/main/java/com/hrk/notification/NotificationApplication.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,31 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
65

7-
@SpringBootApplication
8-
@EnableEurekaClient
6+
@SpringBootApplication(
7+
scanBasePackages = {
8+
"com.hrk.notification",
9+
"com.hrk.amqp"
10+
}
11+
)
912
public class NotificationApplication {
1013
public static void main(String[] args) {
1114
SpringApplication.run(NotificationApplication.class, args);
1215
}
16+
17+
// @Bean
18+
// CommandLineRunner commandLineRunner(
19+
// RabbitMQMessageProducer producer,
20+
// NotificationConfig notificationConfig
21+
// ) {
22+
// return args -> {
23+
// producer.publish(
24+
// new Person("omid", 24),
25+
// notificationConfig.getInternalExchange(),
26+
// notificationConfig.getInternalNotificationRoutingKey()
27+
// );
28+
// };
29+
// }
30+
//
31+
// record Person(String name, int age){};
1332
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.hrk.notification;
2+
3+
import lombok.Getter;
4+
import org.springframework.amqp.core.Binding;
5+
import org.springframework.amqp.core.BindingBuilder;
6+
import org.springframework.amqp.core.Queue;
7+
import org.springframework.amqp.core.TopicExchange;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
@Configuration
13+
@Getter
14+
public class NotificationConfig {
15+
16+
@Value("${rabbitmq.exchanges.internal}")
17+
private String internalExchange;
18+
19+
@Value("${rabbitmq.queue.notification}")
20+
private String notificationQueue;
21+
22+
@Value("${rabbitmq.routing-keys.internal-notification}")
23+
private String internalNotificationRoutingKey;
24+
25+
@Bean
26+
public TopicExchange internalTopicExchange(){
27+
return new TopicExchange(this.internalExchange);
28+
}
29+
30+
@Bean
31+
public Queue notificationQueue(){
32+
return new Queue(this.notificationQueue);
33+
}
34+
35+
@Bean
36+
public Binding exchangeToQueueBinding(){
37+
return BindingBuilder
38+
.bind(notificationQueue())
39+
.to(internalTopicExchange())
40+
.with(this.internalNotificationRoutingKey);
41+
}
42+
}

notification/src/main/resources/application.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,20 @@ spring:
2222
zipkin:
2323
base-url: http://localhost:9411
2424

25+
rabbitmq:
26+
addresses: localhost:5672
27+
2528
eureka:
2629
client:
2730
service-url:
2831
defaultZone: http://localhost:8761/eureka
2932
fetch-registry: true
30-
register-with-eureka: true
33+
register-with-eureka: true
34+
35+
rabbitmq:
36+
exchanges:
37+
internal: internal.exchange
38+
queue:
39+
notification: notification.queue
40+
routing-keys:
41+
internal-notification: internal.notification.routing-key

0 commit comments

Comments
 (0)