简体   繁体   中英

What is the best practice to call a method from client to a server side application that implements a thread event?

I have this situation:

A third-party library that has a method with an abstract class as parameter:

On server side I could call this third-party method with

    public void save(){
    ThirdParty.doSomethingBackground(new Callback() {
    public void done(Exception e) {
    //SOMETHING TO DO
    }
    });
    }

I could call this method on client doing something like:

ServerSide.save() and passing my implementation of CallBack, but I don´t want to see Third-party library from my client, I need to do this transparent for the situation when I change my third-party library.

What is the best practice to do so?

Tks

Create a custom intermediary object that holds all information needed to create your custom Callback implementation. It would resemble a simple DTO. If you are using some sort of remoting, then the object would be serializable.

Pass this intermediary object from client to server. On the server, transform the intermediary object to your Callback implementation before making the call to the ThirdParty.

It's not clear from your question if your ThirdParty library call is asynchronous. If you want the ServerSide.save call to be synchronous, and the ThirdParty library call is asynchronous, then you would want to block the server thread and wait on the ThirdParty result before returning. One way of doing that would be to use Java 6/7's FutureTask class.

If you did not want the ServerSide.save call to be synchronous, then you would either need to have the client poll the server looking for the result in another server call, or devise two-way communication. Two-way communication is usually much more complex though, and heavily dependent on whatever platform/protocol/technology you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM