简体   繁体   English

通过套接字发送可执行Java

[英]Sending Executable Java over a Socket

I'm working on a networking project in Java where I want to send some executable code from Node A to Node B and have Node B return the result of the executed code back to Node A. 我正在使用Java进行网络项目,我想将一些可执行代码从节点A发送到节点B,并使节点B将执行后的代码返回给节点A。

The way I went about trying to do this (Which I now know isn't suitable for my use case) was to define an abstract class called Job which had an abstract method run(). 我尝试执行此操作的方式(现在我知道这不适合我的用例)是定义一个名为Job的抽象类,它具有一个抽象方法run()。 It looks something like this: 看起来像这样:

import java.io.Serializable;

public abstract class Job implements Serializable {

  public String id;

  public Job(String id) {
      this.id = id;
  }

  public abstract void run();

}

I was then planning to be instantiate children classes from Job, defining the run method and to send the derived class from Node A to Node B using the built in ObjectOutputStream and ObjectInputStream and have simply have Node B call the run() method when it received a job. 然后,我打算从Job实例化子类,定义run方法,并使用内置的ObjectOutputStreamObjectInputStream从Node A将派生类发送到Node B,并让Node B在收到时简单地调用run()方法一份工作。

However this goes beyond what can be done with a Serialized Object sent though the ObjectOutputStream as Node B would need the exact definition of the class being sent from Node A - which it can't have as each Job will be of a different sub type and will have different definitions of the run() method. 但是,这超出了通过ObjectOutputStream发送作为对象B发送的序列化对象所无法完成的工作,因为它需要从节点A发送的类的确切定义-因为每个Job都具有不同的子类型,所以它不能具有该类的定义。将对run()方法有不同的定义。

Does anyone know of any other way to do this? 有人知道其他方法吗? A way to send executable code from Node A to Node B and have Node B execute the code? 一种将可执行​​代码从节点A发送到节点B并让节点B执行代码的方法?

I know I could probably just create .jars and send them through the OutputStream, and have Node B execute them. 我知道我可以只创建.jars并通过OutputStream发送它们,然后让Node B执行它们。 But if there is any way I could do this with having a base Job class that I can extend and instantiate from to define different types of Jobs and send those Jobs to Node B? 但是,如果有任何办法,我可以拥有一个基类Job类,可以对其进行扩展和实例化以定义不同类型的Jobs,并将这些Jobs发送到Node B,从而做到这一点?

Any help or suggestions would be greatly appreciated!! 任何帮助或建议,将不胜感激! Thanks in advance! 提前致谢! :) :)

I think you are right - this is not possible. 我认为您是对的-这是不可能的。 If you use object output streams, the name of the concrete class is part of the transferred operation; 如果使用对象输出流,则具体类的名称是已传输操作的一部分; so you need the class definition on the other side (because the JVM will want to load the .class file for the object you transferred). 因此,您需要在另一侧定义类(因为JVM将要为传输的对象加载 .class文件)。 So the .class file needs to exist on both sides. 因此,.class文件必须同时存在于两侧。

I don't see any other option than sending .class or .jar files and to use reflection to execute the corresponding code. 除了发送.class或.jar文件并使用反射来执行相应的代码,我看不到其他任何选择。

EDIT: but using reflection ... does still allow you to build different job classes on node A. It still makes sense to organize your classes in a meaningful way; 编辑:但是使用反射...仍然允许您在节点A上构建不同的作业类。以有意义的方式组织您的类仍然有意义; even when you have to transfer them first and then use reflection to invoke them. 即使您必须先传输它们,然后使用反射来调用它们。

您可以通过RMI和代码库功能完全做到这一点。

To add to EJP's answer, what you need is a dynamic networked class loading facility, which must be able to fetch the bytecode of classes that are not in the local classpath of your application. 为了增加EJP的答案,您需要的是动态的网络类加载工具,该工具必须能够获取不在应用程序本地类路径中的类的字节码。

Yes, the codebase feature of an RMI Server should allow you to do that, provided the location of the jars can be expressed as a URL and you have a server infrastructure (or URL handler) to serve it. 是的,只要罐子的位置可以表示为URL,并且您具有服务于它的服务器基础结构(或URL处理程序),RMI Server的代码库功能就应该允许您这样做。

I also know of two open source grid computing frameworks which provide this kind of facility implicitely and transparently for the API users: JPPF and GridGain . 我也知道两个开源网格计算框架,它们为API用户隐式和透明地提供了这种功能: JPPFGridGain

Disclaimer: I'm the main JPPF developer. 免责声明:我是JPPF的主要开发人员。

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

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