所以我的代码使用异步,但似乎不起作用
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())
}
}
我确实有异步配置,但当我添加或不添加它时,它似乎没有做任何事情
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()
感谢您的帮助
在这段代码中
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) 行用新的(非代理)实例覆盖了它!