Giter Club home page Giter Club logo

idempotentizer's Introduction

CI pipeline codecov LICENSE

Idempotentizer

The Idempotentizer is a Java library designed to simplify the process of implementing idempotent behavior in your consumer applications. Idempotence is a crucial concept in distributed systems and APIs, ensuring that repeated requests have the same effect as the initial request, even if the request is executed multiple times.

Getting Started

To get started with Idempotentizer in your Java project, follow these simple steps:

  1. Add Idempotentizer to Your Project: Include Idempotentizer as a dependency in your project. You can either download the JAR file manually or use a dependency management tool like Maven or Gradle.

    <!-- Maven -->
    <dependency>
        <groupId>com.ersalgado</groupId>
        <artifactId>idempotentizer</artifactId>
        <version>1.0.0</version>
    </dependency>
    // Gradle
    implementation 'com.ersalgado:idempotentizer:1.0.0'
  2. Create an instance: Create an instance of Idempotentizer, providing one of the available repository implementations.

    // Create an instance of one of the provided repositories.
    Repository repository = new SQLRepository(datasource, new JacksonObjectSerde());
    
    // Instances of Idempotentizer are thread safe so you can
    // use this instance as a singleton in your application.
    Idempotentizer idempotentizer = new DefaultIdempotentizer(repository);
  3. Handle Idempotent Requests: Use Idempotentizer to check if incoming requests were already processed, ensuring that duplicate requests are detected and processed accordingly.

    private void doUpdateInventoryOnce(UpdateInventoryRequest request, UUID idempotencyKey) {
        var requestInfo = idempotentizer.checkProcessed(idempotencyKey, CONSUMER_ID);
        if (!requestInfo.getProcessed()) {
    
            // Process the request...
    
            // Mark the request as processed.
            idempotentizer.markAsProcessed(idempotencyKey, CONSUMER_ID);
        } else {
            log.debug("The request '{}' for consumer '{}' was already processed", idempotencyKey, CONSUMER_ID);
        }
    }

    You can store any result object generated after successfully processing a request to ensure consistency for all subsequent duplicate requests. This stored result can then be returned when duplicate calls are detected, maintaining the integrity of the system.

    private MyResult doUpdateInventoryOnce(UpdateInventoryRequest request, UUID idempotencyKey) {
        var requestInfo = idempotentizer.checkProcessed(idempotencyKey, CONSUMER_ID);
        if (!requestInfo.getProcessed()) {
            var result = // Process the request and get a result...
    
            // Mark the request as processed, providing a result.
            idempotentizer.markAsProcessed(idempotencyKey, CONSUMER_ID, result);
            
            return result;
        } else {
            log.debug("The request '{}' for consumer '{}' was already processed", idempotencyKey, CONSUMER_ID);
            return (MyResult) requestInfo.getReturnedValue();
        }
    }

Contributing

Contributions to Idempotentizer are welcome! If you encounter any issues, have ideas for improvements, or would like to contribute code, please submit a pull request or open an issue.

License

The Idempotentizer library is open-source software licensed under the MIT License.

idempotentizer's People

Contributors

3rs4lg4d0 avatar github-actions[bot] avatar dependabot[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.