Code Monkey home page Code Monkey logo

sequence-rest-service's Introduction

Sequence Generator REST Service

This Spring Boot application uses Reactor framework to generate unique sequence numbers in concurrent environments.

This service can be used to generate customer, invoice, case numbers, etc. You can control the starting number and the format for each sequence.

In Grails applications you can use the plugin Restful Sequence Number Generator as a client. The Grails client will communicate with this service to get unique sequence numbers.

Why not use database sequences?

Although database sequences are fast they put restrictions on data types (i.e. require numbers). This service lets you use formatted sequence numbers, for example TICKET-1234 and you can also change the sequence number programatically. You could for example reset the sequence to start with YYYY0001 on the first of January every year.

Configuration

application.properties
    server.port=8082

    spring.datasource.url=jdbc:mysql://localhost/sequence_db
    spring.datasource.username=dbuser
    spring.datasource.password=dbpasswd
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.jpa.hibernate.ddl-auto=update

    sequence.user.password=password
    sequence.admin.password=password

Running

./gradlew bootRun

Initializing

Create a new sequence

The following request creates a new sequence called Customer that will start at 1001 and return numbers formatted as 01001.

curl --user admin:password -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"number":1001, "format":"%05d"}' http://localhost:8082/api/sequence/1/Customer

Get next number

The following three requests returns number 01001, 01002 and 01003.

curl -u user:password  http://localhost:8082/api/sequence/1/Customer/next
{ "number": "01001" }
curl -u user:password  http://localhost:8082/api/sequence/1/Customer/next
{ "number": "01002" }
curl -u user:password  http://localhost:8082/api/sequence/1/Customer/next
{ "number": "01003" }

Get status of a sequence

The following request returns the status of the Customer sequence without increasing the number.

curl -u user:password  http://localhost:8082/api/sequence/1/Customer
{
  "timestamp" : 1404149733472,
  "name" : "Customer",
  "format" : "%05d",
  "number" : 1004
}

Update a sequence

The following request updates an existing sequence called Customer so that the next number will be 5000. To reduce concurrency issues you must specify the current number AND the new number. If the current sequence number is not equal to the specified number, the update will not be applied.

curl --user admin:password -H "Accept: application/json" -H "Content-Type: application/json" -X PUT -d '{"current":1004, "number":5000}' http://localhost:8082/api/sequence/1/Customer
curl -u user:password  http://localhost:8082/api/sequence/1/Customer/next
{ "number": "05000" }

Delete a sequence

The following request deletes an existing sequence called Customer.

curl --user admin:password -H "Accept: application/json" -H "Content-Type: application/json" -X DELETE -d '{}' http://localhost:8082/api/sequence/1/Customer
curl -u user:password  http://localhost:8082/api/sequence/1/Customer
404 NOT FOUND

sequence-rest-service's People

Contributors

goeh 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.