java - @Async 不适合我

标签 java spring

我正在使用@Scheduled,它工作正常,但无法让@Async 工作。我测试了很多次,似乎它使我的方法异步。我还缺少其他任何东西、配置或参数吗?我有一个类有两个方法,一个是用@Scheduled 标记的方法,执行并调用第二个用@Async 标记的方法。

这是我的配置:

<!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="com.socialmeety" />
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<task:annotation-driven/>

<!-- Configures support for @Controllers -->
<mvc:annotation-driven />

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<dwr:configuration />
<dwr:annotation-config />
<dwr:url-mapping />
<dwr:controller id="dwrController" debug="true" />

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

谢谢。

最佳答案

当您从同一对象中的另一个方法调用 @Async 方法时,您可能会绕过异步代理代码,而只是调用您的普通方法,即在同一个线程中。

解决此问题的一种方法是确保您对 @Async 方法的调用来自另一个对象。请参阅本文末尾的评论: http://groovyjavathoughts.blogspot.com/2010/01/asynchronous-code-with-spring-3-simple.html

但是这样做会很麻烦,所以您可以 Autowiring TaskScheduler,将您的方法包装在 Runnable 中并自己执行。

关于java - @Async 不适合我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4060718/

相关文章:

java - 我如何将其转换为 for 循环?

java - cordova cli 更新后无法添加 cordova 插件

java - 如何修复gradle项目中的重复依赖项?

java - 创建名称为 : Lookup method resolution failed; nested exception is IllegalStateException: Failed to introspect Class from ClassLoader 的 bean 时出错

java - 将自己的应用程序添加到 Whatsapp 音频选择中

java - 从一个本地钱包到交换钱包的 Ripple 交易(示例 : crex24) gives error code tecDST_TAG_NEEDED using java

spring mongodb聚合求和计数

java - CXF、Camel 和 Spring 线程阻塞问题

java - 如何等待QueueChannel处理所有消息?

java - 无法提取结果集