Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document usage of io.quarkus.runtime.Shutdown annotation #35930

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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