服务器监听端口,等待客户端的传入请求(实际上,客户端是ejb)。这些请求将完整的对象模型作为参数传递(例如,雇员列表,每个雇员列表,任务列表等)。
现在,服务器必须使用该对象模型作为参数,在同一台计算机上的新JVM实例中启动另一个Java程序。
这个其他的Java程序应该是一个独立的Java程序,但是我无法将对象模型作为参数传递给main方法(void main(String[] args)
)。
那么该怎么办?我正在寻找“简单”的解决方案,例如最好没有数据库或持久性文件。
独立程序确实占用大量CPU,并且不能由应用服务器托管。
谢谢。
最佳答案
运行该应用程序并捕获其输入/输出流,然后通过它流化序列化的对象模型。新应用程序应反序列化来自System.in的输入。
这个概念的示例(我只是想确保我的示例可以正常工作,对不起,抱歉):
package tests;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class AppFirst {
public static void main(String[] args) throws Exception {
ProcessBuilder pb = new ProcessBuilder("java", "-cp",
"./bin", "tests.AppSecond");
pb.directory(new File("."));
pb.redirectErrorStream(true);
Process proc = pb.start();
ObjectOutputStream out = new ObjectOutputStream(
proc.getOutputStream());
ObjectInputStream oin = null;
for (int i = 0; i < 1000; i++) {
out.writeObject("Hello world " + i);
out.flush();
if (oin == null) {
oin = new ObjectInputStream(proc.getInputStream());
}
String s = (String)oin.readObject();
System.out.println(s);
}
out.writeObject("Stop");
out.flush();
proc.waitFor();
}
}
package tests;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class AppSecond {
public static void main(String[] args) throws Exception {
ObjectInputStream oin = null;
ObjectOutputStream out = new ObjectOutputStream(System.out);
while (true) {
if (oin == null) {
oin = new ObjectInputStream(System.in);
}
String s = (String)oin.readObject();
if ("Stop".equals(s)) {
break;
}
out.writeObject("Received: " + s);
out.flush();
}
}
}
编辑:添加了循环版本。请注意,OOS需要一个技巧,因为它会立即开始读取传入的流(并阻塞您的应用程序,如果操作步骤错误-应该在第一个对象发送给子对象之后将其包装处理)。