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
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); } }
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); } }
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 → data loader (or string → 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... } }
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); } }
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... } }
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The difference between HashMap and Hashtable is mainly reflected in thread safety, null value support and performance. 1. In terms of thread safety, Hashtable is thread-safe, and its methods are mostly synchronous methods, while HashMap does not perform synchronization processing, which is not thread-safe; 2. In terms of null value support, HashMap allows one null key and multiple null values, while Hashtable does not allow null keys or values, otherwise a NullPointerException will be thrown; 3. In terms of performance, HashMap is more efficient because there is no synchronization mechanism, and Hashtable has a low locking performance for each operation. It is recommended to use ConcurrentHashMap instead.

Java uses wrapper classes because basic data types cannot directly participate in object-oriented operations, and object forms are often required in actual needs; 1. Collection classes can only store objects, such as Lists use automatic boxing to store numerical values; 2. Generics do not support basic types, and packaging classes must be used as type parameters; 3. Packaging classes can represent null values ??to distinguish unset or missing data; 4. Packaging classes provide practical methods such as string conversion to facilitate data parsing and processing, so in scenarios where these characteristics are needed, packaging classes are indispensable.

StaticmethodsininterfaceswereintroducedinJava8toallowutilityfunctionswithintheinterfaceitself.BeforeJava8,suchfunctionsrequiredseparatehelperclasses,leadingtodisorganizedcode.Now,staticmethodsprovidethreekeybenefits:1)theyenableutilitymethodsdirectly

The JIT compiler optimizes code through four methods: method inline, hot spot detection and compilation, type speculation and devirtualization, and redundant operation elimination. 1. Method inline reduces call overhead and inserts frequently called small methods directly into the call; 2. Hot spot detection and high-frequency code execution and centrally optimize it to save resources; 3. Type speculation collects runtime type information to achieve devirtualization calls, improving efficiency; 4. Redundant operations eliminate useless calculations and inspections based on operational data deletion, enhancing performance.

Instance initialization blocks are used in Java to run initialization logic when creating objects, which are executed before the constructor. It is suitable for scenarios where multiple constructors share initialization code, complex field initialization, or anonymous class initialization scenarios. Unlike static initialization blocks, it is executed every time it is instantiated, while static initialization blocks only run once when the class is loaded.

Factory mode is used to encapsulate object creation logic, making the code more flexible, easy to maintain, and loosely coupled. The core answer is: by centrally managing object creation logic, hiding implementation details, and supporting the creation of multiple related objects. The specific description is as follows: the factory mode handes object creation to a special factory class or method for processing, avoiding the use of newClass() directly; it is suitable for scenarios where multiple types of related objects are created, creation logic may change, and implementation details need to be hidden; for example, in the payment processor, Stripe, PayPal and other instances are created through factories; its implementation includes the object returned by the factory class based on input parameters, and all objects realize a common interface; common variants include simple factories, factory methods and abstract factories, which are suitable for different complexities.

InJava,thefinalkeywordpreventsavariable’svaluefrombeingchangedafterassignment,butitsbehaviordiffersforprimitivesandobjectreferences.Forprimitivevariables,finalmakesthevalueconstant,asinfinalintMAX_SPEED=100;wherereassignmentcausesanerror.Forobjectref

There are two types of conversion: implicit and explicit. 1. Implicit conversion occurs automatically, such as converting int to double; 2. Explicit conversion requires manual operation, such as using (int)myDouble. A case where type conversion is required includes processing user input, mathematical operations, or passing different types of values ??between functions. Issues that need to be noted are: turning floating-point numbers into integers will truncate the fractional part, turning large types into small types may lead to data loss, and some languages ??do not allow direct conversion of specific types. A proper understanding of language conversion rules helps avoid errors.
