🏷 Async Programming
2 patterns
Topic: Async Programming
All Java patterns related to Async Programming — java.evolved
Concurrency
CompletableFuture chaining
Old
Future<String> future =
executor.submit(this::fetchData);
String data = future.get(); // blocks
String result = transform(data);
Modern
CompletableFuture.supplyAsync(
this::fetchData
)
.thenApply(this::transform)
.thenAccept(IO::println);
hover to see modern →
JDK 8+
learn more →
Enterprise
Message-Driven Bean vs Reactive Messaging
Old
@MessageDriven(activationConfig = {
@ActivationConfigProperty(
propertyName = "destinationType",
propertyValue = "jakarta.jms.Queue"),
@ActivationConfigProperty(
propertyName = "destination",
propertyValue = "java:/jms/OrderQueue")
})
public class OrderMDB implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage txt = (TextMessage) message;
processOrder(txt.getText());
}
}
Modern
@ApplicationScoped
public class OrderProcessor {
@Incoming("orders")
public void process(Order order) {
// automatically deserialized from
// the "orders" channel
fulfillOrder(order);
}
}
hover to see modern →
JDK 11+
learn more →