From e5e152e2229c89fd05fa749fda36fa7dd1cdae8d Mon Sep 17 00:00:00 2001 From: eojin0814 Date: Thu, 23 Nov 2023 22:22:28 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20rabbitMQ=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 5 +++ .../service/SubscribeReadUseCase.java | 4 ++ .../global/feign/MemberFeignUseCase.java | 8 ++++ .../server/global/rabbitMQ/Email.java | 37 +++++++++++++++++++ .../global/rabbitMQ/EmailMsgSender.java | 31 ++++++++++++++++ .../global/rabbitMQ/RabbitMQConfig.java | 2 +- .../server/global/rabbitMQ/Receiver.java | 31 ---------------- .../global/rabbitMQ/SubscribeDataMessage.java | 26 +++++++++++++ 8 files changed, 112 insertions(+), 32 deletions(-) create mode 100644 src/main/java/gwangjang/server/global/rabbitMQ/Email.java create mode 100644 src/main/java/gwangjang/server/global/rabbitMQ/EmailMsgSender.java delete mode 100644 src/main/java/gwangjang/server/global/rabbitMQ/Receiver.java create mode 100644 src/main/java/gwangjang/server/global/rabbitMQ/SubscribeDataMessage.java diff --git a/build.gradle b/build.gradle index 9a2c38b..d5c0c65 100644 --- a/build.gradle +++ b/build.gradle @@ -32,6 +32,7 @@ ext { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'junit:junit:4.13.1' runtimeOnly 'com.mysql:mysql-connector-j' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:4.0.2' @@ -86,6 +87,10 @@ dependencies { testImplementation 'org.springframework.amqp:spring-rabbit-test' implementation 'org.springframework.boot:spring-boot-starter-amqp:2.5.4' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'org.mockito:mockito-core' + testImplementation 'org.mockito:mockito-core:3.11.2' + } dependencyManagement { diff --git a/src/main/java/gwangjang/server/domain/subscribe/application/service/SubscribeReadUseCase.java b/src/main/java/gwangjang/server/domain/subscribe/application/service/SubscribeReadUseCase.java index ed2f10b..7c41c98 100644 --- a/src/main/java/gwangjang/server/domain/subscribe/application/service/SubscribeReadUseCase.java +++ b/src/main/java/gwangjang/server/domain/subscribe/application/service/SubscribeReadUseCase.java @@ -33,6 +33,10 @@ public IssueSubscribeInfoRes getSubscribeIssueInfo(Long issueId) { public List getAllIssueBySubscribers() { return subscribeQueryService.getIssueBySubscribers(); } + //issue Id , counT값 보재문 + // 키워드로 보내서 + // 메인 url 연결 + //키워드랑 영역이랑 맵핑해서 diff --git a/src/main/java/gwangjang/server/global/feign/MemberFeignUseCase.java b/src/main/java/gwangjang/server/global/feign/MemberFeignUseCase.java index 9614081..9ec085b 100644 --- a/src/main/java/gwangjang/server/global/feign/MemberFeignUseCase.java +++ b/src/main/java/gwangjang/server/global/feign/MemberFeignUseCase.java @@ -3,15 +3,20 @@ import gwangjang.server.domain.auth.application.mapper.MemberMapper; import gwangjang.server.domain.member.domain.entity.Member; import gwangjang.server.domain.member.domain.service.MemberQueryService; +import gwangjang.server.domain.subscribe.adapter.producer.web.dto.SubscribeData; +import gwangjang.server.domain.subscribe.domain.service.SubscribeQueryService; import gwangjang.server.global.feign.dto.response.MemberDto; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.List; + @Service @RequiredArgsConstructor public class MemberFeignUseCase { private final MemberQueryService memberQueryService; + private final SubscribeQueryService subscribeQueryService; private final MemberMapper memberMapper = new MemberMapper(); @@ -19,4 +24,7 @@ public MemberDto getMemberDto(String socialId) { Member member = memberQueryService.getMemberBySocialId(socialId); return memberMapper.mapToMemberDto(member); } + public List getAllIssueBySubscribers() { + return subscribeQueryService.getIssueBySubscribers(); + } } diff --git a/src/main/java/gwangjang/server/global/rabbitMQ/Email.java b/src/main/java/gwangjang/server/global/rabbitMQ/Email.java new file mode 100644 index 0000000..13da122 --- /dev/null +++ b/src/main/java/gwangjang/server/global/rabbitMQ/Email.java @@ -0,0 +1,37 @@ +package gwangjang.server.global.rabbitMQ; + +public class Email { + + private String to; + private String body; + + public Email() { + } + + public Email(String to, String body) { + this.to = to; + this.body = body; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + @Override + public String toString() { + return String.format("Email{to=%s, body=%s}", getTo(), getBody()); + } + +} diff --git a/src/main/java/gwangjang/server/global/rabbitMQ/EmailMsgSender.java b/src/main/java/gwangjang/server/global/rabbitMQ/EmailMsgSender.java new file mode 100644 index 0000000..657772e --- /dev/null +++ b/src/main/java/gwangjang/server/global/rabbitMQ/EmailMsgSender.java @@ -0,0 +1,31 @@ +package gwangjang.server.global.rabbitMQ; + +import gwangjang.server.domain.subscribe.adapter.producer.web.dto.SubscribeData; +import gwangjang.server.domain.subscribe.domain.service.SubscribeQueryService; +import org.springframework.amqp.core.TopicExchange; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class EmailMsgSender { + + @Autowired + private RabbitTemplate rabbitTemplate; + @Autowired + private SubscribeQueryService subscribeQueryService; + + @Autowired + private TopicExchange topic; + + public void sendEmail(String routingKey, Email email){ + rabbitTemplate.convertAndSend(topic.getName(), routingKey, email); + } + public void sendSubscribeData(String routingKey) { + List subscribeDataList = subscribeQueryService.getIssueBySubscribers(); + SubscribeDataMessage message = new SubscribeDataMessage(subscribeDataList); + rabbitTemplate.convertAndSend(topic.getName(), routingKey, message); + } +} \ No newline at end of file diff --git a/src/main/java/gwangjang/server/global/rabbitMQ/RabbitMQConfig.java b/src/main/java/gwangjang/server/global/rabbitMQ/RabbitMQConfig.java index 0965c0f..c5cd640 100644 --- a/src/main/java/gwangjang/server/global/rabbitMQ/RabbitMQConfig.java +++ b/src/main/java/gwangjang/server/global/rabbitMQ/RabbitMQConfig.java @@ -26,4 +26,4 @@ public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) rabbitTemplate.setMessageConverter(producerJackson2MessageConverter()); return rabbitTemplate; } -} +} \ No newline at end of file diff --git a/src/main/java/gwangjang/server/global/rabbitMQ/Receiver.java b/src/main/java/gwangjang/server/global/rabbitMQ/Receiver.java deleted file mode 100644 index 93ad1cc..0000000 --- a/src/main/java/gwangjang/server/global/rabbitMQ/Receiver.java +++ /dev/null @@ -1,31 +0,0 @@ -package gwangjang.server.global.rabbitMQ; - - -import org.springframework.amqp.rabbit.annotation.Exchange; -import org.springframework.amqp.rabbit.annotation.Queue; -import org.springframework.amqp.rabbit.annotation.QueueBinding; -import org.springframework.amqp.rabbit.annotation.RabbitListener; -import org.springframework.stereotype.Component; - -import java.util.concurrent.CountDownLatch; - -@Component -public class Receiver { - - @RabbitListener(bindings = @QueueBinding( - value = @Queue, - exchange = @Exchange(value = "amq.topic", type = "topic", durable = "true"), // - key = "test")) - public void handleMsg1() { - System.out.println("test"); - } - - @RabbitListener(bindings = @QueueBinding( - value = @Queue(value = "kkaok", durable = "true"), - exchange = @Exchange(value = "amq.topic", type = "topic", durable = "true"), // - key = "test.0001")) - public void handleMsg2() { - System.out.println("test"); - } - -} diff --git a/src/main/java/gwangjang/server/global/rabbitMQ/SubscribeDataMessage.java b/src/main/java/gwangjang/server/global/rabbitMQ/SubscribeDataMessage.java new file mode 100644 index 0000000..825510b --- /dev/null +++ b/src/main/java/gwangjang/server/global/rabbitMQ/SubscribeDataMessage.java @@ -0,0 +1,26 @@ +package gwangjang.server.global.rabbitMQ; + +import gwangjang.server.domain.subscribe.adapter.producer.web.dto.SubscribeData; + +import java.io.Serializable; +import java.util.List; + +public class SubscribeDataMessage implements Serializable { + private List subscribeDataList; + + public SubscribeDataMessage() { + // Default constructor needed for deserialization + } + + public SubscribeDataMessage(List subscribeDataList) { + this.subscribeDataList = subscribeDataList; + } + + public List getSubscribeDataList() { + return subscribeDataList; + } + + public void setSubscribeDataList(List subscribeDataList) { + this.subscribeDataList = subscribeDataList; + } +}