Giriş
Şu satırı dahil ederiz.
constructor
Şu satırı dahil ederiz.
import org.springframework.scheduling.annotation.AsyncResult;
org.springframework.scheduling.annotation.AsyncResult sınıfı Future ve  ListenableFuture arayüzülerini gerçekleştirir. Bence CompletableFuture sınıfı varken artık bu sınıfı kullanmaya gerek yok.
constructor
Örnek
Şöyle yaparız
@Service
public class AsyncService {
  @Async
  public Future<String> asyncInvokeReturnFuture(int i) {
    Future<String> future;
    try {
      Thread.sleep(1000 * 1);
      future = new AsyncResult<String>("success:" + i);
    } catch (InterruptedException e) {
      future = new AsyncResult<String>("error");
    }
    return future;
  }
}Örnek
Şöyle yaparız.
Şöyle yaparız.
Şöyle yaparız.
@Async
@Override
public Future<Response> doTasks(Student student) {
  Response response = new Response();
  //do Tasks
  return new AsyncResult<Response>(response);
}Şöyle yaparız.
@Service
@Scope("prototype")
public class PostMan2 implements PostMans2 {
 
  @Async
  public Future<String> deliverLetter(String message, int i) {
    ...
    String res = "result!";
    return new AsyncResult<String>(res);
  }
  ...
}
completable metodu
Şöyle yaparız.
Şöyle yaparız.
@Service
public class Aservice {
  @Async
  public CompletableFuture<Integer> getData() throws InterruptedException {
    Thread.sleep(1000 * 3); // sleep for 3 sec
    return new AsyncResult<>(2).completable(); // wrap integer 2
  }
} 
Hiç yorum yok:
Yorum Gönder