io.stardog.dropwizard.worker:dropwizard-worker

Simple asynchronous processing for Dropwizard, using SQS

License

License

Categories

Categories

DropWizard Container Microservices
GroupId

GroupId

io.stardog.dropwizard.worker
ArtifactId

ArtifactId

dropwizard-worker
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

io.stardog.dropwizard.worker:dropwizard-worker
Simple asynchronous processing for Dropwizard, using SQS
Project URL

Project URL

https://github.com/stardogventures/dropwizard-worker
Source Code Management

Source Code Management

http://github.com/stardogventures/dropwizard-worker/tree/master

Download dropwizard-worker

How to add to project

<!-- https://jarcasting.com/artifacts/io.stardog.dropwizard.worker/dropwizard-worker/ -->
<dependency>
    <groupId>io.stardog.dropwizard.worker</groupId>
    <artifactId>dropwizard-worker</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.stardog.dropwizard.worker/dropwizard-worker/
implementation 'io.stardog.dropwizard.worker:dropwizard-worker:0.2.0'
// https://jarcasting.com/artifacts/io.stardog.dropwizard.worker/dropwizard-worker/
implementation ("io.stardog.dropwizard.worker:dropwizard-worker:0.2.0")
'io.stardog.dropwizard.worker:dropwizard-worker:jar:0.2.0'
<dependency org="io.stardog.dropwizard.worker" name="dropwizard-worker" rev="0.2.0">
  <artifact name="dropwizard-worker" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.stardog.dropwizard.worker', module='dropwizard-worker', version='0.2.0')
)
libraryDependencies += "io.stardog.dropwizard.worker" % "dropwizard-worker" % "0.2.0"
[io.stardog.dropwizard.worker/dropwizard-worker "0.2.0"]

Dependencies

compile (4)

Group / Artifact Type Version
io.dropwizard : dropwizard-core jar 1.3.5
com.amazonaws : aws-java-sdk-sqs jar 1.11.437
redis.clients : jedis jar 2.9.0
com.google.auto.value : auto-value-annotations jar 1.6.2

provided (1)

Group / Artifact Type Version
com.google.auto.value : auto-value jar 1.6.2

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.mockito : mockito-core jar 2.23.0

Project Modules

There are no modules declared in this project.

dropwizard-worker

by Ian White, Stardog Ventures (@eonwhite)

This is a simple way to run asynchronous, decentralized jobs from a Dropwizard service.

This is particularly useful for crons scheduled with Amazon CloudWatch, as described in this great post by Dan McKinley (@mcfunley):

https://blog.skyliner.io/a-simple-pattern-for-jobs-and-crons-on-aws-2f965e43932f

But this worker also should be suitable for busy event-processing queues as well.

The basic pattern is:

  • Create any number of jobs you want to run. They need to implement Consumer<Map<String,Object>>, where the Map represents parameters to your method.
class MyCron implements Consumer<<Map<String,Object>> {
    public void consume(Map<String,Object> params) {
        // do something interesting here
    }
}
  • Build a WorkerMethods object that contains your list of WorkMethods available. Each WorkMethod gets a unique name for the method and a consumer. The names are important, since you'll use those names to call the method remotely.

If you have some really simple methods, you may want to consider inlining them as lambdas.

WorkerMethods methods = WorkerMethods.of(ImmutableList.of(
        WorkMethod.of("cron", new MyCron()),
        WorkMethod.of("ping", params -> System.out.println("pong"))
));
  • Configure a WorkerManager with your configuration, and the worker you are using for processing messages (normally SqsWorker)
env.lifecycle().manage(new WorkerManager(
        "worker",
        WorkerConfig.builder().maxThreads(20).build(),
        new SqsWorker(sqsClient, "queue-name", env.metrics()),
        env.metrics());
  • Send to the queue (from anywhere in your distributed system, such as for example CloudWatch) messages in the format:
  {"method":<method>}
  or
  {"method":<method>,"params":<params>}
  or
  {"method":<method>,"params":<params>,"at":<millis>}

Where method is the name of the method, params is an object containing your parameters, and at is a millisecond timestamp of the time the message was queued.

  • You can also send messages with a SqsSender like so:
SqsSender sender = new SqsSender(sqsClient, "queue-name")

Features:

  • Scales up for busy queues, launching threads and polling as quickly as possible, but automatically scaling down for infrequently used queues to save money and resources.
  • Builtin metrics for monitoring the state of your queue.
  • Includes a Dropwizard Task to run methods manually.

Timezone-aware crons

While it's generally better to schedule crons in UTC time, there are situations where you want a cron to run at a time in a local timezone (e.g. "9 am Eastern time"), adjusting for Daylight Savings time.

CloudWatch however does not support timezone-aware cron scheduling.

So the solution is:

  • Wrap your cron in a TimeZoneCron like so:
     TimeZoneCron.of(new MyCron(), ZoneId.of("America/New_York"))
  • TimeZoneCron will check for a dst property in the params. If dst is set to true, the cron will only run when in Daylight Savings Time, and if dst is false, the cron will only run when not in Daylight Savings Time.
  • Schedule two otherwise identical CloudWatch SQS events at the two potential times (e.g. 9am Eastern time could be either 13:00 UTC or 14:00 UTC), one with "dst":true and one with "dst":false
  • TimeZoneCron will run your cron only once, depending on whether it is currently DST or not.
  • The above could get screwy if you schedule your cron in the wee hours of the morning during the DST changeover period, so don't do that.

Redis PubSub

dropwizard-worker can send and receive messages in the same format using Redis PubSub via Jedis.

This can be useful instead of SQS in situations where you require multiple-subscriber behavior -- for example to have your appservers subscribe to a cache invalidation mechanism.

Because the Jedis client handles its own polling, there is no need to use the WorkerManager with Redis. You can instantiate RedisWorker to consume pubsub, and RedisSender to send messages.

See the RedisExample example for how this works.

io.stardog.dropwizard.worker

Stardog Ventures

Versions

Version
0.2.0
0.1.1
0.1.0