如何使用Spring配置异步和同步事件发布

如何使用Spring配置异步和同步事件发布

本文介绍了如何使用Spring配置异步和同步事件发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用弹簧events.I事件框架才知道,春天的事件框架的默认行为是同步。但是,如果它发现Spring上下文初始化过程中与ID applicationEventMulticaster一个bean,它的行为异步。

I am trying to implement an event framework using spring events.I came to know that the default behavior of spring event framework is sync. But during spring context initialization if it finds a bean with id applicationEventMulticaster it behaves Async.

现在我想有两个同步和异步事件发布在我的应用程序,因为一些事件需要被同步公布。我试图用多路广播配置SysncTaskExecutor sync事件,但我不能找到一种方法,它注入到我的AsyncEventPublisher的applicationEventPublisher财产。
我的Spring配置文件是如下

Now i want to have both sync and async event publishers in my application, because some of the events needs to be published sync. I tried to configure sync event multicaster using SysncTaskExecutor, but i cant find a way to inject it into my AsyncEventPublisher's applicationEventPublisher property.My spring configuration file is as below

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" destroy-method="shutdown">
        <property name="corePoolSize" value="5" />
        <property name="maxPoolSize" value="10" />
        <property name="WaitForTasksToCompleteOnShutdown" value="true" />
    </bean>

    <bean id="syncTaskExecutor" class="org.springframework.core.task.SyncTaskExecutor" />

    <bean id="customEventPublisher" class="x.spring.event.CustomEventPublisher" />
    <bean id="customEventHandler" class="x.spring.event.CustomEventHandler" />
    <bean id="eventSource" class="x.spring.event.EventSource" />
    <bean id="responseHandler" class="x.spring.event.ResponseHandler" />
    <bean id="syncEventSource" class="x.spring.event.syncEventSource" />


    <bean id="applicationEventMulticaster" class="org.springframework.context.event.SimpleApplicationEventMulticaster">
        <property name="taskExecutor" ref="taskExecutor" />
    </bean>

    <bean id="syncApplicationEventMulticaster" class="org.springframework.context.event.SimpleApplicationEventMulticaster">
        <property name="taskExecutor" ref="syncTaskExecutor" />
    </bean>

谁能帮助我在这里?

Can anyone help me out here ?

推荐答案

不,你不能这样做,弹簧initApplicationEventMulticaster只是初始化只有一个,而BeanName必须applicationEventMulticaster。所以你只可以选择下面的执行人之一:

no, you can't do that, the spring initApplicationEventMulticaster just init only one, and the BeanName must be applicationEventMulticaster. so you just can choose one of below Executor:

什么办法,你可以修改org.springframework.context.event.SimpleApplicationEventMulticaster
加你的逻辑,那么你可以控制是否需要同步/异步

any way, you can modify org.springframework.context.event.SimpleApplicationEventMulticasterto add your logic, then you can control whether need to Sync/Async

    /**
 * Initialize the ApplicationEventMulticaster.
 * Uses SimpleApplicationEventMulticaster if none defined in the context.
 * @see org.springframework.context.event.SimpleApplicationEventMulticaster
 */
protected void initApplicationEventMulticaster() {
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
        this.applicationEventMulticaster =
                beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
        if (logger.isDebugEnabled()) {
            logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
        }
    }
    else {
        this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
        beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
        if (logger.isDebugEnabled()) {
            logger.debug("Unable to locate ApplicationEventMulticaster with name '" +
                    APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
                    "': using default [" + this.applicationEventMulticaster + "]");
        }
    }
}

这篇关于如何使用Spring配置异步和同步事件发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:21