search
HomeJavajavaTutorialUsing annotations in Java to make a strategy

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

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.

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);
    }
}
</string>

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);

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);
    }
}
</string>

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:

  • 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);

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...
    }
}
</string></string></void></dataloader>

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);
    }
}
</string>

Underneath the facade

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...
    }
}
</string></string></void></dataloader>

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!

Statement
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
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How can I use Java's RMI (Remote Method Invocation) for distributed computing?How can I use Java's RMI (Remote Method Invocation) for distributed computing?Mar 11, 2025 pm 05:53 PM

This article explains Java's Remote Method Invocation (RMI) for building distributed applications. It details interface definition, implementation, registry setup, and client-side invocation, addressing challenges like network issues and security.

How do I use Java's sockets API for network communication?How do I use Java's sockets API for network communication?Mar 11, 2025 pm 05:53 PM

This article details Java's socket API for network communication, covering client-server setup, data handling, and crucial considerations like resource management, error handling, and security. It also explores performance optimization techniques, i

How can I create custom networking protocols in Java?How can I create custom networking protocols in Java?Mar 11, 2025 pm 05:52 PM

This article details creating custom Java networking protocols. It covers protocol definition (data structure, framing, error handling, versioning), implementation (using sockets), data serialization, and best practices (efficiency, security, mainta

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software