RESTful Timers in JavaEE

The EJB @Schedule annotation comes in handy in case you need to create timers automatically. One can use cron-like expression to configure the appropriate schedule. If you need more flexibility, the good old TimerService works like a charm.

Quick background

  • The TimerService interface was Introduced in EJB 2.1 [yeah .. J2EE days ! 😉 ]
  • Used to create Timer objects programmatically
  • Used to work in conjunction with an implementation of the TimedObject interface [pre EJB 3.0] to serve as a call back for timer triggers
  • Since EJB 3.0, the @Timeout annotation was used to mark a method in a (stateless/singleton/message driven) bean to act as the receiver of timer call backs from the EBJ container
  • Things were further improved in EJB 3.1 with the introduction of ScheduleExpression which allowed fine grained timer scheduling – this was the programmatic equivalent of @Schedule

EJB Timer related components (for quick reference)

RESTful Timers

One can easily expose a simple RESTful interface to work with EJB Timers. Actions such as creating timers, fetching timer details as well cancelling timers can be executed on the fly.

A simple implementation is available via this Github project. It’s a simple Java EE 7 Maven project built on Netbeans. You should be able to set it up easily.

Here is a gist

  • POST a request to schedule a timer (JSON/XML payload representing the schedule configuration)
@POST
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response schedule(@HeaderParam("name") final String timerName, final ScheduleConfiguration config) {
    auditScheduler.schedule(from(config), new TimerConfig(timerName, config.isPersistent()));
    return Response.created(UriBuilder.fromResource(AuditSchedulerResource.class).path(timerName).build(timerName)).build();
}
  • GET all active timers and their respective details (JSON/XML representation)
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getAllTimers() {
    List<String> ejbTimers = auditScheduler.getAllTimers();
    List<ScheduledTimerInfo> timers = ejbTimers.stream().map((id) -> auditScheduler.getTimerInfo(id)).collect(Collectors.toList()); 
   GenericEntity<List<ScheduledTimerInfo>> entities = new GenericEntity<List<ScheduledTimerInfo>>(timers) {};
    return Response.ok(entities).build();
}
  • GET information for a specific timer (JSON/XML representation)
@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getTimerInfo(@PathParam("id") String name) {
    ScheduledTimerInfo info = auditScheduler.getTimerInfo(name);
    return Response.ok(info).build();
}
  • DELETE (cancel) an existing timer
@DELETE
@Path("{id}")
public void cancel(@PathParam("id") String name) {
    auditScheduler.cancel(name);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s