Async class

Runs an @EventListener method on a TaskExecutor instead of in the publishing call.

Use it for slow listeners (sending a mail, calling a remote service) so that the code publishing the event does not wait. value names the executor bean; without it the context uses the only (or @Primary) TaskExecutor bean, else the one named taskExecutor.

It takes effect only when the context has a TaskExecutor bean, as Spring's @EnableAsync switches it on. The scheduling starter (spring_boot_starter_scheduling) registers one (applicationTaskExecutor, alias taskExecutor). Without any, @Async is ignored and the listener runs synchronously. A failure (thrown, or from the returned Future) goes to the multicaster's error handler, else it is logged; a result is published as an event, as for synchronous listeners.

The generator accepts @Async on @EventListener methods only: there are no runtime proxies for other methods.

@Component()
class Mailer {
  // The order is confirmed without the publisher waiting for the mail.
  @EventListener()
  @Async()
  Future<void> onPlaced(OrderPlaced order) => sendConfirmation(order);

  @EventListener()
  @Async('mailExecutor')
  void onShipped(OrderShipped order) { ... }
}

Spring (Java):

@EnableAsync
@Configuration
class AsyncConfig {}

@Component
class Mailer {
  @Async @EventListener
  CompletableFuture<Void> onPlaced(OrderPlaced order) { ... }

  @Async("mailExecutor") @EventListener
  void onShipped(OrderShipped order) { ... }
}

Constructors

Async([String? value])
Runs the listener asynchronously; value names the executor bean.
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
value → String?
Name of the TaskExecutor bean; null for the default one.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited