-
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.
Merge pull request #302 from zipdabang/refactor/296
♻️ Refactor/296 thread 순서 체크 위한 log 추가(@async)
- Loading branch information
Showing
5 changed files
with
124 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package zipdabang.server.async; | ||
|
||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
|
||
@Configuration | ||
@EnableAsync | ||
public class AsyncConfig implements AsyncConfigurer { | ||
|
||
private int CORE_POOL_SIZE = 3; | ||
private int MAX_POOL_SIZE = 8; | ||
private int QUEUE_CAPACITY = 1000; | ||
|
||
@Bean(name = "recipeUploadExecutor") | ||
public Executor threadPoolTaskExecutor() { | ||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); | ||
|
||
taskExecutor.setCorePoolSize(CORE_POOL_SIZE); | ||
taskExecutor.setMaxPoolSize(MAX_POOL_SIZE); | ||
taskExecutor.setQueueCapacity(QUEUE_CAPACITY); | ||
taskExecutor.setThreadNamePrefix("Executor-"); | ||
|
||
taskExecutor.setTaskDecorator(new CustomDecorator()); | ||
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); | ||
|
||
return taskExecutor; | ||
} | ||
|
||
@Override | ||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { | ||
return AsyncConfigurer.super.getAsyncUncaughtExceptionHandler(); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/zipdabang/server/async/AsyncExceptionHandler.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,15 @@ | ||
package zipdabang.server.async; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
@Slf4j | ||
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler { | ||
|
||
@Override | ||
public void handleUncaughtException(Throwable ex, Method method, Object... params) { | ||
log.error(ex.getMessage(), ex); | ||
} | ||
} |
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 zipdabang.server.async; | ||
|
||
import org.springframework.core.task.TaskDecorator; | ||
import org.springframework.web.context.request.RequestAttributes; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
|
||
public class CustomDecorator implements TaskDecorator { | ||
@Override | ||
public Runnable decorate(Runnable runnable) { | ||
RequestAttributes attributes = RequestContextHolder.currentRequestAttributes(); | ||
|
||
return () ->{ | ||
try{ | ||
RequestContextHolder.setRequestAttributes(attributes); | ||
|
||
runnable.run(); | ||
} finally { | ||
RequestContextHolder.resetRequestAttributes(); | ||
} | ||
}; | ||
} | ||
} |
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