@Spring中带有@Async的EventListener

@Spring中带有@Async的EventListener,spring,asynchronous,event-handling,Spring,Asynchronous,Event Handling,尝试结合@async和@EventListener注释启用异步事件处理,但我仍然看到侦听器正在发布线程中运行 您可以在此处找到以下示例: @SpringBootApplication @EnableAsync class AsyncEventListenerExample { static final Logger logger = LoggerFactory.getLogger(AsyncEventListenerExample.class); @Bean TaskExecutor tas

尝试结合
@async
@EventListener
注释启用异步事件处理,但我仍然看到侦听器正在发布线程中运行

您可以在此处找到以下示例:

@SpringBootApplication
@EnableAsync
class AsyncEventListenerExample {

static final Logger logger = LoggerFactory.getLogger(AsyncEventListenerExample.class);

@Bean
TaskExecutor taskExecutor() {
    return new SimpleAsyncTaskExecutor();
}


static class MedicalRecordUpdatedEvent {

    private String id;

    public MedicalRecordUpdatedEvent(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "MedicalRecordUpdatedEvent{" +
                "id='" + id + '\'' +
                '}';
    }
}

@Component
static class Receiver {

    @EventListener
    void handleSync(MedicalRecordUpdatedEvent event) {
        logger.info("thread '{}' handling '{}' event", Thread.currentThread(), event);
    }

    @Async
    @EventListener
    void handleAsync(MedicalRecordUpdatedEvent event) {
        logger.info("thread '{}' handling '{}' event", Thread.currentThread(), event);
    }

}

@Component
static class Producer {

    private final ApplicationEventPublisher publisher;

    public Producer(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

    public void create(String id) {
        publisher.publishEvent(new MedicalRecordUpdatedEvent(id));
    }

    @Async
    public void asynMethod() {
        logger.info("running async method with thread '{}'", Thread.currentThread());
    }
}

}
我的测试用例是:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AsyncEventListenerExample.class)
public class AsyncEventListenerExampleTests {

@Autowired
Producer producer;

@Test
public void createEvent() throws InterruptedException {

    producer.create("foo");

    //producer.asynMethod();


    // A chance to see the logging messages before the JVM exists.
    Thread.sleep(2000);

}
}
但是在日志中,我看到两个
@EventListener
都在
主线程中运行

2016-05-12 08:52:43.184  INFO 18671 --- [           main] c.z.e.async2.AsyncEventListenerExample   : thread 'Thread[main,5,main]' handling 'MedicalRecordUpdatedEvent{id='foo'}' event
2016-05-12 08:52:43.186  INFO 18671 --- [           main] c.z.e.async2.AsyncEventListenerExample   : thread 'Thread[main,5,main]' handling 'MedicalRecordUpdatedEvent{id='foo'}' event
async
基础结构使用
@EnableAsync
和异步
任务执行器进行初始化

不知道我做错了什么。你能帮忙吗

谢谢


使用Spring Boot 1.4.2.M2,因此Spring 4.3.0.RC1

在Spring Framework 4.3.0.RC1中出现了一个回归,这导致了您遇到的问题。如果使用快照,您的项目运行良好。

onticketupdateEvent
在Spring Framework 4.2.4发行版的主线程中运行,如下所示。 但如果没有实现AsyncConfigurer,它将在SimpleAsyncTaskExecutor中运行

@EnableAsync(proxyTargetClass = true)
@Component
@Slf4j
public class ExampleEventListener implements AsyncConfigurer {

    @Async
    @EventListener
    public void onTicketUpdatedEvent(TicketEvent ticketEvent) {
        log.debug("received ticket updated event");
    }

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(100);
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}

我通过如下配置TaskExecutorbean解决了我的问题

@Bean(name = "threadPoolTaskExecutor")
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setMaxPoolSize(100);
    executor.initialize();
    return executor;
}

@Async("threadPoolTaskExecutor")
@EventListener
public void onTicketUpdatedEvent(TicketEvent ticketEvent) {
    log.debug("received ticket updated event");
}