java 相当于@EnableAsync 的 Spring XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31401640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:34:17  来源:igfitidea点击:

Spring XML equivalent of @EnableAsync

javaspringasynchronous

提问by Monish Sen

Is there a way to turn on Spring's Async configuration from XML? All the examples I saw are using programmatic context declaration and use @EnableAsync

有没有办法从 XML 打开 Spring 的 Async 配置?我看到的所有示例都使用编程上下文声明并使用@EnableAsync

Is there an XML equivalent for this. In some places I saw <context:annotation-config />being used, but this doesn't mention anything about async .

是否有与此等效的 XML。在某些地方我看到有人<context:annotation-config />在使用,但这并没有提到任何关于 async 的内容。

I am using Spring 4.

我正在使用 Spring 4。

采纳答案by Pulkit

Did you try using this

你试过用这个吗

<task:annotation-driven /> 

回答by Micho

Yes, you can use something like this

是的,你可以使用这样的东西

 <beans>
     <task:annotation-driven executor="myExecutor" exception-handler="exceptionHandler"/>
     <task:executor id="myExecutor" pool-size="7-42" queue-capacity="11"/>
     <bean id="asyncBean" class="com.foo.MyAsyncBean"/>
     <bean id="exceptionHandler" class="com.foo.MyAsyncUncaughtExceptionHandler"/>
 </beans>

According to the Spring documentation, this is equivalent to using @EnableAsync

根据Spring 文档,这相当于使用@EnableAsync

回答by Nishant Mittal

In the annotation based approach you have to have @EnableAsync on the Configuration class. Something like as shown below:

在基于注释的方法中,您必须在 Configuration 类上使用 @EnableAsync。如下图所示:

@Configuration
@EnableAsync
@ComponentScan(basePackages ="com.spring.sample.demoAsync")
public class SpringAsyncConfig {

}

Then you create a component class to have a function that is called Asynchronously. Something like as shown below:

然后您创建一个组件类来拥有一个异步调用的函数。如下图所示:

@Component
public class AsyncClass {

    @Async
    public Future<String> asyncMethod() {
        System.out.println("Executing Thread Async:" +Thread.currentThread().getName());
        return new AsyncResult<String>(Thread.currentThread().getName());
    }
}

To have the xml equivalent of this approach, you can create a bean in the applicationContext.xml file as shown below:

要获得与此方法等效的 xml,您可以在 applicationContext.xml 文件中创建一个 bean,如下所示:

<bean id="AsyncClass" class="com.spring.sample.demoAsync.AsyncClass"/>

To call the function asyncMethod() in your flow, you can refer AsyncClass bean from any other bean or service. Below is something that I tried to stitch the flow:

要在您的流程中调用函数 asyncMethod(),您可以从任何其他 bean 或服务引用 AsyncClass bean。以下是我尝试缝合流程的内容:

<bean id="callingBean" class="comspring.sample.demoAsync.CallingBeanClass">
   <property name="AsyncClassBean" ref="AsyncClass"/>
</bean>

It's not necessary to follow this step but is an alternative approach.

没有必要遵循此步骤,而是一种替代方法。

In my applicationContext.xml file, I also imported the task schema by using:

在我的 applicationContext.xml 文件中,我还使用以下方法导入了任务架构:

xmlns:task="http://www.springframework.org/schema/task
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"

and then mentioning the executor as a task in the same file:

然后在同一个文件中将 executor 作为任务提及:

<task:executor id="myexecutor" pool-size="5"  />

Now my AsyncClass looks like this without @component annotation.

现在我的 AsyncClass 看起来像这样,没有 @component 注释。

public class AsyncClass {

    @Async("myexecutor")
    public Future<String> asyncMethod() {
        System.out.println("Executing Thread Async:" +Thread.currentThread().getName());
        return new AsyncResult<String>(Thread.currentThread().getName());
    }
}

and then finally invoking the asyncMethod() asynchronously from the CallingBeanClass.

然后最后从 CallingBeanClass 异步调用 asyncMethod()。