Run java program with object model as parameter

The server listens on the port, waiting for incoming requests from clients (in fact, the client is ejb). These queries pass the complete object model as a parameter (for example, a list of employees, each employee a list of tasks, etc.).

Now the server should run another java program in a new JVM instance on the same computer with this object model as a parameter.

This other java program should be a standalone java program, but I cannot pass the object model as a parameter to the main method ( void main(String[] args) ).

So what to do? I am looking for a โ€œsimpleโ€ solution, for example. preferably without a database or file to save. The stand-alone program is really intense and cannot be hosted by the application server.

thanks.

0
source share
6 answers

Launch the application and record its input / output streams, and then pass through it a serialized model of the object. The new application should deserialize the input coming from System.in.

Concept example (I just wanted to make sure my example works, sorry for the delay):

 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(); } } } 

Edit: Added cyclic version. Please note that there should be a trick in OOS, as it immediately starts reading the transmitted stream (and blocks your application. If you do this at the wrong step, it should be wrapped AFTER the first object is sent to the child of the process).

+4
source

You can serialize the object and write it to the file system. Then you can read this serialized object at runtime. Alternatively, you can use JMS to transfer this object as a stream of bytes.

More on serialization .

Learn more about JMS . I have always used ActiveMQ, an Apache project for my JMS needs. It is easy to use and highly scalable. Oh yes, and it's free.

+1
source

Take a look at RMI . Your server can start a new service, which, in turn, publishes and exports itself to the local registry. Then your new service can get the object model as a parameter to the remote extension method. It should be very simple, does not require access to files or additional services, and no more heavy than any other answer.

+1
source

One way is that the original JVM opens the socket for listening, and a new instance of the JVM launches a program that connects to this socket and receives the parameters through the socket (Java default serialization will probably be good enough). Thus, you only need to pass the socket port number as a parameter to main, and no files / databases are needed. The same method can also be used to transfer other commands between the JVMs.

0
source

In any case, you cannot pass the object model as it exists in memory. If you want to convey an object model, you will need to find a way to encode and decode this model into something that you can transfer.

Since data is already being sent via EJB, serializing objects is an obvious choice, although there is nothing wrong with XML, JSON, etc.

You also need to get the object from your server program for the newly called JVM. If the object model is small enough, you can pass the serialized object to a java program as a command line argument. You can also, as George IV suggested, write it to the file system, and then tell your new application where to find the file containing the serialized data. You can use JMS. Web Services Sockets.

But if your server application is already based on EJB, the simplest thing is to get your new application to invoke at the EJB level as an EJB client to request the data you are looking for.

0
source

The serialization approach is good. In addition, you need a root object to transfer some references to the corresponding call objects. What I did in the past was to serialize a complete graph of objects and allow the root object to implement some interface, for example Runnable , and then on the receiving node, pass this root object to Runnable and execute run() on it.

0
source

All Articles