在java中实现同步网络调用等待ack消息?

发布于 2024-07-21 06:43:37 字数 224 浏览 3 评论 0原文

您将如何在 Java 中实现相当于等待完成的方法调用?

基本上我想做的是使用一种方法,通过网络向服务器应用程序发送消息,并等待它从服务器获取适当的 Ack 消息,其中包含操作成功完成或操作失败和错误消息。

我的方法已经有一个非阻塞版本,它传递一个回调类,其中有一个在回调时调用的方法。

构造一个回调类将其传递给我之前的方法然后执行等待操作并在回调上让该类执行通知是否有意义?

How would you go about implementing the equivellent of a wait until complete method call in Java?

Basically what I want to do is hava a method that sends a message over the network to a server application and waits till it gets the appropriate Ack message from the server with either an operation completed successfully or operation failed and an error message.

I already have a non blocking version of my method which passes a callback class in which has a method that is called on callback.

would it make sense to construct a callback class pass it in to my previous method and then perform a wait operation and on the callback have that class do a notify?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

旧城空念 2024-07-28 06:43:37

将 .wait() 添加到回调类将是最简单的路径。 它可以在某个标志上旋转,直到您的回调切换它为止,只需确保在循环中包含 Thread.yield() 或 .sleep() 即可。

无需重写替代的阻塞通信通道。

Adding a .wait() to your callback class would be the easiest path. It can sit there spinning on some flag until your callback switches it, just be sure to include Thread.yield() or .sleep() in your loop.

Saves having to rewrite an alternative blocking comms channel.

安静被遗忘 2024-07-28 06:43:37

简短回答:是的。 我已经热衷于使用类库中的内容,您所描述的听起来很像观察者模式,假设您正在使用 J2SE,您已经拥有观察者/可观察接口。

编辑:我的咖啡出了点问题:)显然,我的意思是检查 java.util.concurrent 包中的内容,特别是 Future<> 。 和 ExecutorService 类。

Short answer: Yes. I'm a fan of using what's in the class library already, what you describe sounds a lot like an Observer pattern, for which you have the Observer/Observable interfaces already, assuming you're using J2SE.

EDIT: Something was wrong with my coffee :) Obviously, what I meant to say was check out what's in java.util.concurrent package, specifically the Future<> and ExecutorService classes.

夏九 2024-07-28 06:43:37

是的,按照您的建议使用现有的方法。 在同步方法调用中创建回调,该回调将通过 wait()/notify() 与同步方法调用协调。 同步方法将调用异步方法,然后调用 wait()。 当回调被调用时,它会调用notify()来唤醒sync方法,以便它可以返回结果。

Yes, use your existing method like you are suggesting. Create the callback in your sync method call that will coordinate with the sync method call via wait()/notify(). The sync method will call the async one and then wait(). When the callback is called, it will call notify() to wake up the sync method so it can return the results.

落花随流水 2024-07-28 06:43:37

如果您不使用 JMS,请忽略。

如果您使用 JMS,那么您可以使用 QueueRequestor,它是请求回复集成模式的实现。

也就是说,即使使用异步消息,以下调用对于客户端来说也是同步的:

QueueRequestor requestor = null;
try {
    requestor = new QueueRequestor(session, queue); 
    Message response = requestor.send(request);
} finally {
    if (requestor == null) {
        try {
            requestor.close();
        } catch (JMSException e) {
            // log error message
        }
     }
}

Please ignore if you're not using JMS.

If you are using JMS, then you could use a QueueRequestor, which is an implementation of the Request Reply integration pattern.

That is, the following call appears synchronous to the client, even though asynchronous messages are used:

QueueRequestor requestor = null;
try {
    requestor = new QueueRequestor(session, queue); 
    Message response = requestor.send(request);
} finally {
    if (requestor == null) {
        try {
            requestor.close();
        } catch (JMSException e) {
            // log error message
        }
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文