thundr-deferred

Deferred task execution for Thundr

License

License

GroupId

GroupId

com.threewks.thundr
ArtifactId

ArtifactId

thundr-deferred
Last Version

Last Version

0.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

thundr-deferred
Deferred task execution for Thundr
Project URL

Project URL

http://github.com/kuhnza/thundr-deferred/
Project Organization

Project Organization

3wks
Source Code Management

Source Code Management

https://github.com/kuhnza/thundr-deferred

Download thundr-deferred

How to add to project

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

Dependencies

compile (4)

Group / Artifact Type Version
com.threewks.thundr : thundr jar 0.9.11
com.threewks.thundr : thundr-quartz jar 0.0.1
com.google.guava : guava jar 15.0
com.amazonaws : aws-java-sdk jar 1.6.4

provided (1)

Group / Artifact Type Version
javax.servlet : servlet-api jar 2.5

test (6)

Group / Artifact Type Version
org.powermock : powermock-core jar 1.5.1
org.powermock : powermock-module-junit4 jar 1.5.1
org.powermock : powermock-api-mockito jar 1.5.1
junit : junit jar 4.11
org.hamcrest : hamcrest-all jar 1.3
org.mockito : mockito-all jar 1.9.5

Project Modules

There are no modules declared in this project.

Thundr Deferred

Deferred task execution for Thundr

Build Status

Why

Here are a few reasons why you might want to use Thundr Deferred:

  1. Execute long running tasks asynchonously (e.g. send an email, run a report)
  2. Distribute load across a pool of worker machines
  3. Retry tasks that fail

Dependencies

Thundr Deferred depends on the Thundr Quartz module for its queue monitor implementation.

Usage

In your ApplicationModule:

@Override
public void requires(DependencyRegistry dependencyRegistry) {
    super.requires(dependencyRegistry);

    dependencyRegistry.addDependency(QuartzModule.class);
    dependencyRegistry.addDependency(DeferredModule.class);
}

Take care to add the DeferredModule entry after the Thundr Quartz entry however otherwise you'll get an error when starting your application complaining that QuartzScheduler isn't in your injection context.

With the module added you'll now have access to the DeferredTaskService from your injection context which means it will be injected into any object that declares it as a dependency in its constructor.

Imagine now we've got a controller which subscribes users to a newsletter. Once subscribed we want to immediately send the user a welcome email. This operation is a bit slow to do synchronously within the request so let's defer it.

Here's our controller:

import com.threewks.thundr.deferred.DeferredTaskService;

public class MyController {
  private DeferredTaskService deferredTaskService;
  
  public MyController(DeferredTaskService deferredTaskService) {
    this.deferredTaskService = deferredTaskService;
  }
  
  public JspView subscribe(String firstName, String lastName, String email) {
    // Perhaps do some databasey stuff to create the user etc
    
    // Run our asynchronous email task
    deferredTaskService.defer(new SendWelcomeEmailTask(firstName, lastName, email));
    
    // Return a success page to the user
    return new JspView("success.html")
  }
}

As you can see it's as simple as creating a new task object and passing it to .defer().

So what does our task object look like? Well tasks are pretty simple things to make. All that's required is that you implement the DeferredTask interface like so:

import com.threewks.thundr.deferred.task.DeferredTask;

public class SendWelcomeEmailTask implements DeferredTask {
  private String firstName;
  private String email;
  
  public SendWelcomeEmailTask(String firstName, String email) {
    this.firstName = firstName;
    this.email = email;
  }
  
  public void run() {
    sendEmail(email, "Welcome!", "Hi " + firstName + ",\nWelcome to our service :-)");  
  }
  
  private void sendEmail(String to, String subject, String body) {
    // Some nasty Java Mail API code here...
  }
}

Hopefully you can see that it's really easy to create new tasks and defer them.

How it works

"What is this dark magic?" you ask? Behind the scenes Thundr Deferred makes use of message queues to serialize task requests and their attached data. A queue monitor (powered by Thundr Quartz) polls the queue at a defined interval (defaults to 5 seconds) for new tasks to process. Since quartz is triggering processing of new tasks, tasks are run inside quartz' thread pool (i.e. outside of the current request).

Another upside of using a queue is that task processing can very easily be distributed amongst a cluster of nodes. Provided that the queue is external (i.e. not in memory) nodes will compete to pull messages off the queue in a first come, first serve fashion.

A note on serialization

Thundr Deferred makes use of the GSON library for serializing and deserializing tasks on and off the queue. Consequently you should check out the GSON usage docs if you have any questions about what can and can't be serialized.

For convenience we have included date/time convertors for proper serialization of Joda DateTime objects.

Queue Monitors

There is only one queue monitor supported: Thundr Quartz. Should you require something different raise a pull request or a ticket. In the event that you choose to implement your own this can be configured via the deferredQueueMonitor property in your application.properties file like so:

deferredQueueMonitor=com.threewks.thundr.deferred.QuartzQueueMonitor

Queue Providers

At present two queue providers are included:

  • In memory
  • Amazon Simple Queue Service

Support for additional queue providers is planned however this will be subject to demand. If you have a specific need please feel free to send a pull request or raise a ticket.

The queue provider implementation can be configured via the deferredQueueProvider property in your application.properties file like so.

deferredQueueProvider=com.threewks.thundr.deferred.provider.InMemoryQueueProvider

In memory queue provider

For simplicity sake this is the default queue provider. You probably shouldn't use this for all but the most trivial use cases however as messages posted to this queue are only as reliable and persistent as the node its running on. Also you lose any potential benefits relating to distributed task execution as memory tasks can only be read by the local node.

Amazon Simple Queue Service

As the name suggests this queue provider integrates with Amazon's Simple Queue Service (SQS).

Configuration

The SQS queue provider supports the following additional configuration items in your application.properties

  • deferredSqsAccessKey - your AWS API access key
  • deferredSqsSecretKey - your AWS API secret key
  • deferredSqsRegion - The region your queue is deployed in (e.g. us-east-1)
  • deferredSqsQueueName - Your queue name (defaults to thundr-deferred-[env] where env is your environment name)

Versions

Version
0.0.2
0.0.1