简体   繁体   English

java:通过套接字发送方法

[英]java: Send method through socket

I'm trying to write a program that would send a function to several nodes, those nodes will receive the function and execute it, replying a result (through serialization). 我正在尝试编写一个程序,该程序将向多个节点发送函数,这些节点将接收该函数并执行该函数,并(通过序列化)回复结果。 I've searched around a bit, but found nothing of help. 我搜索了一下,但没有任何帮助。 An alternative might be to send a .class file over the socket and load it on the node, but i'm interested if anyone has found a simpler way 一种替代方法可能是通过套接字发送.class文件并将其加载到节点上,但是我很感兴趣是否有人找到了一种更简单的方法

Edit: I'm trying to make a cluster with this client/server. 编辑:我正在尝试与此客户端/服务器的群集。 And I want the client to always run on the nodes and accept any kind of work without having to recompile the client node code 而且我希望客户端始终在节点上运行并接受任何类型的工作,而不必重新编译客户端节点代码

Solution: seems there's no way around not sending a .class file. 解决方案:似乎没有办法不发送.class文件。 Have to send it, then load it at runtime 必须发送它,然后在运行时加载它

Java is not like some (mostly interpreted) languages where code is data. Java与某些将代码作为数据的语言(大多数是解释性语言)不同。 In order to do what you want with java source code, you would have to send the source code over the wire, compile it to a class file at the other end, and then load the class file. 为了使用Java源代码执行所需的操作,您必须通过网络发送源代码,将其编译到另一端的类文件,然后加载该类文件。 Apart from loading a class file, there is no other way to get new code into a running JVM. 除了加载类文件之外,没有其他方法可以将新代码获取到正在运行的JVM中。

PS: Opening up a port on your machine where you accept arbitrary code, and then execute whatever the client sends to you is like owning a Jewelry store, and leaving it un-locked and un-staffed every day when you go out for lunch. PS:在您的机器上打开一个端口,您可以在其中接受任意代码,然后执行客户端发送给您的任何操作,就像拥有一家珠宝店一样,每天出去吃午餐时,请保持解锁状态并没有人手。

PPS: You could embed an interpreter in your application. PPS:您可以在应用程序中嵌入解释器。 Eg, you could embed JRuby, and then the client could send you Ruby code. 例如,您可以嵌入JRuby,然后客户端可以向您发送Ruby代码。

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

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