【问题标题】:spring restservice asynchronous logging functionalityspring restservice 异步日志记录功能
【发布时间】:2019-03-05 11:27:45
【问题描述】:

我在春季编写了运行良好的休息服务。

现在,我需要在向用户返回响应之前添加执行一些数据库事务。

此数据库事务独立于检索到的响应。

例如,

@PostMapping("login")
    public TransactionResponse loginAuthentication(@Valid @RequestBody LoginRequestBody loginRequest) {
        TransactionResponse transactionResponse = new TransactionResponse();
        try {
            transactionResponse = loginService.validateUser(loginRequest);

            //independent transaction needs to be executed in a separate thread
            loginSerice.addLoginLog(transactionResponse);

            //return below response without waiting to compelete above log transaction
            return transactionResponse; 
        }
        catch (Exception e) {
            return CommonUtils.setErrorResponse(transactionResponse, e);
        }

    }

我在 spring mvc link 中阅读了异步控制器。虽然控制器 在单独的线程中执行相应的功能,但我不想等待数据库事务完成。 从服务层得到响应后,应立即转发给用户。

任何建议!

Spring 版本是 4.3

【问题讨论】:

  • 看看spring的@Async注解

标签: spring asynchronous callable


【解决方案1】:

我发布这个答案是为了帮助有相同要求的开发人员(在单独的线程中执行 void 函数)。

由于我对多线程/异步环境没有经验,我想通过使用 spring 异步方法来保持简单。

所以,我首先创建了线程池

@Configuration
@EnableAsync
public class ThreadConfig {

    @Bean
    public TaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(4);
        executor.setMaxPoolSize(4);
        executor.setThreadNamePrefix("WEBAPP");
        executor.initialize();

        return executor;
    }
}

然后我创建了一个服务,它将在单独的线程中执行我的代码。

@Async
@Service
@Transactional
public class LoggingService {

    public void logintransaction() throws Exception{ 
        System.out.println("start login loggin");

        Thread.sleep(5000);

        System.out.println("exit");
    }

}

最后,我在控制器上调用了上述服务。 正如我所见,首先打印了Total Time Taken,然后打印了“start login loggin”。这意味着我的新方法在新线程中执行。

@Autowired
    private LoggingService loggingService;


    @PostMapping("login")
    public TransactionResponse loginAuthentication(@Valid @RequestBody LoginRequestBody loginRequest) {
        long startTime = System.currentTimeMillis();
        TransactionResponse transactionResponse = new TransactionResponse();
        try {
            transactionResponse = loginService.validateUser(loginRequest);

            //independent transaction needs to be executed in a separate thread
            //loginSerice.addLoginLog(transactionResponse);
            loggingService.logintransaction();

            //return below response without waiting to compelete above log transaction

            System.err.println("Total Time Taken=>"+(System.currentTimeMillis() - startTime));
            return transactionResponse; 
        }
        catch (Exception e) {
            return CommonUtils.setErrorResponse(transactionResponse, e);
        }

    }

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多