cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a reuse service with a custom handler in CAP Java?

0 Kudos

I'm trying to create a reuse service using the CAP for Java framework.

However, the documentation and available tutorials are missing a critical step: how to provide custom logic.

This documentation explains how to do so with the Node.js flavor. However, it says nothing about Java.

This tutorial does explain how to make a reuse service for CAP Java -- however, it says nothing about how to link your custom event handler to the service so you can actually use it.

Does anyone know how to achieve this?

View Entire Topic
marcbecker
Contributor

Hi Brian,

as far as I understood your question it goes more into a direction of how to share reusable CDS service definitions and corresponding event handler code across projects, by directly importing these CDS artifacts and Java event handlers into the projects themselves.

As you correctly identified there is not much documentation and examples on that. This is due to the fact that we haven't intensly investigated this topic so far in CAP Java.

In Node.js sharing service definitions, event handler code, etc. happens through NPM modules. For Java this usually doesn't feel like the right fit, as here dependency tools such as Maven or Gradle are usually used. However as of now there is no mechanism that is supported by the CDS tools that would feel more native to Java.

What I think is possible today (although I haven't explicitly tried it yet):

  • You can as of today share CDS models and service definitions using NPM modules also in Java projects. This however requires at least an additional "npm install" step in your project, the maintance of a package.json and some sort of publishing of the NPM module to a (local) registry.
  • If your reuse project and the projects that are reusing it are more tightly coupled you can also put those projects in a single monorepo and directly refer to the reuse project via relative paths. This is not as flexible as NPM modules, but also a feasible approach depending on the use-case.
  • I don't know of any way to share Java event handler code through NPM modules. However you can basically build a reuse library, that you package as a dedicated maven dependency that contains the source code of your Java event handlers. You can use Spring Boot features to automatically contribute your event handlers to the Spring application through auto-configuration (see here for more details: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configu.... Remember: Event handlers in CAP Java Spring Boot applications are simply Spring beans! You might also want to leverage the possibility for "generic" event handlers. These are event handlers registering on not a specific service by name, but on all services of a certain type, see more here: https://cap.cloud.sap/docs/java/provisioning-api#handlerclasses.

With those approaches you basically split the CDS-based reuse and the code-based reuse across two artifacts that you both need to include into your reuse project.

In the future CAP might provide mechanisms to also share CDS models via a more native Java dependency management, for example via Maven dependencies, but these are only visions at the moment.

Hope this helps.

Best regards,
Marc

0 Kudos

Hi Marc, thanks for your excellent response and thanks for taking the time to write all that out. 🙂