简体   繁体   中英

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

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. Have to send it, then load it at runtime

Java is not like some (mostly interpreted) languages where code is data. 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. Apart from loading a class file, there is no other way to get new code into a running 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.

PPS: You could embed an interpreter in your application. Eg, you could embed JRuby, and then the client could send you Ruby code.

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