-
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: transactional outbox pattern
- Loading branch information
Showing
40 changed files
with
710 additions
and
124 deletions.
There are no files selected for viewing
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
74 changes: 74 additions & 0 deletions
74
jtoon-core/core-api/src/main/java/shop/jtoon/event/EventService.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,74 @@ | ||
package shop.jtoon.event; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import shop.jtoon.dto.ImagePublishData; | ||
import shop.jtoon.event.domain.ImagePublish; | ||
import shop.jtoon.event.service.EventDomainService; | ||
import shop.jtoon.service.EventRedisService; | ||
import shop.jtoon.webtoon.application.WebtoonClientService; | ||
import shop.jtoon.webtoon.request.ImageEvent; | ||
import shop.jtoon.webtoon.service.WebtoonDomainService; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class EventService { | ||
|
||
private final EventDomainService eventDomainService; | ||
private final WebtoonClientService webtoonClientService; | ||
private final EventRedisService eventRedisService; | ||
private final WebtoonDomainService webtoonDomainService; | ||
|
||
@Scheduled(cron = "0/10 * * * * *") | ||
@Transactional | ||
public void publish() { | ||
LocalDateTime now = LocalDateTime.now(); | ||
|
||
List<ImagePublish> publishes = eventDomainService.readRecentEvent(now).stream() | ||
.map(imagePublish -> { | ||
webtoonClientService.upload(ImageEvent.toImageEvent(imagePublish.getImagePayload()).toImageUpload()); | ||
imagePublish.updateStatus(); | ||
|
||
return imagePublish; | ||
}) | ||
.toList(); | ||
|
||
eventDomainService.update(publishes); | ||
} | ||
|
||
@Scheduled(cron = "0/10 * * * * *") | ||
@Transactional | ||
public void eventExecute() { | ||
List<Long> webtoonIds = new ArrayList<>(); | ||
List<ImagePublish> publishes = eventRedisService.consume().stream() | ||
.parallel() | ||
.map(imagePublishData -> updateEvent(imagePublishData, webtoonIds)) | ||
.filter(Objects::nonNull) | ||
.map(ImageEvent::toImagePublish) | ||
.toList(); | ||
|
||
eventDomainService.update(publishes); | ||
webtoonDomainService.updateWebtoonStatus(webtoonIds); | ||
} | ||
|
||
private ImageEvent updateEvent(ImagePublishData imagePublishData, List<Long> wetoonIds) { | ||
ImageEvent imageEvent = ImageEvent.toImageEvent(imagePublishData); | ||
|
||
try { | ||
webtoonClientService.upload(imageEvent.toImageUpload()); | ||
wetoonIds.add(imagePublishData.id()); | ||
|
||
return null; | ||
} catch (Exception e) { | ||
return imageEvent; | ||
} | ||
} | ||
} |
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
18 changes: 3 additions & 15 deletions
18
jtoon-core/core-api/src/main/java/shop/jtoon/webtoon/application/WebtoonClientService.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
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
61 changes: 61 additions & 0 deletions
61
jtoon-core/core-api/src/main/java/shop/jtoon/webtoon/request/ImageEvent.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,61 @@ | ||
package shop.jtoon.webtoon.request; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import lombok.Builder; | ||
import shop.jtoon.dto.ImagePublishData; | ||
import shop.jtoon.dto.ImageUpload; | ||
import shop.jtoon.event.domain.ImagePayload; | ||
import shop.jtoon.event.domain.ImagePublish; | ||
import shop.jtoon.event.entity.EventStatus; | ||
|
||
@Builder | ||
public record ImageEvent( | ||
String key, | ||
byte[] data | ||
) { | ||
|
||
public static ImageEvent toImageEvent(ImagePayload imagePayload) { | ||
return ImageEvent.builder() | ||
.key(imagePayload.key()) | ||
.data(imagePayload.data()) | ||
.build(); | ||
} | ||
|
||
public static ImageEvent toImageEvent(ImagePublishData imagePublishData) { | ||
return ImageEvent.builder() | ||
.key(imagePublishData.key()) | ||
.data(imagePublishData.data()) | ||
.build(); | ||
} | ||
|
||
public ImageUpload toImageUpload() { | ||
return ImageUpload.builder() | ||
.key(key) | ||
.data(data) | ||
.build(); | ||
} | ||
|
||
public ImagePayload toImagePayload() { | ||
return ImagePayload.builder() | ||
.key(key) | ||
.data(data) | ||
.build(); | ||
} | ||
|
||
public ImagePublish toImagePublish() { | ||
return ImagePublish.builder() | ||
.eventStatus(EventStatus.READY) | ||
.imagePayload(this.toImagePayload()) | ||
.publishDate(LocalDateTime.now()) | ||
.build(); | ||
} | ||
|
||
public ImagePublishData imagePublishData(Long id) { | ||
return ImagePublishData.builder() | ||
.id(id) | ||
.key(key) | ||
.data(data) | ||
.build(); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
.../java/shop/jtoon/dto/MultiImageEvent.java → ...toon/webtoon/request/MultiImageEvent.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,12 +1,12 @@ | ||
package shop.jtoon.dto; | ||
package shop.jtoon.webtoon.request; | ||
|
||
import java.util.List; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record MultiImageEvent( | ||
List<ImageUploadEvent> imageUploadEvents | ||
List<ImageEvent> imageEvents | ||
) { | ||
|
||
} |
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
Oops, something went wrong.