1

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

user2494863
  • 451
  • 7
  • 17

1 Answers1

1

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.

kaliatech
  • 17,579
  • 5
  • 72
  • 84
  • But doing this how I would check the result from my call? The method done has the aswer, how I call my client side with the result? – user2494863 Jun 19 '13 at 13:31
  • I think you are now also asking how to handle when the ThirdParty call is asynchronous. I added some additional notes to describe how that would be handled as well. – kaliatech Jun 19 '13 at 13:37
  • Yes, the ThirdParty is asynchronous. I just need to call a method from my client and wait the result from the ThirdParty.Do you think FutureTask is the best for my case? – user2494863 Jun 19 '13 at 13:55
  • If your Callback creates the asynchronous thread, then a FutureTask will probably work well. If the ThirdParty method creates the asynchronous thread internally, then you have multiple options. Check out this answer: http://stackoverflow.com/questions/4639853/sync-version-of-async-method . If you have more specific question on dealing with the asynchronous aspect, then I would recommend creating a new question. – kaliatech Jun 19 '13 at 14:11
  • I read some content and I found observer pattern, isn´t my case more about observer? The diference I saw is that in my case I receive a callback but the observer works with listeners... – user2494863 Jun 20 '13 at 17:32
  • Observer pattern is usually referenced in context of object oriented programming where objects need to notify/observe each other in the local memory space. Your original questions seem to be more about distributed communications and asynchronous handling. Observer pattern can be applied if using something like RMI, but there are many more aspects involved. – kaliatech Jun 20 '13 at 18:12