Skip to content

Commit

Permalink
refactor to Trigger.OnStartup
Browse files Browse the repository at this point in the history
  • Loading branch information
efgpinto committed Dec 19, 2023
1 parent a3bfebe commit 2b666cb
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docs/src/modules/java/pages/actions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Such validation depending on state can only safely be done handling the command

== Actions as Life-cycle Hooks

An Action method can be triggered automatically when some predefined service life-cycle event happens (currently, only on startup is available), serving as a custom hook. For such use, the method needs to be public, annotated with `@Trigger` and cannot receive any parameters, as shown below.
An Action method can be triggered automatically when some predefined service life-cycle event happens (currently, only on startup is available), serving as a custom hook. For such use, the method needs to be public, annotated with `@Trigger.OnStartup` and cannot receive any parameters, as shown below.

IMPORTANT: The on startup hook is called every time a service instance boots up. This can happen for very different reasons: restarting / redeploying the service, scaling up to more instances or even without any user-driven action (e.g. Kalix Runtime versions being rolled out, infrastructure-related incidents, etc.). Therefore, you should carefully consider how you use this hook and its implementation.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
public class OnStartupAction extends Action { // <1>

@PostMapping("/init")
@Trigger(
on = STARTUP, // <2>
@Trigger.OnStartup( // <2>
maxRetries = 3) // <3>
public Action.Effect<String> init() { // <4>
// Do some initial operations here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,24 @@
* The method must be public and have no parameters.
* If the call fails, it will be retried up to the number of times specified by the maxRetries parameter.
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Trigger {
enum TriggerEvent {
/**
* The on startup hook is called every time a service instance boots up.
* This can happen for very different reasons: restarting / redeploying the service,
* scaling up to more instances or even without any user-driven action
* (e.g. Kalix Runtime versions being rolled out, infrastructure-related incidents, etc.).
* Therefore, one should carefully consider how to use this hook and its implementation.
*/
STARTUP,
}


/**
* The service life-cycle event for which this hook will be triggered.
* The on startup hook is called every time a service instance boots up.
* This can happen for very different reasons: restarting / redeploying the service,
* scaling up to more instances or even without any user-driven action
* (e.g. Kalix Runtime versions being rolled out, infrastructure-related incidents, etc.).
* Therefore, one should carefully consider how to use this hook and its implementation.
*/
TriggerEvent on();
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface OnStartup {
/**
* The maximum number of retries we will do upon failure of the method hook calls.
* The default value 0 means no retries are done.
*/
int maxRetries() default 0;
}

/**
* The maximum number of retries we will do upon failure of the method hook calls.
* The default value 0 means no retries are done.
*/
int maxRetries() default 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ import scala.PartialFunction.condOpt
private[impl] object ActionDescriptorFactory extends ComponentDescriptorFactory {

private def hasTriggerMethodOptions(javaMethod: Method): Boolean = {
javaMethod.isPublic && javaMethod.hasAnnotation[Trigger]
javaMethod.isPublic && javaMethod.hasAnnotation[Trigger.OnStartup] // this is the only event available at the moment
}

private def triggerOptions(javaMethod: Method): Option[TriggerOptions] = {
condOpt(hasTriggerMethodOptions(javaMethod)) { case true =>
val ann = javaMethod.getAnnotation(classOf[Trigger]);
val ann = javaMethod.getAnnotation(classOf[Trigger.OnStartup]);

TriggerOptions
.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

import java.util.List;

import static kalix.javasdk.annotations.Trigger.TriggerEvent.STARTUP;

public class ActionsTestModels {

public static class GetWithoutParam extends Action {
Expand Down Expand Up @@ -224,7 +222,7 @@ public Flux<Effect<Message>> message(@RequestBody Flux<Message> messages) {

public static class OnStartupHookAction extends Action {
@PostMapping("/message")
@Trigger(on = STARTUP, maxRetries = 2)
@Trigger.OnStartup(maxRetries = 2)
public Action.Effect<Message> init() {
return effects().reply(new Message("hello"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ import kalix.spring.testmodels.subscriptions.PubSubTestModels.TypeLevelSubscribe
import kalix.spring.testmodels.subscriptions.PubSubTestModels.TypeLevelTopicSubscriptionWithPublishToTopicAction
import kalix.spring.testmodels.subscriptions.PubSubTestModels.VEWithPublishToTopicAction
import kalix.spring.testmodels.valueentity.CounterState
import kalix.triggers.TriggerOptions.TriggerEvent
import org.scalatest.wordspec.AnyWordSpec

import scala.jdk.CollectionConverters.CollectionHasAsScala
Expand Down

0 comments on commit 2b666cb

Please sign in to comment.