-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: async event
- Loading branch information
Showing
21 changed files
with
269 additions
and
52 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
jtoon-core/core-api/src/main/java/shop/jtoon/global/config/ApplicationEventConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package shop.jtoon.global.config; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.event.ApplicationEventMulticaster; | ||
import org.springframework.context.event.SimpleApplicationEventMulticaster; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
@Configuration | ||
public class ApplicationEventConfig { | ||
|
||
@Bean | ||
public ApplicationEventMulticaster applicationEventMulticaster() { | ||
SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster(); | ||
eventMulticaster.setTaskExecutor(asyncExecutor()); | ||
return eventMulticaster; | ||
} | ||
|
||
private Executor asyncExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(10); | ||
executor.setMaxPoolSize(10); | ||
executor.setQueueCapacity(10000); | ||
executor.setWaitForTasksToCompleteOnShutdown(true); | ||
executor.setAwaitTerminationSeconds(10); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
jtoon-core/core-api/src/main/java/shop/jtoon/global/util/AsyncEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package shop.jtoon.global.util; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.springframework.context.event.EventListener; | ||
import org.springframework.core.annotation.AliasFor; | ||
|
||
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@EventListener | ||
public @interface AsyncEventListener { | ||
Phase phase() default Phase.AFTER_COMMIT; | ||
|
||
boolean fallbackExecution() default false; | ||
|
||
@AliasFor( | ||
annotation = EventListener.class, | ||
attribute = "classes" | ||
) | ||
Class<?>[] value() default {}; | ||
|
||
@AliasFor( | ||
annotation = EventListener.class, | ||
attribute = "classes" | ||
) | ||
Class<?>[] classes() default {}; | ||
|
||
@AliasFor( | ||
annotation = EventListener.class, | ||
attribute = "condition" | ||
) | ||
String condition() default ""; | ||
|
||
@AliasFor( | ||
annotation = EventListener.class, | ||
attribute = "id" | ||
) | ||
String id() default ""; | ||
} |
12 changes: 12 additions & 0 deletions
12
jtoon-core/core-api/src/main/java/shop/jtoon/global/util/Phase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package shop.jtoon.global.util; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public enum Phase { | ||
BEFORE_COMMIT, | ||
AFTER_COMMIT, | ||
AFTER_ROLLBACK, | ||
AFTER_COMPLETION; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...re-api/src/main/java/shop/jtoon/webtoon/presentation/WebtoonImageUploadEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package shop.jtoon.webtoon.presentation; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import shop.jtoon.dto.ImageUploadEvent; | ||
import shop.jtoon.dto.MultiImageEvent; | ||
import shop.jtoon.global.util.AsyncEventListener; | ||
import shop.jtoon.webtoon.application.WebtoonClientService; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class WebtoonImageUploadEventListener { | ||
|
||
private final WebtoonClientService webtoonClientService; | ||
|
||
@AsyncEventListener | ||
public void uploadImage(ImageUploadEvent imageUploadEvent) { | ||
webtoonClientService.upload(imageUploadEvent); | ||
} | ||
|
||
@AsyncEventListener | ||
public void uploadMultiImages(MultiImageEvent multiImageEvent) { | ||
multiImageEvent.imageUploadEvents().stream() | ||
.parallel() | ||
.forEach(webtoonClientService::upload); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
jtoon-core/core-api/src/main/java/shop/jtoon/webtoon/request/MultiImagesReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package shop.jtoon.webtoon.request; | ||
|
||
import static shop.jtoon.common.ImageType.*; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import lombok.Builder; | ||
import shop.jtoon.dto.ImageUploadEvent; | ||
|
||
@Builder | ||
public record MultiImagesReq( | ||
List<MultipartFile> mainImages | ||
) { | ||
|
||
public List<ImageUploadEvent> toMultiImageEvent(CreateEpisodeReq request, String webtoonTitle) { | ||
return mainImages.stream() | ||
.map(mainImage -> request.toUploadImageDto(EPISODE_MAIN, webtoonTitle, mainImage).toImageUploadEvent()) | ||
.toList(); | ||
} | ||
} |
6 changes: 4 additions & 2 deletions
6
jtoon-core/core-api/src/main/java/shop/jtoon/webtoon/response/EpisodeInfoRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
package shop.jtoon.webtoon.response; | ||
|
||
import java.util.List; | ||
|
||
import lombok.Builder; | ||
import shop.jtoon.webtoon.domain.EpisodeMainInfo; | ||
import shop.jtoon.webtoon.entity.Episode; | ||
|
||
@Builder | ||
public record EpisodeInfoRes( | ||
String mainUrl | ||
List<String> mainUrls | ||
) { | ||
|
||
public static EpisodeInfoRes from(EpisodeMainInfo episode) { | ||
return EpisodeInfoRes.builder() | ||
.mainUrl(episode.mainUrl()) | ||
.mainUrls(episode.mainUrls()) | ||
.build(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
jtoon-core/core-domain/src/main/java/shop/jtoon/util/WebtoonUitls.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package shop.jtoon.util; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class WebtoonUitls { | ||
|
||
public static final String URL_SPLITER = ","; | ||
} |
8 changes: 6 additions & 2 deletions
8
jtoon-core/core-domain/src/main/java/shop/jtoon/webtoon/domain/EpisodeMainInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
package shop.jtoon.webtoon.domain; | ||
|
||
import static shop.jtoon.util.WebtoonUitls.*; | ||
|
||
import java.util.List; | ||
|
||
import lombok.Builder; | ||
import shop.jtoon.webtoon.entity.Episode; | ||
|
||
@Builder | ||
public record EpisodeMainInfo( | ||
String mainUrl | ||
List<String> mainUrls | ||
) { | ||
|
||
public static EpisodeMainInfo of(Episode episode) { | ||
return EpisodeMainInfo.builder() | ||
.mainUrl(episode.getMainUrl()) | ||
.mainUrls(List.of(episode.getMainUrl().split(URL_SPLITER))) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.