I have the following requirement.
Read data from a messaging Queue and process each message to enrich the message and then finally send the enriched message to a different system. The enrichment logic is in DB and it is based on message type and each logic can be of different language. Each message will have some type like, bank, insurance, ...
Sample enrichment logic data in Mongo DB,
{
type : "Bank",
logics : [
{
"key" : "location.phone",
"type" : "javascript",
"logic" : "function getPhone() { return '888-888-8888';}"
},
{
"key" : "location.zip",
"type" : "groovy",
"logic" : "function getZip() { return '56781';}"
},
{
"key" : "location.address",
"type" : "groovy",
"logic" : "function getAddress() { return '1234 new address';}"
},
]
}
Now on receiving each message from the Queue, Check the type and enrich the data based on the logics for that type.
This is a spring boot based non-web Java Application.
My executing would start from the below run method.
public class AppRunner implements ApplicationRunner {
// Other Bean injection is here
@Override
public void run(final ApplicationArguments args) throws Exception {
// Application starts here ...
}
}
I defined EnrichmentLogicDAO to load the logic from the DB and have a EnrichmentDBService for the DAO. Have an interface with a method exeucte( Command pattern ). There are 2 implementors. 1. Javascript, 2. Groovy
In Javascript executor,
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
In Groovy executor,
ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
In both,
engine.eval(logic1);
engine.eval(logic2);
....
Load all the logic from DB. Javascript based logic should be loaded in JavaScriptExecutor engine and groovy based logic should be loaded in GroovyExecutor engine.
Question 1 :-
How to load all the logics to the corresponding engine?
Currently, I have my code to load the logic inside the AppRunner's run method and call the corresponding Executor class static method based on the type from DB to load the logics into the engine.
Question 2 :-
The code to execute the enrichment logic remain same for both the engine. Only the engine will be different based on the logic type.
I need some help on Loading the logics to the corresponding engine and invoke the logic on each message.
Also, I thought of using the Chain of responsibility pattern, so that the input message would be passed to the series of engines and the message will be enriched based on the logics loaded in each engine. Is there any other better approach.