I am having a design issue here. I have a RESTful web service implemented with jersey. I am also using spring mail to send email.
My specification needs to notify a certain group of users (15-20 approx.) that a PUT request has been made and inform them with the modification of a resource. Also I need to send a JSON array to the client who made the request as a response.
Right now what I have done is after the modification in my method where I process the PUT service,I send emails to the users and then I return the JSON status.
But why should the client wait for the mail notification to finish?What if my suer list is long and clients wait a long time to get a response. It is not his headache. But also I need to notify users too within the PUT method. If I return JSON I lost the context to send notification by getting out from the request response cycle.
Is there a better way to do that?
To have an idea,I am providing the relevant part of my code:
//first notify users
notifyUsers(event,jsonEntity,NotificationType.TicketType.UPDATED);
//then return response
return new JSONObject()
.put("response_code:", Response.status(Status.OK).build())
.put("title", evento.getDescrizioneevento())
.put("status", getEventAsString(evento.getStatoevento()))
.put("solved", evento.getCausascatenante());
Make “notifyUsers” asynchronous.
Queue up the request, and process it later. You can use a simple BlockingQueue in a singleton with a thread listening to the other end, or you can set up a more formal queuing system such as JMS. You could also insert the request in to a database and have something listening for activity there.
Or you could simply fire up a simple thread right there, and let it run as you return from the request. That can work too.
All sorts of alternatives with different pros and cons.