samedi 30 janvier 2021

Dependency Inversion Principle in Spring-boot maven multi module for a messaging system

I have been trying to figure out a pattern to satisfy the SOLID Dependency Inversion Principle while using Spring-boot in a Maven multi-module project. The goal is to have an abstraction layer between my message provider and consumer. That way I can swap out my message provider easily and keep things loosely coupled.

ModuleA -> Spring-boot Kafka Consumer (should be easily replaceable)

ModuleB -> Abstract Message Provider (should be able to transform messages)

ModuleC -> MyMessage Processor (should be able to use the transformed messages for business logic)

According to the SOLID principle:

ModuleC should depend on ModuleB

Module A should depend on ModuleB

Using Spring-boot with dependency injection in a Maven multi-module project complicates this form me.

In ModuleA I receive messages using Apache Kafka libraries, all contained in its own maven module.

In ModuleB I expose a Java LinkedBlockingQueue that ModuleA can fill with messages as they come in.

It's here that I transform the message. To make the messages available to ModuleC I expose an 
Observable Class as a Spring @Component.

In ModuleC I Inject the ModuleB Component and add an Observer to detect new messages.

This all works and here is the code for all to see.

Is this just WRONG. Is there a correct Spring way to do this? Though it works, something just smells wrong about it, to me.

Thanks you.

Aucun commentaire:

Enregistrer un commentaire