0
我正在开发一个使用Spring 4.1.6和Mongodb的应用程序。我想在fire and forget模式中执行一些任务,例如一旦访问了一个方法,将会创建一个集合中的一个条目。我不想等到收集完成或者如果失败,我也不需要任何通知。如何使用Spring实现这一点。春季背景/火与遗忘处理
我正在开发一个使用Spring 4.1.6和Mongodb的应用程序。我想在fire and forget模式中执行一些任务,例如一旦访问了一个方法,将会创建一个集合中的一个条目。我不想等到收集完成或者如果失败,我也不需要任何通知。如何使用Spring实现这一点。春季背景/火与遗忘处理
你可以做到这一点,没有春天,但与春天我建议使用@Async。
首先你需要启用它。这样做对配置类:
@Configuration
@EnableAsync
public class AppConfig {
}
然后在bean中使用@Async你想得太执行异步
@Component
public class MyComponent {
@Async
void doSomething() {
// this will be executed asynchronously
}
}
你的方法可以有参数的方法:
@Component
public class MyComponent {
@Async
void doSomething(String s, int i, long l, Object o) {
// this will be executed asynchronously
}
}
在你的情况下,你不需要它,但方法可以返回一个未来:
@Component
public class MyComponent {
@Async
Future<String> doSomething(String s, int i, long l, Object o) {
// this will be executed asynchronously
return new AsyncResult<>("result");
}
}
我尝试过使用异步,但执行并不火并且忘记。它等待Async方法完成执行。我想要火,忘记执行的类型。 – Debopam
@Debopam,它应该使用'@ Async',你正确使用它吗?我们可以在问题中看到一些代码吗? – ESala
对不起,我错过了配置中的@EnableAsync。它正在工作。 – Debopam