I went through a very interesting situation at work and I wanted to share the solution here.
Imagine that you need to process a set of data. And to deal with this set of data you have several different strategies for this. For example, I needed to create strategies for how to fetch a collection of data from S3, or examples within the local repository, or passed as input.
And whoever will dictate this strategy is the one making the request:
I want to get the data in S3. Take the data generated on day X between hours H1 and H2, which is from the Abóbora client. Get the last 3000 data that meets this.
Or else:
Take the example data you have there, copy it 10000 times to do the stress test.
Or even:
I have this directory, you also have access to it. Get everything in that directory and recursively into the subdirectories.
And also finally:
Take this data unit that is in the input and use it.
My first thought was: "how can I define the shape of my input in Java?"
And I reached the first conclusion, super important for the project: "you know what? I'm not going to define shape. Add a Map
On top of that, as I didn't put any shapes in the DTO, I had complete freedom to experiment with the input.
So after establishing a proof of concept, we arrive at the situation: we need to get out of the stress POC and move on to something close to real use.
The service I was doing was to validate rules. Basically, when changing a rule, I needed to take that rule and match it against the events that occurred in the production application. Or, if the application was changed and there were no bugs, the expectation is that the decision for the same rule will remain the same for the same data; Now, if the decision for the same rule using the same data set is changed... well, that's potential trouble.
So, I needed this application to run the backtesting of the rules. I need to hit the real application sending the data for evaluation and the rule in question. The use of this is quite diverse:
So, for that, I need some strategies for the origin of events:
And I also need strategies that are different from my rules:
How to deal with this? Well, let the user provide the data!
Do you know something that always caught my attention about json-schema? This here:
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "https://json-schema.org/draft/2020-12/schema", "$vocabulary": { //... } }
These fields start with $. In my opinion, they are used to indicate metadata. So why not use this in the data input to indicate the metadata of which strategy is being used?
{ "dados": { "$strategy": "sample", "copias": 15000 }, //... }
For example, I can order 15000 copies of the data I have as a sample. Or request some things from S3, making a query in Athena:
{ "dados": { "$strategy": "athena-query", "limit": 15000, "inicio": "2024-11-25", "fim": "2024-11-26", "cliente": "Abóbora" }, //... }
Or in the localpath?
{ "dados": { "$strategy": "localpath", "cwd": "/home/jeffque/random-project-file", "dir": "../payloads/esses-daqui/top10-hard/" }, //... }
And so I can delegate to the selection of the strategy ahead.
My first approach to dealing with strategies was this:
public DataLoader getDataLoader(Map<String, Object> inputDados) { final var strategy = (String) inputDados.get("$strategy"); return switch (strategy) { case "localpath" -> new LocalpathDataLoader(); case "sample" -> new SampleDataLoader(resourcePatternResolver_spring); case "athena-query" -> new AthenaQueryDataLoader(athenaClient, s3Client); default -> new AthenaQueryDataLoader(athenaClient, s3Client); } }
So my architect asked two questions during the code-review:
What did I understand from this? That using the facade would be a good idea to delegate processing to the correct corner and... to give up manual control?
Well, a lot of magic happens because of Spring. Since we are in a Java house with Java expertise, why not use idiomatic Java/Spring, right? Just because I as an individual find some things difficult to understand does not necessarily mean that they are complicated. So, let's embrace the world of Java dependency injection magic.
What used to be:
final var dataLoader = getDataLoader(inputDados) dataLoader.loadData(inputDados, workingPath);
Became:
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "https://json-schema.org/draft/2020-12/schema", "$vocabulary": { //... } }
So my controller layer doesn't need to manage this. Leave it to the facade.
So, how are we going to do the facade? Well, to start, I need to inject all the objects into it:
{ "dados": { "$strategy": "sample", "copias": 15000 }, //... }
Ok, for the main DataLoader I write it as @Primary in addition to @Service. The rest I just write down with @Service.
Test this here, setting getDataLoader to return null just to try out how Spring is calling the constructor and... it worked. Now I need to note with metadata each service what strategy they use...
How to do this...
Well, look! In Java we have annotations! I can create a runtime annotation that has within it which strategies are used by that component!
So I can have something like this in AthenaQueryDataLoader:
{ "dados": { "$strategy": "athena-query", "limit": 15000, "inicio": "2024-11-25", "fim": "2024-11-26", "cliente": "Abóbora" }, //... }
And I can have aliases too, why not?
{ "dados": { "$strategy": "localpath", "cwd": "/home/jeffque/random-project-file", "dir": "../payloads/esses-daqui/top10-hard/" }, //... }
And show!
But how to create this annotation? Well, I need it to have an attribute that is a vector of strings (the Java compiler already deals with providing a solitary string and transforming it into a vector with 1 position). The default value is value. It looks like this:
public DataLoader getDataLoader(Map<String, Object> inputDados) { final var strategy = (String) inputDados.get("$strategy"); return switch (strategy) { case "localpath" -> new LocalpathDataLoader(); case "sample" -> new SampleDataLoader(resourcePatternResolver_spring); case "athena-query" -> new AthenaQueryDataLoader(athenaClient, s3Client); default -> new AthenaQueryDataLoader(athenaClient, s3Client); } }
If the annotation field wasn't value, I would need to make it explicit, and that would look ugly, as in the EstrategiaFeia annotation:
final var dataLoader = getDataLoader(inputDados) dataLoader.loadData(inputDados, workingPath);
It doesn't sound so natural in my opinion.
Okay, given that, we still need:
To extract the annotation, I need to have access to the object class:
dataLoaderFacade.loadData(inputDados, workingPath);
On top of that, can I ask if this class was annotated with an annotation like Strategy:
@Service // para o Spring gerenciar esse componente como um serviço public class DataLoaderFacade implements DataLoader { public DataLoaderFacade(DataLoader primaryDataLoader, List<DataLoader> dataLoaderWithStrategies) { // armazena de algum modo } @Override public CompletableFuture<Void> loadData(Map<String, Object> input, Path workingPath) { return getDataLoader(input).loadData(input, workingPath); } private DataLoader getDataLoader(Map<String, Object> input) { final var strategy = input.get("$strategy"); // magia... } }
Do you remember that it has the values field? Well, this field returns a vector of strings:
@Service @Primary @Estrategia("athena-query") public class AthenaQueryDataLoader implements DataLoader { // ... }
Show! But I have a challenge, because before I had an object of type T and now I want to map that same object into, well, (T, String)[]. In streams, the classic operation that does this is flatMap. And Java also doesn't allow me to return a tuple like that out of nowhere, but I can create a record with it.
It would look something like this:
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "https://json-schema.org/draft/2020-12/schema", "$vocabulary": { //... } }
What if there is an object that was not annotated with strategy? Will it give NPE? Better not, let's filter it out before the NPE:
{ "dados": { "$strategy": "sample", "copias": 15000 }, //... }
Given that, I still need to put together a map. And, well, look: Java already provides a collector for this! Collector.toMap(keyMapper, valueMapper)
{ "dados": { "$strategy": "athena-query", "limit": 15000, "inicio": "2024-11-25", "fim": "2024-11-26", "cliente": "Abóbora" }, //... }
So far, ok. But flatMap particularly bothered me. There is a new Java API called mapMulti, which has this potential to multiply:
{ "dados": { "$strategy": "localpath", "cwd": "/home/jeffque/random-project-file", "dir": "../payloads/esses-daqui/top10-hard/" }, //... }
Beauty. I got it for DataLoader, but I also need to do the same thing for RuleLoader. Or maybe not? If you notice, there is nothing in this code that is specific to DataLoader. We can abstract this code!!
public DataLoader getDataLoader(Map<String, Object> inputDados) { final var strategy = (String) inputDados.get("$strategy"); return switch (strategy) { case "localpath" -> new LocalpathDataLoader(); case "sample" -> new SampleDataLoader(resourcePatternResolver_spring); case "athena-query" -> new AthenaQueryDataLoader(athenaClient, s3Client); default -> new AthenaQueryDataLoader(athenaClient, s3Client); } }
For purely utilitarian reasons, I placed this algorithm within the annotation:
final var dataLoader = getDataLoader(inputDados) dataLoader.loadData(inputDados, workingPath);
And for the facade? Well, the job is well to say the same. I decided to abstract this:
dataLoaderFacade.loadData(inputDados, workingPath);
And the facade looks like this:
@Service // para o Spring gerenciar esse componente como um serviço public class DataLoaderFacade implements DataLoader { public DataLoaderFacade(DataLoader primaryDataLoader, List<DataLoader> dataLoaderWithStrategies) { // armazena de algum modo } @Override public CompletableFuture<Void> loadData(Map<String, Object> input, Path workingPath) { return getDataLoader(input).loadData(input, workingPath); } private DataLoader getDataLoader(Map<String, Object> input) { final var strategy = input.get("$strategy"); // magia... } }
The above is the detailed content of Using annotations in Java to make a strategy. For more information, please follow other related articles on the PHP Chinese website!