1. 비동기 설정. ( SpringAsyncConfig.java )
package com.etoos.cmn.spring; import java.util.concurrent.Executor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import com.etoos.cmn.util.ConfigUtil; @Configuration @EnableAsync public class SpringAsyncConfig { @Bean(name = "apiLogExecutor") public Executor apiLogExecutor() { int corePoolSize = ConfigUtil.getInt("apilog.thread.pool.core-size"); int maxPoolSize = ConfigUtil.getInt("apilog.thread.pool.max-size"); int queueSize = ConfigUtil.getInt("apilog.thread.queue.size"); ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(corePoolSize); taskExecutor.setMaxPoolSize(maxPoolSize); taskExecutor.setQueueCapacity(queueSize); taskExecutor.setThreadNamePrefix("apiLogExecutor-"); taskExecutor.initialize(); return taskExecutor; } }
2. 비동기 서비스에 thread executor 지정.
package com.etoos.api.common.service; import org.apache.ibatis.session.SqlSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.etoos.api.common.vo.ApiLogVO; @Service public class ApiLogService { @Autowired @Qualifier("apiLogSqlSession") private SqlSession apiLogSqlSession; /** * API Log 저장 * * @param apiLogInfo API Log 정보 */ @Async("apiLogExecutor") @Transactional(propagation = Propagation.REQUIRES_NEW) public void insertApiLog(ApiLogVO apiLogInfo) { apiLogSqlSession.insert("apiLog.insertApiLog", apiLogInfo); } }