diff --git a/docs/src/modules/java/pages/actions.adoc b/docs/src/modules/java/pages/actions.adoc index 394225df0c..3b5c408d0c 100644 --- a/docs/src/modules/java/pages/actions.adoc +++ b/docs/src/modules/java/pages/actions.adoc @@ -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. diff --git a/samples/java-spring-doc-snippets/src/main/java/com/example/trigger/OnStartupAction.java b/samples/java-spring-doc-snippets/src/main/java/com/example/trigger/OnStartupAction.java index 89fdda4105..d9e9e96d62 100644 --- a/samples/java-spring-doc-snippets/src/main/java/com/example/trigger/OnStartupAction.java +++ b/samples/java-spring-doc-snippets/src/main/java/com/example/trigger/OnStartupAction.java @@ -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 init() { // <4> // Do some initial operations here diff --git a/sdk/java-sdk-spring/src/main/java/kalix/javasdk/annotations/Trigger.java b/sdk/java-sdk-spring/src/main/java/kalix/javasdk/annotations/Trigger.java index d5644f1f61..3a1acc4042 100644 --- a/sdk/java-sdk-spring/src/main/java/kalix/javasdk/annotations/Trigger.java +++ b/sdk/java-sdk-spring/src/main/java/kalix/javasdk/annotations/Trigger.java @@ -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; } diff --git a/sdk/java-sdk-spring/src/main/scala/kalix/javasdk/impl/ActionDescriptorFactory.scala b/sdk/java-sdk-spring/src/main/scala/kalix/javasdk/impl/ActionDescriptorFactory.scala index 3672da299b..4888d80ab3 100644 --- a/sdk/java-sdk-spring/src/main/scala/kalix/javasdk/impl/ActionDescriptorFactory.scala +++ b/sdk/java-sdk-spring/src/main/scala/kalix/javasdk/impl/ActionDescriptorFactory.scala @@ -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() diff --git a/sdk/java-sdk-spring/src/test/java/kalix/spring/testmodels/action/ActionsTestModels.java b/sdk/java-sdk-spring/src/test/java/kalix/spring/testmodels/action/ActionsTestModels.java index f7defb214e..63b4d756a0 100644 --- a/sdk/java-sdk-spring/src/test/java/kalix/spring/testmodels/action/ActionsTestModels.java +++ b/sdk/java-sdk-spring/src/test/java/kalix/spring/testmodels/action/ActionsTestModels.java @@ -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 { @@ -224,7 +222,7 @@ public Flux> message(@RequestBody Flux messages) { public static class OnStartupHookAction extends Action { @PostMapping("/message") - @Trigger(on = STARTUP, maxRetries = 2) + @Trigger.OnStartup(maxRetries = 2) public Action.Effect init() { return effects().reply(new Message("hello")); } diff --git a/sdk/java-sdk-spring/src/test/scala/kalix/javasdk/impl/ActionDescriptorFactorySpec.scala b/sdk/java-sdk-spring/src/test/scala/kalix/javasdk/impl/ActionDescriptorFactorySpec.scala index 7197b7e8b4..298e31a778 100644 --- a/sdk/java-sdk-spring/src/test/scala/kalix/javasdk/impl/ActionDescriptorFactorySpec.scala +++ b/sdk/java-sdk-spring/src/test/scala/kalix/javasdk/impl/ActionDescriptorFactorySpec.scala @@ -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