Home > Java > javaTutorial > Using annotations in Java to make a strategy

Using annotations in Java to make a strategy

Susan Sarandon
Release: 2025-01-10 12:13:43
Original
271 people have browsed it

Usando annotations em Java para fazer um strategy

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.

How to implement?

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 that can handle it."

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:

  • validate potential deviations when updating the application
  • validate whether the changed rules maintain the same behavior
    • for example, optimizing rule execution time
  • check whether the change in rules generated the expected change in decisions
  • validate that the change in the application actually made it more efficient
    • for example, using the new version of GraalVM with JVMCI turned on is increasing the number of requests I can make?

So, for that, I need some strategies for the origin of events:

  • get the real data from S3
  • take the data that is as a sample within the repository and copy it multiple times
  • get the data from a specific location on my local machine

And I also need strategies that are different from my rules:

  • passed via input
  • uses the fast-running stub
  • uses a sample based on production rule
  • use this path here on my machine

How to deal with this? Well, let the user provide the data!

The API for Strategy

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": {
        //...
    }
}
Copy after login
Copy after login
Copy after login

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
    },
    //...
}
Copy after login
Copy after login
Copy after login

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"
    },
    //...
}
Copy after login
Copy after login
Copy after login

Or in the localpath?

{
    "dados": {
        "$strategy": "localpath",
        "cwd": "/home/jeffque/random-project-file",
        "dir": "../payloads/esses-daqui/top10-hard/"
    },
    //...
}
Copy after login
Copy after login
Copy after login

And so I can delegate to the selection of the strategy ahead.

Code review and the facade

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);
    }
}
Copy after login
Copy after login
Copy after login

So my architect asked two questions during the code-review:

  • "why do you instantiate everything and not let Spring work for you?"
  • he created a DataLoaderFacade in the code and abandoned it half baked

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.

Creating the façade object

What used to be:

final var dataLoader = getDataLoader(inputDados)
dataLoader.loadData(inputDados, workingPath);
Copy after login
Copy after login
Copy after login

Became:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://json-schema.org/draft/2020-12/schema",
    "$vocabulary": {
        //...
    }
}
Copy after login
Copy after login
Copy after login

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
    },
    //...
}
Copy after login
Copy after login
Copy after login

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"
    },
    //...
}
Copy after login
Copy after login
Copy after login

And I can have aliases too, why not?

{
    "dados": {
        "$strategy": "localpath",
        "cwd": "/home/jeffque/random-project-file",
        "dir": "../payloads/esses-daqui/top10-hard/"
    },
    //...
}
Copy after login
Copy after login
Copy after login

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);
    }
}
Copy after login
Copy after login
Copy after login

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);
Copy after login
Copy after login
Copy after login

It doesn't sound so natural in my opinion.

Okay, given that, we still need:

  • extract class annotation from passed objects
  • create a string map rightarrow data loader (or string rightarrow T)

Extracting the annotation and assembling the map

To extract the annotation, I need to have access to the object class:

dataLoaderFacade.loadData(inputDados, workingPath);
Copy after login
Copy after login

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...
    }
}
Copy after login
Copy after login

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 {
    // ...
}
Copy after login

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": {
        //...
    }
}
Copy after login
Copy after login
Copy after login

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
    },
    //...
}
Copy after login
Copy after login
Copy after login

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"
    },
    //...
}
Copy after login
Copy after login
Copy after login

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/"
    },
    //...
}
Copy after login
Copy after login
Copy after login

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);
    }
}
Copy after login
Copy after login
Copy after login

Underneath the facade

For purely utilitarian reasons, I placed this algorithm within the annotation:

final var dataLoader = getDataLoader(inputDados)
dataLoader.loadData(inputDados, workingPath);
Copy after login
Copy after login
Copy after login

And for the facade? Well, the job is well to say the same. I decided to abstract this:

dataLoaderFacade.loadData(inputDados, workingPath);
Copy after login
Copy after login

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...
    }
}
Copy after login
Copy after login

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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template