ka3ak
ka3ak

Reputation: 3181

REST service and scheduled message sending

I'm working on a client-server application using Jersey as JAX-RS implementation. I use the code below for handling user requests.

@Path("service")
public class SomeRestService {

    @POST
    @Path("good")
    @Consumes(MediaType.APPLICATION_XML)
    @Produces(MediaType.APPLICATION_XML)
    public Resp getCardRequest(Req request) {
        String result = "Request has been received: " + request;
        Response.status(201).entity(result).build();

                // Here it becomes clear that the service will have to send an email to [email protected] with some content on 17.01.2019 at 14:00 for example
                ...

             Resp response = ...

       return response;
    }

As far as I know every user request triggers a creation of thread on server side. After the response has been returned, the thread terminates. So If I have to send an email to the customer in a month or at specific date and time, I have to schedule it somehow. I thought about storing the required information in the database and run unix utilities like cron, or at to query the corresponding tables and send emails if needed. Not sure whether it's the best way for doing such a task. How is it done usually? I was told that in the future there may be other notification channels like sms, whatsapp, viber etc.

Upvotes: 1

Views: 169

Answers (1)

Andrei Ciobanu
Andrei Ciobanu

Reputation: 12840

One option that I've used in the past was Quartz Framework.

I would give a read to the CronTrigger Tutorial.

For example you define a JOB:

public class SimpleJob implements Job {
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        // send email code
    }
}

And you write code to trigger it every Friday at noon or every weekday at 9:30 am:

CronTrigger trigger = TriggerBuilder.newTrigger()
  .withIdentity("trigger3", "group1")
  .withSchedule(CronScheduleBuilder.cronSchedule("0 0/2 8-17 * * ?"))
  .forJob("myJob", "group1")
  .build();

Upvotes: 1

Related Questions