Elefantenstark for JAVA

A postgres advisory lock based worker queue.

License

License

Categories

Categories

Ant Build Tools Net
GroupId

GroupId

net.andreaskluth
ArtifactId

ArtifactId

elefantenstark
Last Version

Last Version

0.1-RELEASE
Release Date

Release Date

Type

Type

jar
Description

Description

Elefantenstark for JAVA
A postgres advisory lock based worker queue.
Project URL

Project URL

https://github.com/AndreasKl/elefantenstark
Source Code Management

Source Code Management

http://github.com/AndreasKl/elefantenstark/tree/master

Download elefantenstark

How to add to project

<!-- https://jarcasting.com/artifacts/net.andreaskluth/elefantenstark/ -->
<dependency>
    <groupId>net.andreaskluth</groupId>
    <artifactId>elefantenstark</artifactId>
    <version>0.1-RELEASE</version>
</dependency>
// https://jarcasting.com/artifacts/net.andreaskluth/elefantenstark/
implementation 'net.andreaskluth:elefantenstark:0.1-RELEASE'
// https://jarcasting.com/artifacts/net.andreaskluth/elefantenstark/
implementation ("net.andreaskluth:elefantenstark:0.1-RELEASE")
'net.andreaskluth:elefantenstark:jar:0.1-RELEASE'
<dependency org="net.andreaskluth" name="elefantenstark" rev="0.1-RELEASE">
  <artifact name="elefantenstark" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.andreaskluth', module='elefantenstark', version='0.1-RELEASE')
)
libraryDependencies += "net.andreaskluth" % "elefantenstark" % "0.1-RELEASE"
[net.andreaskluth/elefantenstark "0.1-RELEASE"]

Dependencies

test (5)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-engine jar 5.3.1
org.junit.jupiter : junit-jupiter-params jar 5.3.1
org.postgresql : postgresql jar 42.2.4
com.opentable.components : otj-pg-embedded jar 0.12.0
org.slf4j : slf4j-simple jar 1.7.25

Project Modules

There are no modules declared in this project.

elefantenstark

Build Status Maven Central codecov

Elefantenstark is a PostgreSQL powered worker queue for Java 8. It uses Postgres advisory locks to lock work while being worked on.

The lock can be tweaked to solve requirements regarding the ordering of how work has to be processed. E.g. all work items sharing the same key must be processed one after another.

Initializing the database tables

The Initializer creates the default queue table and indexes.

withPostgres(
    connection -> {
      new Initializer().build(connection);
      validateTableIsAvailable(connection);
    });

Producing work

Producer is thread safe and can be used to produce a WorkItem to the queue.

withPostgresAndSchema(
    connection -> {
      WorkItem workItem = WorkItem.groupedOnKey("_test_key_", "_test_value_", 0);
      new Producer().produce(connection, workItem);

      WorkItem queuedWorkItem = queryForWorkItem(connection);

      assertEquals(workItem, queuedWorkItem);
    });

Consuming work

A Consumer can be sessionScoped (tied to the current connection) or transactionScoped (creates a transaction). The session scoped can modify state that is not rolled back when the consuming function fails, however can cause unreleased locks when the application is killed. These are cleaned up when the OS decides to close the network connection.

withPostgresAndSchema(
    connection -> {
      Producer producer = new Producer();
      Consumer consumer = Consumer.sessionScoped();
      producer.produce(connection, WorkItem.groupedOnKey("a", "b", 23));

      Optional<String> next = consumer.next(
          connection,
          workItemContext -> {
            capturedWork.set(workItemContext);
            return "OK+";
          });
    });

Builds

Snapshot builds are available on Sonatype Snapshots, release builds are available on maven central.

<dependencies>
  <dependency>
    <groupId>net.andreaskluth</groupId>
    <artifactId>elefantenstark</artifactId>
    <version>0.1-RELEASE</version>
  </dependency>
</dependencies>

Versions

Version
0.1-RELEASE