呼叫球衣服务 (PUT) 时发送电子邮件通知的设计问题

发布于 2024-11-26 14:54:10 字数 880 浏览 1 评论 0原文

我这里有一个设计问题。我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬心 2024-12-03 14:54:10

使“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.

上课铃就是安魂曲 2024-12-03 14:54:10

在我拥有 Web 服务的类中,我有一个内部类,它将在单独的线程中执行。

 class MailExecutorService implements Runnable {

    EventiAnomali eventiAnomali;
    JSONObject jsonEntity;
    NotificationType.TicketType type;

    public MailExecutorService(EventiAnomali eventiAnomali,JSONObject jsonEntity,
                               NotificationType.TicketType type) {
        this.eventiAnomali = eventiAnomali;
        this.jsonEntity =jsonEntity;
        this.type = type;
    }

    public void run() {
        notifyUsers(eventiAnomali,jsonEntity,type); // I've made notifyUser asynchronous
    }
}

在我使用的 return 之前的 @PUT 方法中,

 executorService.execute(
 new MailExecutorService(evento,jsonEntity,
       (evento.getStatoevento().equals("5"))? NotificationType.TicketType.CLOSED:
       NotificationType.TicketType.UPDATED));

 response = MY_JSON

我将 executor.shotDown() 放入我的 finally 块中。然后最后一行是返回的 JSON (响应)

这些都可以吗?更不用说它有效..

In the class where I have my web service I have this inner class which will executes in seperate thread.

 class MailExecutorService implements Runnable {

    EventiAnomali eventiAnomali;
    JSONObject jsonEntity;
    NotificationType.TicketType type;

    public MailExecutorService(EventiAnomali eventiAnomali,JSONObject jsonEntity,
                               NotificationType.TicketType type) {
        this.eventiAnomali = eventiAnomali;
        this.jsonEntity =jsonEntity;
        this.type = type;
    }

    public void run() {
        notifyUsers(eventiAnomali,jsonEntity,type); // I've made notifyUser asynchronous
    }
}

In the @PUT method just before the return I've used,

 executorService.execute(
 new MailExecutorService(evento,jsonEntity,
       (evento.getStatoevento().equals("5"))? NotificationType.TicketType.CLOSED:
       NotificationType.TicketType.UPDATED));

 response = MY_JSON

I've put executor.shotDown() in my finally block. Then the last line is the retured JSON (response)

Are all these okay to do? Not to mention it works ..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文