Skip to content

Commit

Permalink
Merge pull request #35930 from mkouba/document-shutdown-anno
Browse files Browse the repository at this point in the history
Document usage of io.quarkus.runtime.Shutdown annotation
  • Loading branch information
mkouba committed Sep 15, 2023
2 parents 8677d59 + 7c95670 commit b1a1196
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/src/main/asciidoc/lifecycle.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ A bean represented by a class, producer method or field annotated with `@Startup
----
package org.acme.lifecycle;
import io.quarkus.runtime.Startup;
import jakarta.enterprise.context.ApplicationScoped;
@Startup // <1>
Expand All @@ -210,6 +211,57 @@ NOTE: `@Dependent` beans are destroyed immediately afterwards to follow the beha

TIP: If a class is annotated with `@Startup` but with no scope annotation then `@ApplicationScoped` is added automatically.

The `@Startup` annotation can be also declared on a non-static non-producer no-args method:

[source,java]
----
package org.acme.lifecycle;
import io.quarkus.runtime.Startup;
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class EagerAppBean {
@Startup
void init() { <1>
doSomeCoolInit();
}
}
----
<1> The bean is created and the `init()` method is invoked upon the contextual instance when the application starts.

[[shutdown_annotation]]
=== Using `@Shutdown` to execute a business method of a CDI bean during application shutdown

The `@io.quarkus.runtime.Shutdown` annotation is used to mark a business method of a CDI bean that should be executed during application shutdown.
The annotated method must be non-private and non-static and declare no arguments.
The behavior is similar to a declaration of a `ShutdownEvent` observer.
The following examples are functionally equivalent.

[source,java]
----
import io.quarkus.runtime.Shutdown;
import io.quarkus.runtime.ShutdownEvent;
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
class Bean1 {
void onShutdown(@Observes ShutdownEvent event) {
// place the logic here
}
}
@ApplicationScoped
class Bean2 {
@Shutdown
void shutdown() {
// place the logic here
}
}
----

== Package and run the application

Run the application with:
Expand Down

0 comments on commit b1a1196

Please sign in to comment.