Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions backend/src/main/java/com/moong/config/AsyncConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ public Executor emalEventExecutor() {
return executor;
}

@Bean(name = "createAIMedicalAdviceEventExecutor")
public Executor createAIMedicalAdviceEventExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(4);
executor.setQueueCapacity(100);
executor.setRejectedExecutionHandler((r, ex) -> {
log.warn("createAIMedicalAdviceEventExecutor rejected task. poolSize={}, active={}, queued={}",
ex.getPoolSize(), ex.getActiveCount(), ex.getQueue().size());
});
executor.initialize();
return executor;
}

@Bean(name = "rankingRebuildExecutor")
public Executor rankingRebuildExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.moong.service.medicaladvice.PetMedicalAdviceService;
import com.moong.service.pet.PetService;
import com.moong.service.petgroup.PetGroupService;
import com.moong.util.transaction.TransactionAfterCommit;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

Expand All @@ -19,11 +21,21 @@ public class PetGroupFacadeService {
private final PetGroupService petGroupService;
private final PetService petService;
private final PetMedicalAdviceService petMedicalAdviceService;
private final TransactionAfterCommit transactionAfterCommit;

@Transactional
public PetCreateResponse firstJoin(Member member, PetCreateRequest petCreateRequest){
PetCreateResponse response = petService.createPet(member, petCreateRequest);
PetGroup savedPetGroup = petGroupService.firstJoin(member, response.petId());
petMedicalAdviceService.createMedicalAdvice(member.getId(), savedPetGroup.getId(), LocalDate.now());

transactionAfterCommit.run(() ->
petMedicalAdviceService.createMedicalAdvice(
member.getId(),
savedPetGroup.getId(),
LocalDate.now()
)
);

return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void upsertAllMedicalAdvice(LocalDate date) {
}
}

@Async("groupEventPublisherExecutor")
@Async("createAIMedicalAdviceEventExecutor")
public void createMedicalAdvice(long memberId, long groupId, LocalDate date) {
Year nextYear = Year.from(date).plusYears(1);
AiMedicalAdviceRequest input = getAiMedicalAdviceInput(groupId, date);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.moong.util.transaction;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

@Component
public class TransactionAfterCommit {

@Transactional
public void run(Runnable action) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
action.run();
}
});
}
}