maven 异步sprinbootKotlin不工作线程[main,5,main]

soat7uwm  于 12个月前  发布在  Maven
关注(0)|答案(1)|浏览(166)

所以我有我的代码使用marticlec,但它似乎不工作

package com.rifqi.jsnowball.service

import org.springframework.scheduling.annotation.Async
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component

@Component
class HelloAsync {
    private val log: Logger = LoggerFactory.getLogger(this.javaClass)
    @Async
    fun helloWorld (){
        Thread.sleep(2000)
        log.info("hello after 2 seconds {}",Thread.currentThread())
    }
}

字符串
我确实有一个blog配置,但它似乎没有做任何事情,当我添加它或不

package com.rifqi.jsnowball.configurations

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler
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.lang.reflect.Method
import java.util.concurrent.Executor

@EnableAsync
@Configuration
class AsyncConfig : AsyncConfigurer {
    private val log: Logger = LoggerFactory.getLogger(this.javaClass)
    override fun getAsyncExecutor(): Executor {
        val taskExecutor = ThreadPoolTaskExecutor()
        taskExecutor.corePoolSize = 5
        taskExecutor.maxPoolSize = 10
        taskExecutor.setQueueCapacity(25)
        taskExecutor.initialize()
        return taskExecutor
    }

    override fun getAsyncUncaughtExceptionHandler(): AsyncUncaughtExceptionHandler {
        return MyAsyncUncaughtExceptionHandler()
    }

    inner class MyAsyncUncaughtExceptionHandler : AsyncUncaughtExceptionHandler {
        override fun handleUncaughtException(
            throwable: Throwable, method: Method, vararg params: Any?) {
            log.error("Uncaught exception in asynchronous method", throwable)
        }
    }
}


这是我的helloaync测试

package com.rifqi.jsnowball

import com.rifqi.jsnowball.service.HelloAsync
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.scheduling.annotation.EnableAsync

@EnableAsync
class HelloAsyncTest(

) {
    @Autowired
    private lateinit var helloAsync: HelloAsync

    @Autowired
    val log: Logger = LoggerFactory.getLogger(this.javaClass)

    @Test
    fun hello() {

        helloAsync = HelloAsync()

        for (i in 0..9) {
            helloAsync.helloWorld()
        }
        log.info("After calling hello()")
        Thread.sleep(5000)
    }
}


输出不是我所期望的,仍然是同步的,我犯了什么错误?我很确定我所有的依赖关系和项目结构是17 java是正确的
这是我得到的输出

09:03:59.446 [main] INFO com.rifqi.jsnowball.service.HelloAsync -- hello after 2 seconds Thread[main,5,main]
09:04:01.455 [main] INFO com.rifqi.jsnowball.service.HelloAsync -- hello after 2 seconds Thread[main,5,main]
09:04:03.456 [main] INFO com.rifqi.jsnowball.service.HelloAsync -- hello after 2 seconds Thread[main,5,main]
09:04:05.461 [main] INFO com.rifqi.jsnowball.service.HelloAsync -- hello after 2 seconds Thread[main,5,main]
09:04:07.467 [main] INFO com.rifqi.jsnowball.service.HelloAsync -- hello after 2 seconds Thread[main,5,main]
09:04:09.469 [main] INFO com.rifqi.jsnowball.service.HelloAsync -- hello after 2 seconds Thread[main,5,main]
09:04:11.473 [main] INFO com.rifqi.jsnowball.service.HelloAsync -- hello after 2 seconds Thread[main,5,main]
09:04:13.474 [main] INFO com.rifqi.jsnowball.service.HelloAsync -- hello after 2 seconds Thread[main,5,main]
09:04:15.476 [main] INFO com.rifqi.jsnowball.service.HelloAsync -- hello after 2 seconds Thread[main,5,main]
09:04:17.477 [main] INFO com.rifqi.jsnowball.service.HelloAsync -- hello after 2 seconds Thread[main,5,main]
09:04:17.477 [main] INFO com.rifqi.jsnowball.HelloAsyncTest -- After calling hello()


谢谢你的帮助

9jyewag0

9jyewag01#

本守则所

01    @Autowired
02    private lateinit var helloAsync: HelloAsync
03
04    @Autowired
05    val log: Logger = LoggerFactory.getLogger(this.javaClass)
06
07    @Test
08    fun hello() {
09
10        helloAsync = HelloAsync()
11
12        for (i in 0..9) {
13            helloAsync.helloWorld()

字符串
您在(02)正确地要求Spring注入HelloAsync的示例(它是一个带有代理的代理),但随后您在第(10)行用一个新的(非代理的)示例覆盖了它!

相关问题