Spring boot Kafka messaging. How to use SpEL to manage handler access - spring

I'm using Kafka in Spring Boot project. There are a lot of benefits in case you have simple flow (to use #KafkaListener, #KafkaHandler) and spring prepares almost everything for development.
In my application I have different handlers for the same message data. I want to use SpEL to manage handlers manipulating header data, but I've not detected corresponding API for that.
So my question: is it possible to manage my handlers via SpEL in case I have special headers for that (Header for example "X-OPERATION_TYPE":"patch")? How?
P.S.
I can make workarounds using GoF Strategy as example, but I hope spring already has solution for that case.

There is not such a "conditional routing" in Spring for Apache Kafka, but you can do that routing manually in the single #KafkaListener with plain if...else or switch.
For more comprehensive routing logic it would be better to take a look into Spring Integration: https://docs.spring.io/spring-integration/docs/5.0.9.RELEASE/reference/html/messaging-routing-chapter.html

Related

Custom Spring Actuator Endpoint which has subsystem and can be added dynamically

I'm looking for a way to implement custom endpoints for a reactive application using Spring Boot 2.2.
The endpoints have some subsystems and perform specific resource operations on the subsystems. The URL paths look like:
/actuator/system1/subsystem_a
/actuator/system1/subsystem_b
/actuator/system2/subsystem_c
Furthermore, system1 and system2 are not both always deployed, so I'd like to add dynamically the endpoints of the deployed system only.
I know I can use ReactiveHealthContributorRegistry to add custom health check endpoints dynamically. Is there a similar way for a fully custom endpoint?
Thanks in advance.
It seems there is no way to construct such complex endpoints like what I asked in Spring Boot Actuator.
I finally decided to use RouterFunction and HandlerFunction referring to the following websites.
https://www.baeldung.com/spring-5-functional-web
https://spring.io/blog/2016/09/22/new-in-spring-5-functional-web-framework

Does Apache Camel replace or complement creating micro-services with Spring Boot?

I have been working for a while with Spring micro-services and have no come across Apache Camel as a tool for building micro-services. I'm unclear -- is Apache Camel a replacement for creating micro-sevices with Spring Boot or does it add functionality / short-cuts to developing such services with Spring Boot? It's already fairly simple to create microservices with Spring Boot so it's hard to imagine what Apache Camel would add but that is the essence of my question.
Apache Camel has nothing to do with microservices.
It's an implementation of the Enterprise Integration Patterns: https://www.enterpriseintegrationpatterns.com/
Apache Camel provides an implementation for most of the patterns from the book from Gregor Hohpe and Bobby Woolf. Plus a variety of inbound and outbound endpoints to integrate with systems like the file system, FTP, HTTP, Messaging, Facebook etc.
Find more information on the website: https://camel.apache.org/
There is a Spring Boot Starter project to run Camel in a Spring Boot application:
https://camel.apache.org/spring-boot.html
what Apache Camel would add, that is the essence of my question
In service of declaring REST based microservices, Camel's REST DSL provides a fluent API for declaring microservices. Take for example:
rest("/books").produces("application/json")
.get().outType(Book[].class)
.to("bean:bookService?method=getBooks(${header.bookCategory})")
Should tell you at a glance that requests to the path /books will get you a List of Book, as long as you send a request parameter named bookCategory. This is mapped to a POJO bean called bookService.
Spring Boot is a framework which simplifies application packing and startup while Spring is the actual framework which has libraries for performing various tasks.
Technically, we can use Camel for building micro-services as well and many aspects of camel depend on Spring. If you foresee many integration related functionality like sending email or communicating with other system, you can use also use Hexagonal architecture.

Kafka bindings without #EnableBinding annotations in Spring

I'm using spring cloud to connect to my Kafka broker. It works fine. Now I want to create my binding by code instead of annotation.
Is there a convenient way to do it?
Could you elaborate why do you want to do the binding programmatically instead of using #EnableBinding.
While Spring Cloud Stream simplifies exactly that, if you prefer to use your own way of connecting (for any other specific reason), then you might want to check the Spring Integration adapters to do the binding. But, in this case, you are on your own by setting up the lifecycle and all other goodies that Spring Cloud Stream provides.
If you still want to use Spring Cloud Stream but don't want to use the annotation, then check here to see all the configuration that Spring Cloud Stream does when you annotate and apply your use case.
Please follow https://github.com/spring-cloud/spring-cloud-stream/issues/954. We plan to add this feature to 1.3.0.RC1.

Is a HTTP server(Jetty) required for hosting camel routes processing HL7 messages

I want to create a HL7 listener in camel and process the HL7 messages I receive. I was planning to use SpringBoot/dropwizard along with camel for this purpose. The reason dropwizard is already used in my company for creating restful API's and I thought of reusing them for creating Camel routes as microservices also. My questions are,
HL7 messages are received using MLLP(Mina or Netty) over TCP. There isnt any HTTP involved. So is there any purpose of using a server like Jetty? Is it better to use Camel standlone?
If there is not any HTTP requests/listeners involved in my camel application, is there any use of me going for dropwizard/Springboot, as these frameworks were mainly created for creating Restful API's i.e for HTTP traffic?
Ad 1)
Yes HTTP is not involved and Camel uses Netty (preferred) or Mina.
Mind there is camel-mllp which is a more hardened than camel-hl7 which has some more advanced HL7 corner cases fixed. See the readme file: https://github.com/apache/camel/tree/master/components/camel-mllp
Ad 2)
You can opt out HTTP in spring boot, just dont have its -starter-web dependency and its a standalone non HTTP app. Just mind that you may need to turn on camel.springboot.main-run-controller=true in the application.properties to keep the JVM running.
And by using Spring Boot or DropWizard etc you have a similar deployment and packaging as other apps, instead of having to create something yourself.

Dynamically register hystrix commands without javanica annotations in spring boot

We have developed a software proxy based on spring boot and zuul, that is meant to govern services within our integration layer. We do not own the systems consuming the various services, nor do we own the actual services themselves. The services are SOAP based webservices at present. We make use of pre, post , error and route filters. Validations are database driven, including which client is allowed to call what webservice. All service definitions reside in the database (request endpoint, request xsd, response xsd, which clients are allowed to invoke, etc.).
The aim now is to add hystrix commands to handle service failures, as well as a hystrix dashboard.
The standard way to use hystrix commands involves annotating service methods with javanica. Is there a way to dynamically declare/register hystrix commands for these webservices at runtime after reading the configurations from the database? The hystrix interception will need to happen based on the multiple webservice endpoints being invoked from a single point.
Hoping this is achievable ...if not, I would really appreciate any alternative proposals for how hystrix commands could be declared in this way.
Thanks!
You're saying that you are already using Spring Boot and Zuul. How are you mapping the routes? Through the url param? Then you'll have to enroll your own. But if you define the routes as ribbon services and pass the routes as ribbon servers as described in the documentation you will get Hystrix for free.

Resources