diff --git a/README.md b/README.md index e4ac3a5..3f48e8a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A CAP Java plugin that integrates SAP Alert Notification service (ANS) with the The `cds-feature-notifications` plugin enables out-of-the-box notification handling for CAP Java applications. Annotate CDS events with `@notification`, emit them from a handler or annotate entities with `@notifications` for automatic delivery triggered by CRUD events, actions, or functions. The plugin takes care of the rest and delivers notifications via [SAP Alert Notification service (ANS)](https://help.sap.com/docs/alert-notification). -In **local mode**, notifications are logged to the console with no ANS binding required. In **production mode**, notifications are delivered through Web (SAP Build Work Zone) and Email channels. You can either create a standalone ANS service instance or use Work Zone's built-in notification credentials. Both approaches are covered in the [Production Mode](#production-mode) section. +In **local mode**, notifications are logged to the console with no ANS binding required. In **production mode**, notifications are delivered through Web (SAP Build Work Zone) and Email channels. Production mode is activated automatically when an `alert-notification` service binding is detected, or can be enabled explicitly via configuration. You can either create a standalone ANS service instance or use Work Zone's built-in notification credentials. Both approaches are covered in the [Production Mode](#production-mode) section. ## Table of Contents @@ -387,7 +387,7 @@ Both approaches require the same setup: enable production mode, then provide an ### Enable Production Mode -In `srv/src/main/resources/application.yaml`, enable production mode: +Production mode is activated automatically when an `alert-notification` service binding is detected at startup. You can also enable it explicitly in `srv/src/main/resources/application.yaml`, for example for hybrid testing against a remote ANS instance without a local binding: ```yaml cds: @@ -396,7 +396,7 @@ cds: enabled: true ``` -> **Note:** If this property is omitted, the plugin defaults to local mode (console output only, no ANS required). +If neither a binding is present nor the property is set, the plugin defaults to local mode (console output only, no ANS required). In production mode: - **`ProductionHandler`** sends notifications to ANS (instead of console logging) diff --git a/cds-feature-notifications/src/main/java/com/sap/cds/notifications/NotificationServiceConfiguration.java b/cds-feature-notifications/src/main/java/com/sap/cds/notifications/NotificationServiceConfiguration.java index a5ab934..da75db9 100644 --- a/cds-feature-notifications/src/main/java/com/sap/cds/notifications/NotificationServiceConfiguration.java +++ b/cds-feature-notifications/src/main/java/com/sap/cds/notifications/NotificationServiceConfiguration.java @@ -96,8 +96,24 @@ public void eventHandlers(CdsRuntimeConfigurer configurer) { OutboxService.PERSISTENT_ORDERED_NAME); } - if (Boolean.TRUE.equals(environment.getProduction().isEnabled())) { - logger.info("Production mode enabled - using ProductionHandler"); + boolean ansBindingPresent = + configurer + .getCdsRuntime() + .getEnvironment() + .getServiceBindings() + .anyMatch( + b -> b.getServiceName().map(n -> n.equals("alert-notification")).orElse(false)); + + boolean productionEnabled = + environment.getProduction() != null + && Boolean.TRUE.equals(environment.getProduction().isEnabled()); + + if (productionEnabled || ansBindingPresent) { + if (ansBindingPresent && !productionEnabled) { + logger.info("alert-notification binding detected - using ProductionHandler"); + } else { + logger.info("Production mode enabled - using ProductionHandler"); + } configurer.eventHandler(new ProductionHandler(outboxedSvc, configurer.getCdsRuntime())); // Register handler for auto-provisioning standalone templates on application prepared event configurer.eventHandler( @@ -135,7 +151,7 @@ public void environment(CdsRuntimeConfigurer configurer) { runtime .getEnvironment() .getServiceBindings() - .filter(b -> b.getServiceName().get().equals("alert-notification")) + .filter(b -> b.getServiceName().map(n -> n.equals("alert-notification")).orElse(false)) .findFirst() .orElse(null);