org.springframework.scheduling.annotation.EnableAsync

  • Post author:
  • Post category:其他


org.springframework.scheduling.annotation.EnableAsync


@Import(value={AsyncConfigurationSelector.class})

@Target(value={TYPE})

@Retention(value=RUNTIME)

@Documented

# capability: 才能,能力; 容量; 性能; 生产率;

# execution: 执行,执行命令; 成功,奏效,效果;

# asynchronous:异步的;

# Enables: 使能够,提供做…的权利[措施]( enable的第三人称单数 ); 使可能;

# functionality:(计算机或电子系统的)功能; 功能性; 设计目的; 实用;

Enables Spring’s asynchronous method execution capability, similar to functionality found in Spring’s <task:*> XML namespace.

# 启用Spring的异步方法执行功能,类似于Spring的&lt;任务:*&gt;XML命名空间中的功能。

To be used together with @Configuration classes as follows, enabling annotation-driven async processing for an entire Spring application context:

# 与@Configuration类一起使用,如下所示,为整个Spring应用程序上下文启用注释驱动的异步处理:

@Configuration

@EnableAsync

public class AppConfig {

}

# annotated:  注解,注释

# transparently:  明亮地,显然地,易觉察地;

MyAsyncBean is a user-defined type with one or more methods annotated with either Spring’s @Async annotation, the EJB 3.1 @javax.ejb.Asynchronous annotation, or any custom annotation specified via the annotation attribute. The aspect is added transparently for any registered bean, for instance via this configuration:  @Configuration

# 我的Async Bean是一个用户定义的类型,其中包含一个或多个带有Spring@Async注释的方法

# EJB3.1@javax.ejb.Asynchronous注释,或通过注释属性指定的任何自定义注释。

# 该切面是显示地添加到任何注册的bean,例如通过此配置:@Configuration

public class AnotherAppConfig {

@Bean

public MyAsyncBean asyncBean() {


return new MyAsyncBean();

}

}


By default, Spring will be searching for an associated thread pool definition: either a unique org.springframework.core.task.TaskExecutor bean in the context, or an java.util.concurrent.Executor bean named “taskExecutor” otherwise. If neither of the two is resolvable, a org.springframework.core.task.SimpleAsyncTaskExecutor will be used to process async method invocations. Besides, annotated methods having a void return type cannot transmit any exception back to the caller. By default, such uncaught exceptions are only logged.

#默认情况下,Spring将搜索关联的线程池定义

# 要么是一个独特的org.springframework.core。任务。 上下文中的Task Executor bean

# 否则,上下文中的Task Executor bean,或java.util.concurrent。 Executor bean命名为“task Executor”,

# 如果两者都不是可解决的,则org.springframework.core.任务。 简单异步任务执行程序将用于处理异步方法调用。

# 此外,具有void返回类型的注释方法不能将任何异常传输回调用方

# 默认情况下,这些未捕获的异常只被记录

#  customize:定制,定做; 按规格改制;

# 实现异步配置器并提供:

To customize all this, implement AsyncConfigurer and provide:

•your own Executor through the getAsyncExecutor() method, and

•your own AsyncUncaughtExceptionHandler through the getAsyncUncaughtExceptionHandler() method.

NOTE: AsyncConfigurer configuration classes get initialized early in the application context bootstrap. If you need any dependencies on other beans there, make sure to declare them ‘lazy’ as far as possible in order to let them go through other post-processors as well.

# 注意:异步配置类在应用程序上下文引导中早期初始化。

@Configuration

@EnableAsync

public class AppConfig implements AsyncConfigurer {

@Override

public Executor getAsyncExecutor() {


ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

executor.setCorePoolSize(7);

executor.setMaxPoolSize(42);

executor.setQueueCapacity(11);

executor.setThreadNamePrefix(“MyExecutor-“);

executor.initialize();

return executor;

}

@Override

public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {


return new MyAsyncUncaughtExceptionHandler();

}

}

If only one item needs to be customized, null can be returned to keep the default settings. Consider also extending from AsyncConfigurerSupport when possible.

Note: In the above example the ThreadPoolTaskExecutor is not a fully managed Spring bean. Add the @Bean annotation to the getAsyncExecutor() method if you want a fully managed bean. In such circumstances it is no longer necessary to manually call the executor.initialize() method as this will be invoked automatically when the bean is initialized.

#注意:在上面的示例中,Thread Pool Task Executor不是一个完全管理的Spring bean

#如果您想要一个完全管理的bean,请将@Bean注释添加到get Async Executor()方法中。

#在这种情况下,不再需要手动调用executor.initialize()方法,因为这将在bean初始化时自动调用。

For reference, the example above can be compared to the following Spring XML configuration:

<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>

# equivalent: 相等的,相当的,等效的; 等价的,等积的; [化学]当量的;

# demonstrates: 示范。展示; 显示; 论证;

The above XML-based and JavaConfig-based examples are equivalent except for the setting of the thread name prefix of the Executor; this is because the <task:executor> element does not expose such an attribute. This demonstrates how the JavaConfig-based approach allows for maximum configurability through direct access to actual componentry.

The mode attribute controls how advice is applied: If the mode is AdviceMode.PROXY (the default), then the other attributes control the behavior of the proxying. Please note that proxy mode allows for interception of calls through the proxy only; local calls within the same class cannot get intercepted that way.

#上述基于XML和基于javaconfig的示例是等价的,除了设置执行器的线程名称前缀之外;  这是因为<;任务:Executor>;元素没有公开这样的属性

#这演示了基于JavaConfig的方法如何通过直接访问实际组件来允许最大的可配置性

Note that if the mode is set to AdviceMode.ASPECTJ, then the value of the proxyTargetClass attribute will be ignored.

Note also that in this case the spring-aspects module JAR must be present on the classpath, with compile-time weaving or load-time weaving applying the aspect to the affected classes.

There is no proxy involved in such a scenario; local calls will be intercepted as well.

Since:3.1Author:Chris BeamsJuergen HoellerStephane NicollSam BrannenSee Also:AsyncAsyncConfigurerAsyncConfigurationSelector

# 注意,如果模式设置为AdviceMode.ASPECTJ,模式。 然后将忽略目标代理类属性的值。



版权声明:本文为CarloPan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。