From 8306efc2f77dcb5c052c564628daebdc2608bfc2 Mon Sep 17 00:00:00 2001 From: yeeun Date: Tue, 7 Nov 2023 14:38:17 +0900 Subject: [PATCH 01/12] =?UTF-8?q?feat=20:=20=EB=B9=84=EB=8F=99=EA=B8=B0=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 기본적으로 @Async 메서드에서 발생하는 예외는 호출자에게 전파가 되지 않음. - 이는 @Async 어노테이션이 붙은 메서드가 별도의 스레드에서 실행되므로 메인 스레드에서 캐치를 할 수 없기 때문 - 비동기 메서드에서 발생하는 예외를 처리하는 클래스인 AsyncExceptionHandler를 구현 - AsyncUncaughtExceptionHandler 인터페이스를 구현하였으며, handleUncaughtException 메서드를 통해 예외를 처리 --- .../core/exception/AsyncExceptionHandler.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/java/com/javajober/core/exception/AsyncExceptionHandler.java diff --git a/src/main/java/com/javajober/core/exception/AsyncExceptionHandler.java b/src/main/java/com/javajober/core/exception/AsyncExceptionHandler.java new file mode 100644 index 0000000..825aad4 --- /dev/null +++ b/src/main/java/com/javajober/core/exception/AsyncExceptionHandler.java @@ -0,0 +1,20 @@ +package com.javajober.core.exception; + +import java.lang.reflect.Method; + +import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler { + + @Override + public void handleUncaughtException(Throwable throwable, Method method, Object... obj) { + log.info("Exception message - " + throwable.getMessage()); + log.info("Method name - " + method.getName()); + for (Object param: obj) { + log.info("Parameter value - " + param); + } + } +} From 7f0a1273fa3bc97f4fa411572ec6f11ac213219a Mon Sep 17 00:00:00 2001 From: yeeun Date: Tue, 7 Nov 2023 14:41:51 +0900 Subject: [PATCH 02/12] =?UTF-8?q?feat=20:=20=EB=B9=84=EB=8F=99=EA=B8=B0=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 비동기 처리를 위한 설정 클래스인 AsyncConfig를 추가 - 'threadPoolTaskExecutor'라는 이름의 Executor 빈을 등록하는 메서드를 구현 - Executor는 비동기 작업을 처리할 때 사용될 스레드 풀을 설정하며, corePoolSize는 5, maxPoolSize는 30, queueCapacity는 50으로 설정 - AsyncConfigurerSupport의 getAsyncUncaughtExceptionHandler() 메서드를 오버라이드하여 비동기 메서드에서 발생하는 예외를 처리하는 클래스인 AsyncExceptionHandler를 반환하도록 함 --- .../javajober/core/config/AsyncConfig.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/main/java/com/javajober/core/config/AsyncConfig.java diff --git a/src/main/java/com/javajober/core/config/AsyncConfig.java b/src/main/java/com/javajober/core/config/AsyncConfig.java new file mode 100644 index 0000000..c5d1b39 --- /dev/null +++ b/src/main/java/com/javajober/core/config/AsyncConfig.java @@ -0,0 +1,33 @@ +package com.javajober.core.config; + +import java.util.concurrent.Executor; + +import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.AsyncConfigurerSupport; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import com.javajober.core.exception.AsyncExceptionHandler; + +@Configuration +@EnableAsync +public class AsyncConfig extends AsyncConfigurerSupport { + + @Bean(name="threadPoolTaskExecutor") + public Executor getAsyncExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(5); + executor.setMaxPoolSize(30); + executor.setQueueCapacity(50); + executor.setThreadNamePrefix("ASYNC-"); + executor.initialize(); + return executor; + } + + @Override + public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { + return new AsyncExceptionHandler(); + } +} From bea35ac2797959166ac34ed6ede5bf33e49316d4 Mon Sep 17 00:00:00 2001 From: yeeun Date: Tue, 7 Nov 2023 15:08:59 +0900 Subject: [PATCH 03/12] =?UTF-8?q?refactor=20:=20=EC=8A=A4=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=20=EC=83=9D=EC=84=B1=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=EC=9D=84=20SpaceService=20=ED=81=B4=EB=9E=98=EC=8A=A4=EB=A1=9C?= =?UTF-8?q?=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 유저의 개인 및 단체 스페이스를 초기화하고 저장하는 작업을 비동기적으로 처리 --- .../member/service/MemberService.java | 45 +++---------------- .../javajober/space/service/SpaceService.java | 45 ++++++++++++++++++- 2 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/javajober/member/service/MemberService.java b/src/main/java/com/javajober/member/service/MemberService.java index f0845cf..436c61e 100644 --- a/src/main/java/com/javajober/member/service/MemberService.java +++ b/src/main/java/com/javajober/member/service/MemberService.java @@ -1,9 +1,7 @@ package com.javajober.member.service; -import java.util.HashSet; import java.util.Optional; -import java.util.Set; import javax.transaction.Transactional; @@ -21,10 +19,9 @@ import com.javajober.core.refreshToken.repository.RefreshTokenRepository; import com.javajober.core.security.JwtTokenizer; import com.javajober.core.refreshToken.domain.RefreshToken; -import com.javajober.space.domain.AddSpace; -import com.javajober.space.domain.SpaceType; -import com.javajober.space.dto.request.SpaceSaveRequest; -import com.javajober.space.repository.AddSpaceRepository; +import com.javajober.space.service.SpaceService; + +import lombok.extern.slf4j.Slf4j; @Service public class MemberService { @@ -32,15 +29,15 @@ public class MemberService { private final PasswordEncoder passwordEncoder; private final JwtTokenizer jwtTokenizer; private final RefreshTokenRepository refreshTokenRepository; - private final AddSpaceRepository addSpaceRepository; + private final SpaceService spaceService; public MemberService(MemberRepository memberRepository, PasswordEncoder passwordEncoder, JwtTokenizer jwtTokenizer, - RefreshTokenRepository refreshTokenRepository, AddSpaceRepository addSpaceRepository) { + RefreshTokenRepository refreshTokenRepository, SpaceService spaceService) { this.memberRepository = memberRepository; this.passwordEncoder = passwordEncoder; this.jwtTokenizer = jwtTokenizer; this.refreshTokenRepository = refreshTokenRepository; - this.addSpaceRepository = addSpaceRepository; + this.spaceService = spaceService; } @Transactional @@ -55,39 +52,11 @@ public MemberSignupResponse signup(MemberSignupRequest memberSignupRequest) { member.setPassword(passwordEncoder.encode(memberSignupRequest.getPassword())); Member saveMember = memberRepository.save(member); - initializeAndSaveNewMemberSpaces(member); + spaceService.initializeAndSaveNewMemberSpaces(member); return new MemberSignupResponse(saveMember); } - private void initializeAndSaveNewMemberSpaces(Member member) { - - SpaceSaveRequest personalSpaceRequest = createSpaceSaveRequest(member.getMemberName(), SpaceType.PERSONAL.getEngTitle(), member.getMemberName()); - SpaceSaveRequest organizationSpaceRequest = createSpaceSaveRequest(member.getMemberName(), SpaceType.ORGANIZATION.getEngTitle(), "임시회사명"); - - Set spaces = new HashSet<>(); - - AddSpace personalSpace = SpaceSaveRequest.toEntity(personalSpaceRequest, member); - spaces.add(personalSpace); - - AddSpace organizationSpace = SpaceSaveRequest.toEntity(organizationSpaceRequest, member); - spaces.add(organizationSpace); - - saveSpaces(spaces); - } - - private SpaceSaveRequest createSpaceSaveRequest(String spaceTitle, String spaceType, String representativeName) { - return SpaceSaveRequest.builder() - .spaceTitle(spaceTitle) - .spaceType(spaceType) - .representativeName(representativeName) - .build(); - } - - private void saveSpaces(Set spaces) { - addSpaceRepository.saveAll(spaces); - } - @Transactional public MemberLoginResponse login(MemberLoginRequest loginDto) { Member member = memberRepository.findMember(loginDto.getEmail()).orElseThrow(() diff --git a/src/main/java/com/javajober/space/service/SpaceService.java b/src/main/java/com/javajober/space/service/SpaceService.java index db076d3..7db0a07 100644 --- a/src/main/java/com/javajober/space/service/SpaceService.java +++ b/src/main/java/com/javajober/space/service/SpaceService.java @@ -14,12 +14,21 @@ import com.javajober.space.dto.response.MemberGroupResponse; import com.javajober.spaceWall.domain.FlagType; import com.javajober.spaceWall.repository.SpaceWallRepository; + +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; +import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.Set; + +import javax.transaction.Transactional; + +import lombok.extern.slf4j.Slf4j; +@Slf4j @Service public class SpaceService { @@ -37,6 +46,41 @@ public SpaceService(final MemberRepository memberRepository, final MemberGroupRe this.spaceWallRepository = spaceWallRepository; } + @Async("threadPoolTaskExecutor") + public void initializeAndSaveNewMemberSpaces(Member member) { + + log.info("initializeAndSaveNewMemberSpaces 시작: {}, 스레드 이름: {}", member.getMemberName(), Thread.currentThread().getName()); + + SpaceSaveRequest personalSpaceRequest = createSpaceSaveRequest(member.getMemberName(), SpaceType.PERSONAL.getEngTitle(), member.getMemberName()); + SpaceSaveRequest organizationSpaceRequest = createSpaceSaveRequest(member.getMemberName(), SpaceType.ORGANIZATION.getEngTitle(), "임시회사명"); + + Set spaces = new HashSet<>(); + + AddSpace personalSpace = SpaceSaveRequest.toEntity(personalSpaceRequest, member); + spaces.add(personalSpace); + + AddSpace organizationSpace = SpaceSaveRequest.toEntity(organizationSpaceRequest, member); + spaces.add(organizationSpace); + + saveSpaces(spaces); + + log.info("initializeAndSaveNewMemberSpaces 종료: {}, 스레드 이름: {}", member.getMemberName(), Thread.currentThread().getName()); + } + + private SpaceSaveRequest createSpaceSaveRequest(String spaceTitle, String spaceType, String representativeName) { + return SpaceSaveRequest.builder() + .spaceTitle(spaceTitle) + .spaceType(spaceType) + .representativeName(representativeName) + .build(); + } + + @Transactional + public void saveSpaces(Set spaces) { + addSpaceRepository.saveAll(spaces); + } + + public SpaceSaveResponse save(SpaceSaveRequest request, Long memberId) { Member member = memberRepository.findMember(memberId); AddSpace space = SpaceSaveRequest.toEntity(request, member); @@ -64,5 +108,4 @@ private static void existsAddSpace(Long addSpaceId, List addSpaceIds) { throw new ApplicationException(ApiStatus.NOT_FOUND, "존재하지 않는 스페이스입니다."); } } - } \ No newline at end of file From f4606ef7ec5395855b21549bdd2ee6c15adf13c0 Mon Sep 17 00:00:00 2001 From: yeeun Date: Tue, 7 Nov 2023 15:09:28 +0900 Subject: [PATCH 04/12] =?UTF-8?q?style=20:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20import=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/javajober/member/service/MemberService.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/javajober/member/service/MemberService.java b/src/main/java/com/javajober/member/service/MemberService.java index 436c61e..b50c627 100644 --- a/src/main/java/com/javajober/member/service/MemberService.java +++ b/src/main/java/com/javajober/member/service/MemberService.java @@ -21,8 +21,6 @@ import com.javajober.core.refreshToken.domain.RefreshToken; import com.javajober.space.service.SpaceService; -import lombok.extern.slf4j.Slf4j; - @Service public class MemberService { private final MemberRepository memberRepository; From 6ce8b2c7198277ce4f1b55affcc939e43d016af8 Mon Sep 17 00:00:00 2001 From: yeeun Date: Tue, 7 Nov 2023 21:12:01 +0900 Subject: [PATCH 05/12] =?UTF-8?q?feat=20:=20MemberSignupEvent=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1=20(#228)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Event를 사용해 이벤트로 사용하는 클래스라는 것을 명시적으로 표현 - 이벤트에 필요한 정보를 담고 있으며, 이벤트가 발생했을 때 해당 정보를 사용 --- .../member/event/MemberSignupEvent.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/com/javajober/member/event/MemberSignupEvent.java diff --git a/src/main/java/com/javajober/member/event/MemberSignupEvent.java b/src/main/java/com/javajober/member/event/MemberSignupEvent.java new file mode 100644 index 0000000..5a0b45e --- /dev/null +++ b/src/main/java/com/javajober/member/event/MemberSignupEvent.java @@ -0,0 +1,23 @@ +package com.javajober.member.event; + +import com.javajober.member.domain.Member; +import com.javajober.space.dto.request.SpaceSaveRequest; + +import lombok.Getter; + +@Getter +public class MemberSignupEvent { + + private final Member member; + private final SpaceSaveRequest personalSpaceRequest; + private final SpaceSaveRequest organizationSpaceRequest; + + public MemberSignupEvent( + final Member member, final SpaceSaveRequest personalSpaceRequest, + final SpaceSaveRequest organizationSpaceRequest + ) { + this.member = member; + this.personalSpaceRequest = personalSpaceRequest; + this.organizationSpaceRequest = organizationSpaceRequest; + } +} From f32e4881e03ae77fdc8f4320e827184af236bf2b Mon Sep 17 00:00:00 2001 From: yeeun Date: Tue, 7 Nov 2023 21:58:47 +0900 Subject: [PATCH 06/12] =?UTF-8?q?refactor=20:=20MemberSignupEvent=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=ED=95=84=EB=93=9C=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0=20(#228)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/javajober/member/event/MemberSignupEvent.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/com/javajober/member/event/MemberSignupEvent.java b/src/main/java/com/javajober/member/event/MemberSignupEvent.java index 5a0b45e..7b80371 100644 --- a/src/main/java/com/javajober/member/event/MemberSignupEvent.java +++ b/src/main/java/com/javajober/member/event/MemberSignupEvent.java @@ -1,7 +1,6 @@ package com.javajober.member.event; import com.javajober.member.domain.Member; -import com.javajober.space.dto.request.SpaceSaveRequest; import lombok.Getter; @@ -9,15 +8,8 @@ public class MemberSignupEvent { private final Member member; - private final SpaceSaveRequest personalSpaceRequest; - private final SpaceSaveRequest organizationSpaceRequest; - public MemberSignupEvent( - final Member member, final SpaceSaveRequest personalSpaceRequest, - final SpaceSaveRequest organizationSpaceRequest - ) { + public MemberSignupEvent(final Member member) { this.member = member; - this.personalSpaceRequest = personalSpaceRequest; - this.organizationSpaceRequest = organizationSpaceRequest; } } From 5fcbfb67ee9ca5a189bbee70a470dd00fd0d76be Mon Sep 17 00:00:00 2001 From: yeeun Date: Tue, 7 Nov 2023 22:11:00 +0900 Subject: [PATCH 07/12] =?UTF-8?q?refactor=20:=20=ED=9A=8C=EC=9B=90=20?= =?UTF-8?q?=EA=B0=80=EC=9E=85=20=EC=8B=9C=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=94=94=EC=8A=A4=ED=8C=A8=EC=B2=98=EB=A5=BC=20=ED=86=B5?= =?UTF-8?q?=ED=95=9C=20=EC=8A=A4=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EC=B4=88?= =?UTF-8?q?=EA=B8=B0=ED=99=94=20=EB=A1=9C=EC=A7=81=20=EC=A0=81=EC=9A=A9=20?= =?UTF-8?q?(#228)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/service/MemberService.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/javajober/member/service/MemberService.java b/src/main/java/com/javajober/member/service/MemberService.java index b50c627..d0ddab5 100644 --- a/src/main/java/com/javajober/member/service/MemberService.java +++ b/src/main/java/com/javajober/member/service/MemberService.java @@ -5,6 +5,7 @@ import javax.transaction.Transactional; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; @@ -15,11 +16,11 @@ import com.javajober.member.dto.MemberLoginResponse; import com.javajober.member.dto.MemberSignupRequest; import com.javajober.member.dto.MemberSignupResponse; +import com.javajober.member.event.MemberSignupEvent; import com.javajober.member.repository.MemberRepository; import com.javajober.core.refreshToken.repository.RefreshTokenRepository; import com.javajober.core.security.JwtTokenizer; import com.javajober.core.refreshToken.domain.RefreshToken; -import com.javajober.space.service.SpaceService; @Service public class MemberService { @@ -27,19 +28,25 @@ public class MemberService { private final PasswordEncoder passwordEncoder; private final JwtTokenizer jwtTokenizer; private final RefreshTokenRepository refreshTokenRepository; - private final SpaceService spaceService; - public MemberService(MemberRepository memberRepository, PasswordEncoder passwordEncoder, JwtTokenizer jwtTokenizer, - RefreshTokenRepository refreshTokenRepository, SpaceService spaceService) { + private final ApplicationEventPublisher eventPublisher; + + public MemberService( + final MemberRepository memberRepository, + final PasswordEncoder passwordEncoder, + final JwtTokenizer jwtTokenizer, + final RefreshTokenRepository refreshTokenRepository, + final ApplicationEventPublisher eventPublisher + ) { this.memberRepository = memberRepository; this.passwordEncoder = passwordEncoder; this.jwtTokenizer = jwtTokenizer; this.refreshTokenRepository = refreshTokenRepository; - this.spaceService = spaceService; + this.eventPublisher = eventPublisher; } @Transactional - public MemberSignupResponse signup(MemberSignupRequest memberSignupRequest) { + public MemberSignupResponse signup(final MemberSignupRequest memberSignupRequest) { Optional existingMember = memberRepository.findMember(memberSignupRequest.getEmail()); if (existingMember.isPresent()) { @@ -50,13 +57,13 @@ public MemberSignupResponse signup(MemberSignupRequest memberSignupRequest) { member.setPassword(passwordEncoder.encode(memberSignupRequest.getPassword())); Member saveMember = memberRepository.save(member); - spaceService.initializeAndSaveNewMemberSpaces(member); + eventPublisher.publishEvent(new MemberSignupEvent(member)); return new MemberSignupResponse(saveMember); } @Transactional - public MemberLoginResponse login(MemberLoginRequest loginDto) { + public MemberLoginResponse login(final MemberLoginRequest loginDto) { Member member = memberRepository.findMember(loginDto.getEmail()).orElseThrow(() -> new ApplicationException(ApiStatus.NOT_FOUND, "존재하지 않는 회원 아이디입니다.")); From 266ef8ff0c15056ef6774741228dc9af3466a8ca Mon Sep 17 00:00:00 2001 From: yeeun Date: Tue, 7 Nov 2023 22:17:22 +0900 Subject: [PATCH 08/12] =?UTF-8?q?feat=20:=20=ED=9A=8C=EC=9B=90=20=EA=B0=80?= =?UTF-8?q?=EC=9E=85=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=ED=95=B8=EB=93=A4?= =?UTF-8?q?=EB=9F=AC=20=EA=B5=AC=ED=98=84=20(#228)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/MemberSignupEventHandler.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/com/javajober/member/event/MemberSignupEventHandler.java diff --git a/src/main/java/com/javajober/member/event/MemberSignupEventHandler.java b/src/main/java/com/javajober/member/event/MemberSignupEventHandler.java new file mode 100644 index 0000000..e14d338 --- /dev/null +++ b/src/main/java/com/javajober/member/event/MemberSignupEventHandler.java @@ -0,0 +1,23 @@ +package com.javajober.member.event; + +import org.springframework.context.event.EventListener; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import com.javajober.space.service.SpaceService; + +@Component +public class MemberSignupEventHandler { + + private final SpaceService spaceService; + + public MemberSignupEventHandler(final SpaceService spaceService) { + this.spaceService = spaceService; + } + + @EventListener(MemberSignupEvent.class) + @Async("threadPoolTaskExecutor") + public void handleMemberSignupEvent(final MemberSignupEvent event) { + spaceService.initializeAndSaveNewMemberSpaces(event.getMember()); + } +} From e7703b37eb9cd92f8ef99bf7965e64880e65aa6f Mon Sep 17 00:00:00 2001 From: yeeun Date: Tue, 7 Nov 2023 22:20:04 +0900 Subject: [PATCH 09/12] =?UTF-8?q?refactor=20:=20=EB=B9=84=EB=8F=99?= =?UTF-8?q?=EA=B8=B0=20=EC=B2=98=EB=A6=AC=20=EB=A1=9C=EC=A7=81=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0=20=EB=B0=8F=20=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=A6=AC?= =?UTF-8?q?=20(#228)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../javajober/space/service/SpaceService.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/javajober/space/service/SpaceService.java b/src/main/java/com/javajober/space/service/SpaceService.java index 7db0a07..ad518da 100644 --- a/src/main/java/com/javajober/space/service/SpaceService.java +++ b/src/main/java/com/javajober/space/service/SpaceService.java @@ -15,7 +15,6 @@ import com.javajober.spaceWall.domain.FlagType; import com.javajober.spaceWall.repository.SpaceWallRepository; -import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.util.HashSet; @@ -37,17 +36,20 @@ public class SpaceService { private final AddSpaceRepository addSpaceRepository; private final SpaceWallRepository spaceWallRepository; - public SpaceService(final MemberRepository memberRepository, final MemberGroupRepository memberGroupRepository, - final AddSpaceRepository addSpaceRepository, final SpaceWallRepository spaceWallRepository) { - + public SpaceService( + final MemberRepository memberRepository, + final MemberGroupRepository memberGroupRepository, + final AddSpaceRepository addSpaceRepository, + final SpaceWallRepository spaceWallRepository + ) { this.memberRepository = memberRepository; this.memberGroupRepository = memberGroupRepository; this.addSpaceRepository = addSpaceRepository; this.spaceWallRepository = spaceWallRepository; } - @Async("threadPoolTaskExecutor") - public void initializeAndSaveNewMemberSpaces(Member member) { + + public void initializeAndSaveNewMemberSpaces(final Member member) { log.info("initializeAndSaveNewMemberSpaces 시작: {}, 스레드 이름: {}", member.getMemberName(), Thread.currentThread().getName()); @@ -67,7 +69,7 @@ public void initializeAndSaveNewMemberSpaces(Member member) { log.info("initializeAndSaveNewMemberSpaces 종료: {}, 스레드 이름: {}", member.getMemberName(), Thread.currentThread().getName()); } - private SpaceSaveRequest createSpaceSaveRequest(String spaceTitle, String spaceType, String representativeName) { + private SpaceSaveRequest createSpaceSaveRequest(final String spaceTitle, final String spaceType, final String representativeName) { return SpaceSaveRequest.builder() .spaceTitle(spaceTitle) .spaceType(spaceType) @@ -76,12 +78,12 @@ private SpaceSaveRequest createSpaceSaveRequest(String spaceTitle, String spaceT } @Transactional - public void saveSpaces(Set spaces) { + public void saveSpaces(final Set spaces) { addSpaceRepository.saveAll(spaces); } - public SpaceSaveResponse save(SpaceSaveRequest request, Long memberId) { + public SpaceSaveResponse save(final SpaceSaveRequest request, final Long memberId) { Member member = memberRepository.findMember(memberId); AddSpace space = SpaceSaveRequest.toEntity(request, member); Long spaceId = addSpaceRepository.save(space).getId(); @@ -89,7 +91,7 @@ public SpaceSaveResponse save(SpaceSaveRequest request, Long memberId) { return new SpaceSaveResponse(spaceId); } - public SpaceResponse find(Long addSpaceId, String spaceTypeString, Long memberId) { + public SpaceResponse find(final Long addSpaceId, final String spaceTypeString, final Long memberId) { SpaceType spaceType = SpaceType.findSpaceTypeByString(spaceTypeString); List addSpaceIds = addSpaceRepository.findAddSpaceIds(spaceType, memberId); existsAddSpace(addSpaceId, addSpaceIds); @@ -102,7 +104,7 @@ public SpaceResponse find(Long addSpaceId, String spaceTypeString, Long memberId .orElseGet(() -> new SpaceResponse(false, 0L, memberGroupResponses)); } - private static void existsAddSpace(Long addSpaceId, List addSpaceIds) { + private static void existsAddSpace(final Long addSpaceId, final List addSpaceIds) { boolean containsId = addSpaceIds.stream().anyMatch(id -> Objects.equals(id, addSpaceId)); if(!containsId) { throw new ApplicationException(ApiStatus.NOT_FOUND, "존재하지 않는 스페이스입니다."); From 7622eb1e7ccbce26ac56a5ab4ac9bb5feff3894c Mon Sep 17 00:00:00 2001 From: yeeun Date: Wed, 8 Nov 2023 01:31:28 +0900 Subject: [PATCH 10/12] =?UTF-8?q?refactor=20:=20TransactionalEventListener?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20(#228)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 트랜잭션이 성공할 때만 이벤트 핸들러를 실행되도록 TransactionalEventListener 사용 --- .../com/javajober/member/event/MemberSignupEventHandler.java | 5 +++-- .../java/com/javajober/member/service/MemberService.java | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/javajober/member/event/MemberSignupEventHandler.java b/src/main/java/com/javajober/member/event/MemberSignupEventHandler.java index e14d338..e4049c2 100644 --- a/src/main/java/com/javajober/member/event/MemberSignupEventHandler.java +++ b/src/main/java/com/javajober/member/event/MemberSignupEventHandler.java @@ -1,8 +1,9 @@ package com.javajober.member.event; -import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; +import org.springframework.transaction.event.TransactionPhase; +import org.springframework.transaction.event.TransactionalEventListener; import com.javajober.space.service.SpaceService; @@ -15,7 +16,7 @@ public MemberSignupEventHandler(final SpaceService spaceService) { this.spaceService = spaceService; } - @EventListener(MemberSignupEvent.class) + @TransactionalEventListener(classes = MemberSignupEvent.class, phase = TransactionPhase.AFTER_COMMIT) @Async("threadPoolTaskExecutor") public void handleMemberSignupEvent(final MemberSignupEvent event) { spaceService.initializeAndSaveNewMemberSpaces(event.getMember()); diff --git a/src/main/java/com/javajober/member/service/MemberService.java b/src/main/java/com/javajober/member/service/MemberService.java index d0ddab5..0cceafd 100644 --- a/src/main/java/com/javajober/member/service/MemberService.java +++ b/src/main/java/com/javajober/member/service/MemberService.java @@ -28,7 +28,6 @@ public class MemberService { private final PasswordEncoder passwordEncoder; private final JwtTokenizer jwtTokenizer; private final RefreshTokenRepository refreshTokenRepository; - private final ApplicationEventPublisher eventPublisher; public MemberService( From b07d7dca724b9f8255d543512bbb59381fa879b7 Mon Sep 17 00:00:00 2001 From: yeeun Date: Fri, 10 Nov 2023 01:00:22 +0900 Subject: [PATCH 11/12] =?UTF-8?q?refactor=20:=20TransactionalEventListener?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20(#228)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 스프링 트랜잭션 성공에 따라 이벤트 핸들러 실행되도록 변경 --- .../com/javajober/member/event/MemberSignupEventHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/javajober/member/event/MemberSignupEventHandler.java b/src/main/java/com/javajober/member/event/MemberSignupEventHandler.java index e4049c2..b1a863e 100644 --- a/src/main/java/com/javajober/member/event/MemberSignupEventHandler.java +++ b/src/main/java/com/javajober/member/event/MemberSignupEventHandler.java @@ -16,8 +16,8 @@ public MemberSignupEventHandler(final SpaceService spaceService) { this.spaceService = spaceService; } - @TransactionalEventListener(classes = MemberSignupEvent.class, phase = TransactionPhase.AFTER_COMMIT) @Async("threadPoolTaskExecutor") + @TransactionalEventListener(classes = MemberSignupEvent.class, phase = TransactionPhase.AFTER_COMMIT) public void handleMemberSignupEvent(final MemberSignupEvent event) { spaceService.initializeAndSaveNewMemberSpaces(event.getMember()); } From b95ab189d476cf92e093f3299dcf2378366f2f47 Mon Sep 17 00:00:00 2001 From: yeeun Date: Fri, 10 Nov 2023 01:01:21 +0900 Subject: [PATCH 12/12] =?UTF-8?q?style=20:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EA=B3=B5=EB=B0=B1=20=EC=A0=9C=EA=B1=B0=20(#228)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/javajober/member/service/MemberService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/javajober/member/service/MemberService.java b/src/main/java/com/javajober/member/service/MemberService.java index 0cceafd..ebe7714 100644 --- a/src/main/java/com/javajober/member/service/MemberService.java +++ b/src/main/java/com/javajober/member/service/MemberService.java @@ -1,6 +1,5 @@ package com.javajober.member.service; - import java.util.Optional; import javax.transaction.Transactional;