我在Spring中异步调用方法时遇到麻烦,此时调用程序是一个从外部系统接收通知的嵌入式库.代码如下所示:
@Service
public class DefaultNotificationProcessor implements NotificationProcessor {
private NotificationClient client;
@Override
public void process(Notification notification) {
processAsync(notification);
}
@PostConstruct
public void startClient() {
client = new NotificationClient(this, clientPort);
client.start();
}
@PreDestroy
public void stopClient() {
client.stop();
}
@Async
private void processAsync(Notification notification) {
// Heavy processing
}
}
Run Code Online (Sandbox Code Playgroud)
的NotificationClient内部具有在它接收到来自另一系统的通知的线程.它接受一个NotificationProcessor构造函数,它基本上是将对通知进行实际处理的对象.
在上面的代码中,我将Spring bean作为处理器,并尝试使用@Async注释异步处理通知.但是,似乎通知在与使用的通道相同的线程中处理NotificationClient.实际上,@Async被忽略了.
我在这里错过了什么?
我正在尝试在我的Service类中实现并发方法调用.
我的服务类中有一些注释为@Async的方法,我试图同时调用所有这些方法.但这些方法是按顺序执行的.
这是我的服务类(虚拟):
@Service public class TestService {
public SomeDataType getSOmeDataType() {
try {
List<DataType> a = retrieveDataA().get();
List<DataType> b = retrieveDataB().get();
List<DataType> c = retrieveDataC().get();
List<DataType> d = retrieveDataD().get();
List<DataType> e = retrieveDataE().get();
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (ExecutionException e) {
e.printStackTrace();
}
return referralDetailsReferenceData;
}
@Async
private Future<List<DataType>> retrieveDataA() {
//method logic
}
@Async
private Future<List<DataType>> retrieveDataB() {
//method logic
}
@Async
private Future<List<DataType>> retrieveDataC() {
//method logic
}
@Async
private Future<List<DataType>> retrieveDataD() {
//method …Run Code Online (Sandbox Code Playgroud)