简体   繁体   English

从客户端到实现线程事件的服务器端应用程序调用方法的最佳实践是什么?

[英]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. ServerSide.save()并传递了我的CallBack实现,但是我不想从客户端看到第三方库,当我更改第三方库时,我需要对这种情况保持透明。

What is the best practice to do so? 最佳做法是什么?

Tks TKS

Create a custom intermediary object that holds all information needed to create your custom Callback implementation. 创建一个自定义中介对象,其中包含创建自定义回调实现所需的所有信息。 It would resemble a simple DTO. 它类似于一个简单的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. 在服务器上,将中间对象转换为您的Callback实现,然后再调用ThirdParty。

It's not clear from your question if your ThirdParty library call is asynchronous. 从您的问题尚不清楚您的ThirdParty库调用是否异步。 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. 如果希望ServerSide.save调用是同步的,并且ThirdParty库调用是异步的,那么您将希望阻塞服务器线程并等待ThirdParty结果再返回。 One way of doing that would be to use Java 6/7's FutureTask class. 一种方法是使用Java 6/7FutureTask类。

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. 如果您不希望ServerSide.save调用是同步的,则可能需要让客户端轮询服务器以在另一个服务器调用中寻找结果,或者设计双向通信。 Two-way communication is usually much more complex though, and heavily dependent on whatever platform/protocol/technology you are using. 但是双向通信通常要复杂得多,并且在很大程度上取决于您使用的平台/协议/技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自客户端或服务器的时间戳-最佳做法是什么? - Timestamp from the client or the server - What's the best practice? 在客户端更新Web应用程序的最佳实践(GWT) - Best practice to update web application on client side (GWT) 在WEB API服务器和Android客户端应用程序之间共享模型的最佳实践是什么 - What is the best practice for sharing models between a WEB API server and an Android client application 将CSS编译到SWF服务器端Java,最佳实践是什么? - Compiling CSS to SWF server side Java, What is the best practice? 客户端服务器最佳实践 java - Client server best practice java GWT从服务器向客户端发送大量数据的最佳实践 - GWT Best practice to send huge amount of data from server to client 从客户端浏览器读取/写入HSQL的最佳实践是什么? - What is the best practice to read/write to HSQL from a client browser? 从控制器到应用程序线程的Javafx调用方法 - Javafx call method from controller to application thread 从父类 object 调用子类方法的最佳实践 - Best practice to call childclass method from parentclass object 从服务器端 html 按钮调用客户端 java 方法? - calling a client side java method from server side html button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM