本文介绍了Java实现来处理回调消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在代码中处理以下情况:

I have the following situation I need to handle in my code:

public class Class1 {

    IRequester requester;

    public Class1(Requester impl) {
        requester = impl;
    }

    public List doSomething() {
        requester.request1();  // sends messages to a set of nodes
        //do some more local processing
        list = requester.request2(); // sends some more messages and returns a list
        return list;
    }
}

在这种情况下,request1()向a发送请求节点集并返回一个结果,该结果将在本地用于更多处理,然后执行request2()并返回一个列表。这需要在doSomething()执行结束时返回。 request1()和request2()是通过类型为IRequester的请求者完成的。

In this case request1() sends a request to a set of nodes and returns a result which will be used locally for more processing, and then the request2() is made which returns a list. This needs to be returned at the end of execution of doSomething(). request1() and request2() are done through requester which is of type IRequester

public interface IRequester {

    request1();

    List request2();
}

现在request1()和request2()由实际执行的类实现请求。此类处理节点之间的通信。

Now request1() and request2() are implemented by the class which actually does the requests. This is the class that handles the communication between the nodes.

public NetworkManager implements IRequester {

    request1() {

        // Create an operation
        // Add callback to the operation
        // schedule operation
    }

    request2() {

    }
}

现在,我的问题是当我在此处实现request1()时,我需要创建一个将消息发送到节点的过程。此过程可以附加一个回调。当节点响应时,它返回结果。

Now, my issue is that when I implement request1() here in there I need to create a procedure which will send a message to the node. This procedure can have a callback attached. When the node responds it returns the result. How do I implement this such that it returns the result at the end of my request1?

推荐答案

自request1的返回类型以来,我该如何实现它以便在请求1的末尾返回结果? )是无效的,因此您无法从中返回价值。
但是在IRequester的实现类中,您可以传递resultObject,每当执行request1()方法时,
就会将结果存储在结果对象中,当您需要获取结果时,您可以可以从ResultObject获取它

Since the return type of request1() is void so you cannot return value from it .But in the implementation class of IRequester , you can pass a resultObject ,whenever the request1() method is execute it will store the result in the result Object, and when you need to get the result , you can get it from the ResultObject

class ResultObject{
getResult(); ///return result
setResult(); ///store result
}


public NetworkManager implements IRequester {

private ResultObject callBackResult;

public ResultObject getResult(){
return callBackResult;
}
public void setResult(ResultObject value){
this.callBackResult=value;
}

request1() {

    // Create an operation
    this.setResult(callProcedure());
    // schedule operation
}

request2() {

}

}

public class Main{


public static void main(String args){
IRequester r=new NetworkManger();
ResultObject res=new ResultObject();
r.setResult(res);
r.request1();
r.getResult();
r.request2();
}
 }

这篇关于Java实现来处理回调消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:55
查看更多