呼叫球衣服务 (PUT) 时发送电子邮件通知的设计问题
我这里有一个设计问题。我有一个使用 jersey 实现的 RESTful Web 服务。我也使用 spring mail 来发送电子邮件。
我的规范需要通知某个用户组(大约 15-20 个)已发出 PUT 请求,并通知他们资源的修改。另外,我需要将 JSON 数组发送给发出请求的客户端作为响应。
现在我所做的是修改处理 PUT 服务的方法后,我向用户发送电子邮件,然后返回 JSON 状态。
但是为什么客户端要等待邮件通知完成?如果我的用户列表很长并且客户端等待很长时间才能得到响应怎么办?这不是他头疼的事。但我也需要在 PUT 方法中通知用户。如果我返回 JSON,我就会失去通过退出请求响应周期来发送通知的上下文。
有更好的方法吗?
为了有一个想法,我提供了我的代码的相关部分:
//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());
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());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使“notifyUsers”异步。
将请求排队,稍后处理。您可以在单例中使用简单的 BlockingQueue,并用线程侦听另一端,或者您可以设置更正式的排队系统,例如 JMS。您还可以将请求插入数据库并监听其中的活动。
或者您可以简单地在那里启动一个简单的线程,并让它在您从请求返回时运行。那也可以。
各种替代方案都有不同的优点和缺点。
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.
在我拥有 Web 服务的类中,我有一个内部类,它将在单独的线程中执行。
在我使用的 return 之前的 @PUT 方法中,
我将
executor.shotDown()
放入我的finally
块中。然后最后一行是返回的JSON
(响应)这些都可以吗?更不用说它有效..
In the class where I have my web service I have this inner class which will executes in seperate thread.
In the @PUT method just before the return I've used,
I've put
executor.shotDown()
in myfinally
block. Then the last line is the returedJSON
(response)Are all these okay to do? Not to mention it works ..