From 69dbe3a3fe37f4ad39bdbbe7335627949158285f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=ED=9A=8C?= Date: Mon, 22 Jan 2024 17:25:24 +0900 Subject: [PATCH 01/11] =?UTF-8?q?refactor:=20fcm=20sdk=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=20=EB=B0=8F=20fcm=20=EC=84=9C=EB=B2=84=EC=B8=A1=20?= =?UTF-8?q?=EC=95=8C=EB=A6=BC=EC=B2=98=EB=A6=AC=20=EB=B9=84=EB=8F=99?= =?UTF-8?q?=EA=B8=B0=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pium/admin/ui/TestController.java | 31 +++-- .../fcm/application/FcmMessageSender.java | 117 ++++++++++++------ 2 files changed, 94 insertions(+), 54 deletions(-) diff --git a/backend/pium/src/main/java/com/official/pium/admin/ui/TestController.java b/backend/pium/src/main/java/com/official/pium/admin/ui/TestController.java index 555d26df..e40fdfcf 100644 --- a/backend/pium/src/main/java/com/official/pium/admin/ui/TestController.java +++ b/backend/pium/src/main/java/com/official/pium/admin/ui/TestController.java @@ -1,6 +1,9 @@ package com.official.pium.admin.ui; +import com.official.pium.admin.service.TestService; import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -9,23 +12,17 @@ @RequestMapping("/test") public class TestController { -// private final TestService testService; + private final TestService testService; -// @GetMapping("/notifications") -// public ResponseEntity notificationTest() { -// testService.sendWaterNotificationTest(); -// return ResponseEntity.ok("알림 기능 테스트 성공"); -// } + @GetMapping("/notifications/ramp") + public ResponseEntity notificationRampTest() { + testService.sendWaterNotificationAsyncRampTest(); + return ResponseEntity.ok("비동기 알림 기능 테스트 램프업 성공"); + } -// @GetMapping("/notifications/ramp") -// public ResponseEntity notificationRampTest() { -// testService.sendWaterNotificationAsyncRampTest(); -// return ResponseEntity.ok("비동기 알림 기능 테스트 램프업 성공"); -// } -// -// @GetMapping("/notifications/async") -// public ResponseEntity notificationAsyncTest() { -// testService.sendWaterNotificationAsyncTest(); -// return ResponseEntity.ok("비동기 알림 기능 테스트 성공"); -// } + @GetMapping("/notifications/async") + public ResponseEntity notificationAsyncTest() { + testService.sendWaterNotificationAsyncTest(); + return ResponseEntity.ok("비동기 알림 기능 테스트 성공"); + } } diff --git a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java index a595c90b..7f49161d 100644 --- a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java +++ b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java @@ -1,25 +1,32 @@ package com.official.pium.notification.fcm.application; import com.google.auth.oauth2.GoogleCredentials; -import com.official.pium.notification.fcm.dto.FcmMessageResponse; -import com.official.pium.notification.fcm.exception.FcmException; +import com.google.firebase.FirebaseApp; +import com.google.firebase.FirebaseOptions; +import com.google.firebase.messaging.FirebaseMessaging; +import com.google.firebase.messaging.Message; +import com.google.firebase.messaging.Notification; import com.official.pium.notification.application.MessageSendManager; +import com.official.pium.notification.fcm.dto.FcmMessageResponse; +import jakarta.annotation.PostConstruct; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.concurrent.ExecutionException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; -import java.io.IOException; @Slf4j @Component @RequiredArgsConstructor public class FcmMessageSender implements MessageSendManager { + private static final String JSON_FILE_PATH = "src/main/resources/config/pium-fcm.json"; + @Value("${fcm.api.url}") private String apiUrl; @@ -31,50 +38,86 @@ public class FcmMessageSender implements MessageSendManager { private final RestTemplate restTemplate; - public void sendMessageTo(String targetToken, String title, String body) { + @PostConstruct + public void initialize() { + FileInputStream serviceAccount; try { - FcmMessageResponse message = makeMessage(targetToken, title, body); - - HttpHeaders headers = new HttpHeaders(); - headers.set(HttpHeaders.AUTHORIZATION, "Bearer " + getAccessToken()); - headers.set(HttpHeaders.CONTENT_TYPE, "application/json; UTF-8"); - - HttpEntity request = new HttpEntity<>(message, headers); - - ResponseEntity postResult = restTemplate.postForEntity( - apiUrl, - request, - FcmMessageResponse.class - ); + serviceAccount = new FileInputStream(JSON_FILE_PATH); + FirebaseOptions options = FirebaseOptions.builder() + .setCredentials(GoogleCredentials.fromStream(serviceAccount)) + .build(); - log.info("FCM 메시지 전송 성공: {}", postResult.getBody()); + FirebaseApp.initializeApp(options); + } catch (FileNotFoundException e) { + log.error("파일을 찾을 수 없습니다. " + e); + } catch (IOException e) { + log.error("FCM 인증이 실패했습니다. " + e); + } + } - } catch (Exception e) { - log.error("FCM 메시지 전송 실패", e); - throw new FcmException.FcmMessageSendException(e.getMessage()); + public void sendMessageTo(String targetToken, String title, String body) { + Notification notification = Notification.builder() + .setTitle(title) + .setBody(body) + .build(); + Message message = Message.builder() + .setToken(targetToken) + .setNotification(notification) + .build(); + try { + String response = FirebaseMessaging.getInstance().sendAsync(message).get(); + log.info("응답 결과 : " + response); + } catch (InterruptedException e) { + log.error(e.getMessage()); + } catch (ExecutionException e) { + log.error(e.getMessage()); } } +// public void sendMessageTo(String targetToken, String title, String body) { +// try { +// FcmMessageResponse message = makeMessage(targetToken, title, body); +// +// HttpHeaders headers = new HttpHeaders(); +// headers.set(HttpHeaders.AUTHORIZATION, "Bearer " + getAccessToken()); +// headers.set(HttpHeaders.CONTENT_TYPE, "application/json; UTF-8"); +// +// HttpEntity request = new HttpEntity<>(message, headers); +// +// ResponseEntity postResult = restTemplate.postForEntity( +// apiUrl, +// request, +// FcmMessageResponse.class +// ); +// +// log.info("FCM 메시지 전송 성공: {}", postResult.getBody()); +// +// } catch (Exception e) { +// log.error("FCM 메시지 전송 실패", e); +// throw new FcmException.FcmMessageSendException(e.getMessage()); +// } +// } + private FcmMessageResponse makeMessage(String targetToken, String title, String body) { return FcmMessageResponse.builder() - .message(FcmMessageResponse.Message.builder() - .token(targetToken) - .notification(FcmMessageResponse.Notification.builder() - .title(title) - .body(body) - .image(null) - .build() - ) - .build() + .message(FcmMessageResponse.Message.builder() + .token(targetToken) + .notification(FcmMessageResponse.Notification.builder() + .title(title) + .body(body) + .image(null) + .build() ) - .validate_only(false) - .build(); + .build() + ) + .validate_only(false) + .build(); } private String getAccessToken() throws IOException { GoogleCredentials googleCredentials = GoogleCredentials - .fromStream(new ClassPathResource(keyPath).getInputStream()) - .createScoped(keyScope); + .fromStream(new ClassPathResource(keyPath).getInputStream()) + .createScoped(keyScope); googleCredentials.refreshIfExpired(); return googleCredentials.getAccessToken().getTokenValue(); } From b92cfc63bedc332c22ace14bb4e0461bbc18c74c Mon Sep 17 00:00:00 2001 From: DongUk <68818952+Kim0914@users.noreply.github.com> Date: Mon, 22 Jan 2024 17:59:07 +0900 Subject: [PATCH 02/11] =?UTF-8?q?refactor:=20FCM=20=EC=B4=88=EA=B8=B0?= =?UTF-8?q?=ED=99=94=20=EB=A1=9C=EC=A7=81=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fcm/application/FcmMessageSender.java | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java index 7f49161d..5bb7b5f7 100644 --- a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java +++ b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java @@ -44,10 +44,12 @@ public void initialize() { try { serviceAccount = new FileInputStream(JSON_FILE_PATH); FirebaseOptions options = FirebaseOptions.builder() - .setCredentials(GoogleCredentials.fromStream(serviceAccount)) - .build(); + .setCredentials(GoogleCredentials.fromStream(serviceAccount)) + .build(); - FirebaseApp.initializeApp(options); + if (FirebaseApp.getApps().isEmpty()) { + FirebaseApp.initializeApp(options, "Pium"); + } } catch (FileNotFoundException e) { log.error("파일을 찾을 수 없습니다. " + e); } catch (IOException e) { @@ -57,13 +59,13 @@ public void initialize() { public void sendMessageTo(String targetToken, String title, String body) { Notification notification = Notification.builder() - .setTitle(title) - .setBody(body) - .build(); + .setTitle(title) + .setBody(body) + .build(); Message message = Message.builder() - .setToken(targetToken) - .setNotification(notification) - .build(); + .setToken(targetToken) + .setNotification(notification) + .build(); try { String response = FirebaseMessaging.getInstance().sendAsync(message).get(); log.info("응답 결과 : " + response); @@ -100,24 +102,24 @@ public void sendMessageTo(String targetToken, String title, String body) { private FcmMessageResponse makeMessage(String targetToken, String title, String body) { return FcmMessageResponse.builder() - .message(FcmMessageResponse.Message.builder() - .token(targetToken) - .notification(FcmMessageResponse.Notification.builder() - .title(title) - .body(body) - .image(null) - .build() + .message(FcmMessageResponse.Message.builder() + .token(targetToken) + .notification(FcmMessageResponse.Notification.builder() + .title(title) + .body(body) + .image(null) + .build() + ) + .build() ) - .build() - ) - .validate_only(false) - .build(); + .validate_only(false) + .build(); } private String getAccessToken() throws IOException { GoogleCredentials googleCredentials = GoogleCredentials - .fromStream(new ClassPathResource(keyPath).getInputStream()) - .createScoped(keyScope); + .fromStream(new ClassPathResource(keyPath).getInputStream()) + .createScoped(keyScope); googleCredentials.refreshIfExpired(); return googleCredentials.getAccessToken().getTokenValue(); } From 53ba5120678a30f92d60c49bc510d8cd47db395a Mon Sep 17 00:00:00 2001 From: DongUk <68818952+Kim0914@users.noreply.github.com> Date: Mon, 22 Jan 2024 18:10:14 +0900 Subject: [PATCH 03/11] =?UTF-8?q?refactor:=20FCM=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EA=B2=BD=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pium/notification/fcm/application/FcmMessageSender.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java index 5bb7b5f7..b9747e86 100644 --- a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java +++ b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java @@ -25,7 +25,8 @@ @RequiredArgsConstructor public class FcmMessageSender implements MessageSendManager { - private static final String JSON_FILE_PATH = "src/main/resources/config/pium-fcm.json"; + private static final String SYSTEM_PATH = System.getProperty("user.dir"); + private static final String JSON_FILE_PATH = "/src/main/resources/config/pium-fcm.json"; @Value("${fcm.api.url}") private String apiUrl; @@ -42,7 +43,7 @@ public class FcmMessageSender implements MessageSendManager { public void initialize() { FileInputStream serviceAccount; try { - serviceAccount = new FileInputStream(JSON_FILE_PATH); + serviceAccount = new FileInputStream(SYSTEM_PATH + JSON_FILE_PATH); FirebaseOptions options = FirebaseOptions.builder() .setCredentials(GoogleCredentials.fromStream(serviceAccount)) .build(); From 085478ec6e1b42e853cb677eae812ef2ca8a7648 Mon Sep 17 00:00:00 2001 From: DongUk <68818952+Kim0914@users.noreply.github.com> Date: Mon, 22 Jan 2024 18:17:57 +0900 Subject: [PATCH 04/11] =?UTF-8?q?refactor:=20FCM=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=20=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pium/notification/fcm/application/FcmMessageSender.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java index b9747e86..ee5e93c7 100644 --- a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java +++ b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java @@ -49,7 +49,7 @@ public void initialize() { .build(); if (FirebaseApp.getApps().isEmpty()) { - FirebaseApp.initializeApp(options, "Pium"); + FirebaseApp.initializeApp(options); } } catch (FileNotFoundException e) { log.error("파일을 찾을 수 없습니다. " + e); From df6960aebe9f61633cb093732ef91ef433100520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B1=B4=ED=9A=8C?= Date: Mon, 22 Jan 2024 18:48:54 +0900 Subject: [PATCH 05/11] =?UTF-8?q?test:=20=EC=95=8C=EB=A6=BC=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EA=B3=84=EC=A0=95=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/official/pium/admin/service/TestService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/pium/src/main/java/com/official/pium/admin/service/TestService.java b/backend/pium/src/main/java/com/official/pium/admin/service/TestService.java index b12cba39..3d2ebdd6 100644 --- a/backend/pium/src/main/java/com/official/pium/admin/service/TestService.java +++ b/backend/pium/src/main/java/com/official/pium/admin/service/TestService.java @@ -34,7 +34,7 @@ public class TestService { // } public void sendWaterNotificationAsyncRampTest() { - List petPlants = petPlantRepository.findAllByMemberId(6L); + List petPlants = petPlantRepository.findAllByMemberId(7L); // List events = petPlants.stream() // .map(plant -> NotificationEvent.builder() // .title(plant.getNickname()) @@ -61,7 +61,7 @@ public void sendWaterNotificationAsyncRampTest() { } public void sendWaterNotificationAsyncTest() { - List petPlants = petPlantRepository.findAllByMemberId(6L); + List petPlants = petPlantRepository.findAllByMemberId(7L); List events = petPlants.stream() .map(plant -> NotificationEvent.builder() .title(plant.getNickname()) From ec1c8823bc635fa41a794f3303c77587d5ac2371 Mon Sep 17 00:00:00 2001 From: DongUk <68818952+Kim0914@users.noreply.github.com> Date: Mon, 22 Jan 2024 20:25:18 +0900 Subject: [PATCH 06/11] =?UTF-8?q?chore:=20Async=20=EC=8A=A4=EB=A0=88?= =?UTF-8?q?=EB=93=9C=20=ED=92=80=20=EA=B0=9C=EC=88=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/official/pium/config/AsyncConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/pium/src/main/java/com/official/pium/config/AsyncConfig.java b/backend/pium/src/main/java/com/official/pium/config/AsyncConfig.java index 3518b7b7..50381417 100644 --- a/backend/pium/src/main/java/com/official/pium/config/AsyncConfig.java +++ b/backend/pium/src/main/java/com/official/pium/config/AsyncConfig.java @@ -13,7 +13,7 @@ public class AsyncConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); - executor.setCorePoolSize(40); + executor.setCorePoolSize(20); executor.setThreadNamePrefix("2024-Pium-Thread: "); executor.initialize(); return executor; From 397222ea13f54f675108dfc2be0d9ab688201e2e Mon Sep 17 00:00:00 2001 From: DongUk <68818952+Kim0914@users.noreply.github.com> Date: Mon, 22 Jan 2024 21:02:54 +0900 Subject: [PATCH 07/11] =?UTF-8?q?chore:=20=EC=95=8C=EB=A6=BC=20=EB=A1=9C?= =?UTF-8?q?=EA=B9=85=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EB=B6=88=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20=EC=BD=94=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fcm/application/FcmMessageSender.java | 81 ++----------------- 1 file changed, 7 insertions(+), 74 deletions(-) diff --git a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java index ee5e93c7..e47542a0 100644 --- a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java +++ b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java @@ -7,54 +7,35 @@ import com.google.firebase.messaging.Message; import com.google.firebase.messaging.Notification; import com.official.pium.notification.application.MessageSendManager; -import com.official.pium.notification.fcm.dto.FcmMessageResponse; import jakarta.annotation.PostConstruct; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.concurrent.ExecutionException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; @Slf4j @Component @RequiredArgsConstructor public class FcmMessageSender implements MessageSendManager { - private static final String SYSTEM_PATH = System.getProperty("user.dir"); - private static final String JSON_FILE_PATH = "/src/main/resources/config/pium-fcm.json"; - - @Value("${fcm.api.url}") - private String apiUrl; - - @Value("${fcm.key.path}") - private String keyPath; - - @Value("${fcm.key.scope}") - private String keyScope; - - private final RestTemplate restTemplate; - @PostConstruct public void initialize() { - FileInputStream serviceAccount; try { - serviceAccount = new FileInputStream(SYSTEM_PATH + JSON_FILE_PATH); + ClassPathResource resource = new ClassPathResource("config/pium-fcm.json"); FirebaseOptions options = FirebaseOptions.builder() - .setCredentials(GoogleCredentials.fromStream(serviceAccount)) + .setCredentials(GoogleCredentials.fromStream(resource.getInputStream())) .build(); if (FirebaseApp.getApps().isEmpty()) { FirebaseApp.initializeApp(options); } } catch (FileNotFoundException e) { - log.error("파일을 찾을 수 없습니다. " + e); + log.error("파일을 찾을 수 없습니다. ", e); } catch (IOException e) { - log.error("FCM 인증이 실패했습니다. " + e); + log.error("FCM 인증이 실패했습니다. ", e); } } @@ -69,59 +50,11 @@ public void sendMessageTo(String targetToken, String title, String body) { .build(); try { String response = FirebaseMessaging.getInstance().sendAsync(message).get(); - log.info("응답 결과 : " + response); + log.info("알림 전송 성공 : " + response); } catch (InterruptedException e) { - log.error(e.getMessage()); + log.error("FCM 알림 스레드에서 문제가 발생했습니다.", e); } catch (ExecutionException e) { - log.error(e.getMessage()); + log.error("FCM 알림 전송에 실패했습니다.", e); } } - -// public void sendMessageTo(String targetToken, String title, String body) { -// try { -// FcmMessageResponse message = makeMessage(targetToken, title, body); -// -// HttpHeaders headers = new HttpHeaders(); -// headers.set(HttpHeaders.AUTHORIZATION, "Bearer " + getAccessToken()); -// headers.set(HttpHeaders.CONTENT_TYPE, "application/json; UTF-8"); -// -// HttpEntity request = new HttpEntity<>(message, headers); -// -// ResponseEntity postResult = restTemplate.postForEntity( -// apiUrl, -// request, -// FcmMessageResponse.class -// ); -// -// log.info("FCM 메시지 전송 성공: {}", postResult.getBody()); -// -// } catch (Exception e) { -// log.error("FCM 메시지 전송 실패", e); -// throw new FcmException.FcmMessageSendException(e.getMessage()); -// } -// } - - private FcmMessageResponse makeMessage(String targetToken, String title, String body) { - return FcmMessageResponse.builder() - .message(FcmMessageResponse.Message.builder() - .token(targetToken) - .notification(FcmMessageResponse.Notification.builder() - .title(title) - .body(body) - .image(null) - .build() - ) - .build() - ) - .validate_only(false) - .build(); - } - - private String getAccessToken() throws IOException { - GoogleCredentials googleCredentials = GoogleCredentials - .fromStream(new ClassPathResource(keyPath).getInputStream()) - .createScoped(keyScope); - googleCredentials.refreshIfExpired(); - return googleCredentials.getAccessToken().getTokenValue(); - } } From 370e9318f315f56e8e294d5fd88df896e106113e Mon Sep 17 00:00:00 2001 From: DongUk <68818952+Kim0914@users.noreply.github.com> Date: Thu, 25 Jan 2024 16:14:49 +0900 Subject: [PATCH 08/11] =?UTF-8?q?refactor:=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EA=B2=BD=EB=A1=9C=20=EC=A0=95=EC=A0=81=20=EB=B3=80=EC=88=98?= =?UTF-8?q?=EB=A1=9C=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pium/notification/fcm/application/FcmMessageSender.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java index e47542a0..ba1109e5 100644 --- a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java +++ b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java @@ -21,10 +21,12 @@ @RequiredArgsConstructor public class FcmMessageSender implements MessageSendManager { + private static final String FCM_JSON_PATH = "config/pium-fcm.json"; + @PostConstruct public void initialize() { try { - ClassPathResource resource = new ClassPathResource("config/pium-fcm.json"); + ClassPathResource resource = new ClassPathResource(FCM_JSON_PATH); FirebaseOptions options = FirebaseOptions.builder() .setCredentials(GoogleCredentials.fromStream(resource.getInputStream())) .build(); From 0830d1226341fb4030c285e4209e4a3774b381a6 Mon Sep 17 00:00:00 2001 From: DongUk <68818952+Kim0914@users.noreply.github.com> Date: Thu, 25 Jan 2024 16:23:45 +0900 Subject: [PATCH 09/11] =?UTF-8?q?refactor:=20=EB=B9=84=EB=8F=99=EA=B8=B0?= =?UTF-8?q?=20=EC=8A=A4=EB=A0=88=EB=93=9C=20=ED=92=80=2040=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/official/pium/config/AsyncConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/pium/src/main/java/com/official/pium/config/AsyncConfig.java b/backend/pium/src/main/java/com/official/pium/config/AsyncConfig.java index 50381417..3518b7b7 100644 --- a/backend/pium/src/main/java/com/official/pium/config/AsyncConfig.java +++ b/backend/pium/src/main/java/com/official/pium/config/AsyncConfig.java @@ -13,7 +13,7 @@ public class AsyncConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); - executor.setCorePoolSize(20); + executor.setCorePoolSize(40); executor.setThreadNamePrefix("2024-Pium-Thread: "); executor.initialize(); return executor; From c89909690ee7b3edad02f64b52db909d3e6b02ae Mon Sep 17 00:00:00 2001 From: DongUk <68818952+Kim0914@users.noreply.github.com> Date: Thu, 25 Jan 2024 16:26:28 +0900 Subject: [PATCH 10/11] =?UTF-8?q?chore:=20=ED=98=84=EC=9E=AC=20=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=A3=BC?= =?UTF-8?q?=EC=84=9D=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pium/admin/service/TestService.java | 63 +++++++++---------- .../pium/admin/ui/TestController.java | 24 ++++--- 2 files changed, 41 insertions(+), 46 deletions(-) diff --git a/backend/pium/src/main/java/com/official/pium/admin/service/TestService.java b/backend/pium/src/main/java/com/official/pium/admin/service/TestService.java index 3d2ebdd6..0a3b2856 100644 --- a/backend/pium/src/main/java/com/official/pium/admin/service/TestService.java +++ b/backend/pium/src/main/java/com/official/pium/admin/service/TestService.java @@ -1,9 +1,6 @@ package com.official.pium.admin.service; -import com.official.pium.petPlant.domain.PetPlant; -import com.official.pium.petPlant.event.notification.NotificationEvent; import com.official.pium.petPlant.repository.PetPlantRepository; -import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationEventPublisher; @@ -33,8 +30,8 @@ public class TestService { // log.info("동기 알림 테스트 종료. Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName()); // } - public void sendWaterNotificationAsyncRampTest() { - List petPlants = petPlantRepository.findAllByMemberId(7L); +// public void sendWaterNotificationAsyncRampTest() { +// List petPlants = petPlantRepository.findAllByMemberId(7L); // List events = petPlants.stream() // .map(plant -> NotificationEvent.builder() // .title(plant.getNickname()) @@ -43,39 +40,39 @@ public void sendWaterNotificationAsyncRampTest() { // .build() // ).toList(); - for (int i = 0; i < 100; i++) { - PetPlant petPlant = petPlants.get(i); - NotificationEvent event = NotificationEvent.builder() - .title(petPlant.getNickname()) - .body("물줘") - .deviceToken(petPlant.getMember().getDeviceToken()) - .build(); - publisher.publishEvent(event); - } +// for (int i = 0; i < 100; i++) { +// PetPlant petPlant = petPlants.get(i); +// NotificationEvent event = NotificationEvent.builder() +// .title(petPlant.getNickname()) +// .body("물줘") +// .deviceToken(petPlant.getMember().getDeviceToken()) +// .build(); +// publisher.publishEvent(event); +// } // log.info("비동기 테스트 램프업 시작"); // for (int i = 0; i < 100; i++) { // NotificationEvent notificationEvent = events.get(i); // publisher.publishEvent(notificationEvent); // } - } - - public void sendWaterNotificationAsyncTest() { - List petPlants = petPlantRepository.findAllByMemberId(7L); - List events = petPlants.stream() - .map(plant -> NotificationEvent.builder() - .title(plant.getNickname()) - .body("(테스트 중) 물을 줄 시간이에요!") - .deviceToken(plant.getMember().getDeviceToken()) - .build() - ).toList(); +// } - int i = 1; - log.info("비동기 알림 테스트 시작. Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName()); - for (NotificationEvent event : events) { - log.info(i++ + "번째 알림 이벤트"); - publisher.publishEvent(event); - } - log.info("비동기 알림 테스트 종료. Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName()); - } +// public void sendWaterNotificationAsyncTest() { +// List petPlants = petPlantRepository.findAllByMemberId(7L); +// List events = petPlants.stream() +// .map(plant -> NotificationEvent.builder() +// .title(plant.getNickname()) +// .body("(테스트 중) 물을 줄 시간이에요!") +// .deviceToken(plant.getMember().getDeviceToken()) +// .build() +// ).toList(); +// +// int i = 1; +// log.info("비동기 알림 테스트 시작. Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName()); +// for (NotificationEvent event : events) { +// log.info(i++ + "번째 알림 이벤트"); +// publisher.publishEvent(event); +// } +// log.info("비동기 알림 테스트 종료. Thread: " + Thread.currentThread().getId() + " " + Thread.currentThread().getName()); +// } } diff --git a/backend/pium/src/main/java/com/official/pium/admin/ui/TestController.java b/backend/pium/src/main/java/com/official/pium/admin/ui/TestController.java index e40fdfcf..1d0514a0 100644 --- a/backend/pium/src/main/java/com/official/pium/admin/ui/TestController.java +++ b/backend/pium/src/main/java/com/official/pium/admin/ui/TestController.java @@ -2,8 +2,6 @@ import com.official.pium.admin.service.TestService; import lombok.RequiredArgsConstructor; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -14,15 +12,15 @@ public class TestController { private final TestService testService; - @GetMapping("/notifications/ramp") - public ResponseEntity notificationRampTest() { - testService.sendWaterNotificationAsyncRampTest(); - return ResponseEntity.ok("비동기 알림 기능 테스트 램프업 성공"); - } - - @GetMapping("/notifications/async") - public ResponseEntity notificationAsyncTest() { - testService.sendWaterNotificationAsyncTest(); - return ResponseEntity.ok("비동기 알림 기능 테스트 성공"); - } +// @GetMapping("/notifications/ramp") +// public ResponseEntity notificationRampTest() { +// testService.sendWaterNotificationAsyncRampTest(); +// return ResponseEntity.ok("비동기 알림 기능 테스트 램프업 성공"); +// } +// +// @GetMapping("/notifications/async") +// public ResponseEntity notificationAsyncTest() { +// testService.sendWaterNotificationAsyncTest(); +// return ResponseEntity.ok("비동기 알림 기능 테스트 성공"); +// } } From 2df1dd3a5087c4c73a7f61101bbb96622b9f63ce Mon Sep 17 00:00:00 2001 From: DongUk <68818952+Kim0914@users.noreply.github.com> Date: Thu, 25 Jan 2024 16:34:33 +0900 Subject: [PATCH 11/11] =?UTF-8?q?chore:=20FCM=20=EC=A0=95=EC=A0=81=20?= =?UTF-8?q?=EB=B3=80=EC=88=98=20=EB=B9=88=20=EC=A3=BC=EC=9E=85=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notification/fcm/application/FcmMessageSender.java | 4 +++- backend/pium/src/main/resources/application.yml | 8 +++----- backend/pium/src/test/resources/application.yml | 5 +---- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java index ba1109e5..eba7f6dc 100644 --- a/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java +++ b/backend/pium/src/main/java/com/official/pium/notification/fcm/application/FcmMessageSender.java @@ -13,6 +13,7 @@ import java.util.concurrent.ExecutionException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Component; @@ -21,7 +22,8 @@ @RequiredArgsConstructor public class FcmMessageSender implements MessageSendManager { - private static final String FCM_JSON_PATH = "config/pium-fcm.json"; + @Value("${fcm.json.path}") + private String FCM_JSON_PATH; @PostConstruct public void initialize() { diff --git a/backend/pium/src/main/resources/application.yml b/backend/pium/src/main/resources/application.yml index 5b1ccb8f..7d605ec1 100644 --- a/backend/pium/src/main/resources/application.yml +++ b/backend/pium/src/main/resources/application.yml @@ -27,11 +27,9 @@ management: endpoints: enabled-by-default: false fcm: - key: - path: test/ - scope: https://www.googleapis.com/auth/firebase.messaging - api: - url: https://fcm.googleapis.com/v1/projects/project-id/messages:send + json: + path: config/pium-fcm.json + petPlant: image: directory: test diff --git a/backend/pium/src/test/resources/application.yml b/backend/pium/src/test/resources/application.yml index f25c9199..8fc7a303 100644 --- a/backend/pium/src/test/resources/application.yml +++ b/backend/pium/src/test/resources/application.yml @@ -64,11 +64,8 @@ server: max-http-form-post-size: 10MB fcm: - key: + json: path: config/pium-fcm.json - scope: https://www.googleapis.com/auth/firebase.messaging - api: - url: https://fcm.googleapis.com/v1/projects/pium-test/messages:send management: endpoint: