generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 140
alesi documentation #2352
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
Open
rjayasinghe
wants to merge
2
commits into
main
Choose a base branch
from
alesi_java_docu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
alesi documentation #2352
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| --- | ||
| synopsis: > | ||
| This guide shows how non-CAP Spring Boot applications can integrate themselves into service offerings of SAP BTP using different CAP modules without the need to completely migrate to CAP Java. | ||
| status: released | ||
| --- | ||
|
|
||
| # Use CAP Java modules with plain Spring Boot applications | ||
|
|
||
| <style scoped> | ||
| h1:before { | ||
| content: "Java"; display: block; font-size: 60%; margin: 0 0 .2em; | ||
| } | ||
| </style> | ||
|
|
||
| {{ $frontmatter.synopsis }} | ||
|
|
||
| The CAP Java framework [integrates itself with Spring Boot](./spring-boot-integration) every CAP Java app is also a Spring Boot app. But thanks to the modular nature of CAP Java every plain Spring Boot application (built without CAP) can also use dedicated CAP Java modules (features) if needed. | ||
|
|
||
| The main use case for using CAP Java features in plain Spring Boot is to re-use existing BTP integration modules like e.g. the BTP Audit Log Service or SAP Event Hub. | ||
|
|
||
| In general, adding a feature is just adding one or more dependencies to the application's `pom.xml` as well as adding configuration to the application.yaml (or other mechanisms for [Spring Boot configuration](https://docs.spring.io/spring-boot/reference/features/external-config.html). | ||
|
|
||
| ## SAP Audit Log Service | ||
|
|
||
| In order to add Audit log support you need to add the following dependencies to your `pom.xml`: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>com.sap.cds</groupId> | ||
| <artifactId>cds-services-api</artifactId> | ||
| <version>${cds.services.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.sap.cds</groupId> | ||
| <artifactId>cds-framework-spring-boot</artifactId> | ||
| <version>${cds.services.version}</version> | ||
| <scope>runtime</scope> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| Please also maintain a version property in your `<properties>` section: | ||
|
|
||
| ```xml | ||
| <cds.services.version>4.6.2</cds.services.version> | ||
| ``` | ||
|
|
||
| :::info | ||
| As the logical layer of audit log is already part of the core CAP Java components you can already start with these 2 additions using the audit log for local development. The audit log messages will be written to SLF4j's default logger using the log level `DEBUG`. In order to see the messages on the console of your app you need to set the log level to 'DEBUG': | ||
| ::: | ||
|
|
||
| ```yaml | ||
| logging.level.com.sap.cds.auditlog: DEBUG | ||
| ``` | ||
|
|
||
| Now, you can write client code that is actually producing and publishing audit log messages. First, you need to choose the class/component in that you want to publish audit log messages. In that class you need to declare a new class member of type `com.sap.cds.services.auditlog.AuditLogService` and inject it via constructor injection (@Autowired works but is discouraged). With that done, you can create the message that is going to be published as a audit log: | ||
|
|
||
| ```java | ||
| ConfigChange change = ConfigChange.create(); | ||
| DataObject object = DataObject.create(); | ||
| KeyValuePair key = KeyValuePair.create(); | ||
| key.setKeyName("id"); | ||
| key.setValue(String.valueOf(owner.getId())); | ||
| object.setId(List.of(key)); | ||
| object.setType("Owner"); | ||
| change.setDataObject(object); | ||
| ChangedAttribute attribute = ChangedAttribute.create(); | ||
| attribute.setName("Owner"); | ||
| attribute.setNewValue(String.valueOf(owner.getId())); | ||
| change.setAttributes(List.of(attribute)); | ||
| ``` | ||
|
|
||
| In the final step you pass the created message to the `AuditLogService`: | ||
|
|
||
| ```java | ||
| auditLogService.logConfigChange(Action.CREATE, createOwnerConfigChange(owner.getId())); | ||
| ``` | ||
|
|
||
| When you now run through the modified application code you should be able to read the logged audit message on the console of your Spring Boot application. | ||
|
|
||
| As said, with the past changes you just enabled the local variant of the audit log support. For the full integration of the deployed application with the SAP Audit Log service you also need to add this dependency to your `pom.xml`: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>com.sap.cds</groupId> | ||
| <artifactId>cds-feature-auditlog-ng</artifactId> | ||
| <version>0.0.3</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| For further configuration of this module please follow the [README of the module](https://github.com/cap-java/cds-feature-auditlog-ng). | ||
|
|
||
| ## CAP Messaging | ||
|
|
||
| The CAP framework offers a logical messaging layer. This means that applications can emit events and messages to a `MessagingService` regardless of the target messaging infrastructure. The local default implementation for messaging is using the filesystem as the communication layer. Similar to the logical audit log support the messaging layer is already part of the core CAP Java modules. Thus, a plain Spring Boot application only needs to perform these two steps to activate CAP messaging: | ||
|
|
||
| At first you need to make sure that the following dependencies are part of your `pom.xml`: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>com.sap.cds</groupId> | ||
| <artifactId>cds-services-api</artifactId> | ||
| <version>${cds.services.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.sap.cds</groupId> | ||
| <artifactId>cds-framework-spring-boot</artifactId> | ||
| <version>${cds.services.version}</version> | ||
| <scope>runtime</scope> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| Again, you need to set a property for the `cds.services.version`: | ||
|
|
||
| ```xml | ||
| <cds.services.version>4.6.2</cds.services.version> | ||
| ``` | ||
|
|
||
| After setting up the dependencies you just need to activate the file-based messaging in the applications configuration: | ||
|
|
||
| ```yaml | ||
| cds.messaging.services.messaging.kind: file-based-messaging | ||
| ``` | ||
| ### Emitting Messages | ||
|
|
||
| In order to use CAP Java messaging to send messages from your application's code you need to inject an instance of `com.sap.cds.services.messaging.MessageService` into your class and set it as a class member. Then, you can use the CAP Java `MessageService` in your application's code like this to emit messages: | ||
|
|
||
| ```java | ||
| Map<String, Object> data = Map.of("ownerId", owner.getId(), "petId", petId, "date", visit.getDate(), "descr", visit.getDescription()); | ||
| messaging.emit("org.spring.alesi.PlannedVisit", data); | ||
| ``` | ||
|
|
||
| ### Handling Messages | ||
|
|
||
| And in another | ||
|
|
||
| @On(event = "org.spring.alesi.PetBabiesBorn", service = "messaging") | ||
| public void handlePetBabiesBorn(TopicMessageEventContext context) { | ||
| Map<String, Object> data = context.getDataMap(); | ||
| int ownerId = (int) data.get("ownerId"); | ||
| int petId = (int) data.get("motherId"); | ||
| String count = (String) data.get("description"); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| When you now start your application you will see new files created at the root of your application's file system. These are the files being used for exchanging messages. run through your modified application code you will see that | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe one of these: calesi, Calesi, allis, aleisa?