Skip to content

Commit

Permalink
Updated based on comments from jmle
Browse files Browse the repository at this point in the history
  • Loading branch information
vbalasub committed Dec 4, 2024
1 parent 94a7b41 commit bf7e61d
Showing 1 changed file with 66 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,50 +42,92 @@
url: https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.0-Release-Notes#core-container

- ruleID: spring-framework-5.x-to-6.0-core-container-00020
category: potential
effort: 3
category: optional
effort: 2
labels:
- konveyor.io/source=spring5
- konveyor.io/target=spring6+
when:
java.referenced:
pattern: org.springframework.util.concurrent.ListenableFuture*
location: IMPORT
description: ListenableFuture and related types are deprecated since Spring Framework 6.0.
description: ListenableFuture and related types are deprecated since Spring Framework 6.0
message: |
Spring Framework 6.0 has deprecated support for ListenableFuture and related types (ListenableFutureCallback, SettableListenableFuture, etc.)
The reason for deprecation is the introduction of CompletableFuture in Java 8, which offers a richer set of features and better integration with the Java ecosystem.
java.util.concurrent.CompletableFuture is now the recommended way to handle asynchronous operations in Spring applications.
Example:
import java.util.concurrent.CompletableFuture;
An example of a migration would be:
Before migration:
```
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.util.concurrent.SettableListenableFuture;
public class ListenableFutureExample {
public class CompletableFutureCallback {
public static void main(String[] args) {
CompletableFuture<String> completableFuture = new CompletableFuture<>();
Runnable runnable = callUpstreamWS(completableFuture);
completableFuture.whenComplete((res, error) -> {
if (error != null) {
// Handle exception
} else if (res != null) {
// Process data
}
});
new Thread(runnable).start();
}
SettableListenableFuture<String> future = new SettableListenableFuture<>();
future.addCallback(new ListenableFutureCallback<String>() {
@Override
public void onSuccess(String result) {
System.out.println("Success: " + result);
}
private static Runnable callUpstreamWS(CompletableFuture<String> completableFuture) {
return () -> {
@Override
public void onFailure(Throwable ex) {
System.out.println("Failure: " + ex.getMessage());
}
});
// Simulate asynchronous operation
new Thread(() -> {
try {
//Upstream call
Thread.sleep(5000);
Thread.sleep(1000);
future.set("Hello, World!");
} catch (InterruptedException e) {
e.printStackTrace();
future.setException(e);
}
completableFuture.complete("ws.data");
};
}).start();
}
}
```
After migration:
```
import java.util.concurrent.CompletableFuture;
public class CompletableFutureExample {
public static void main(String[] args) {
CompletableFuture<String> future = new CompletableFuture<>();
future.whenComplete((result, ex) -> {
if (ex != null) {
System.out.println("Failure: " + ex.getMessage());
} else if (result != null) {
System.out.println("Success: " + result);
}
});
// Simulate asynchronous operation
new Thread(() -> {
try {
// Thread.currentThread().interrupt();
Thread.sleep(1000);
future.complete("Hello, World!");
} catch (InterruptedException e) {
future.completeExceptionally(e);
}
}).start();
}
}
```
links:
- title: 'Spring 6.0 migration guide'
url: https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.0-Release-Notes#core-container
Expand Down

0 comments on commit bf7e61d

Please sign in to comment.