简体   繁体   English

如何在Java中将方法或对象从客户端传递到服务器?

[英]How do I pass a method or object from client to server in Java?

I have multiple clients and one server. 我有多个客户端和一台服务器。 Each client has a few commands at their disposal, eg: 每个客户端可以使用一些命令,例如:

public interface Command {
    public int run();
}

public class CommandOne implements Command {
    // ...
}

public class CommandTwo implements Command {
    // ...
}

These commands can be executed within the client (on their machine) - or they can be "passed" to the server to be executed, and then a result will be returned. 这些命令可以在客户端(在其计算机上)中执行-或可以将它们“传递”给服务器以执行,然后将返回结果。

What is the simplest way of implementing both the client and server within Java? 在Java中同时实现客户端和服务器的最简单方法是什么?

The following are what I've come up with: 以下是我想出的内容:

  • Serialize the command, pass it to the server, have the server deserialize it, run the command and return the result. 序列化命令,将其传递给服务器,让服务器反序列化,运行命令并返回结果。 (This won't work because methods cannot be serialized.) (这无法使用,因为方法无法序列化。)
  • Pass a the command's name (as a String) to the server, have the server run a switch statement, instantiate a new command of the same type, run it, and return the result. 将命令的名称(作为字符串)传递给服务器,让服务器运行switch语句,实例化相同类型的新命令,运行该命令,然后返回结果。 This means if I ever add a new command class, I will have to update both the client and the server. 这意味着,如果我添加了新的命令类,则必须同时更新客户端和服务器。
  • Serialize the necessary method logic into some structure (JSON, XML, etc.), pass it to the server, have the server deserialize it into a custom class, run the method logic, and then return the result. 将必要的方法逻辑序列化为某种结构(JSON,XML等),将其传递给服务器,让服务器将其反序列化为自定义类,运行方法逻辑,然后返回结果。 This seems like overkill and requires writing a DSL. 这似乎有些矫kill过正,需要编写DSL。

Is there something I'm missing? 有什么我想念的吗?

EDIT 编辑

Each request must be threaded in some way - because multiple clients could communicate with the server at the same time. 每个请求必须以某种方式进行线程化-因为多个客户端可以同时与服务器通信。 This is probably not an issue with the above problem, though. 但是,这可能不是上述问题的问题。

In an ugly world, the simple thing you could do is make your server expose an interface like 在一个丑陋的世界中,您可以做的简单事情就是使您的服务器公开一个类似于

/commands?run=CommandOne

and pass some parameters (in body or query string) to hold command parameters. 并传递一些参数(在正文或查询字符串中)以保存命令参数。

Then use some HTTP client to submit an HTTP request to that URI. 然后,使用一些HTTP客户端向该URI提交HTTP请求。 The server would instantiate and dispatch some command to execute what you wanted. 服务器将实例化并分派一些命令以执行所需的操作。

In a more structured world, you should look into RPC calls or the RESTful architectural style. 在更加结构化的世界中,您应该研究RPC调用或RESTful体系结构样式。

One solution I found really elegant and fun. 我发现一种解决方案非常优雅且有趣。

If you open a port and have a listener that is just waiting for a "Runnable" to come in, you can execute the run method. 如果您打开端口并且有一个监听器正在等待“ Runnable”的进入,则可以执行run方法。 The variables will all be passed over and whatever the run method does will be done. 变量将全部传递出去,并且无论run方法做什么都将完成。

Even a little better have a "runOnServer()" and "runOnClient()" method. 更好的是使用“ runOnServer()”和“ runOnClient()”方法。 the client creates the object, passes it to the server and goes on it's way, the server executes the runOnServer() method (against the variables since they were serialized over) then the class is automatically serialized back to the client where the runOnClient() stuff is executed. 客户端创建对象,将其传递给服务器,然后继续运行,服务器执行runOnServer()方法(针对变量,因为它们已经被序列化了),然后该类自动序列化回客户端,其中runOnClient()东西被执行了。

Makes it really slick to execute something on the server. 使得在服务器上执行某些操作确实很灵活。

If you want to get more advanced, look into Hazlecast, it does this already. 如果您想更高级,请研究Hazlecast,它已经做到了。

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

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