简体   繁体   中英

How can we initiate two instances of same Java class in two different processes?

I create a POJO class. I want to create two processes which gets two instances of the POJO class in another class.

The two processes should communicate with each other.

class Message {

String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}



 public class Demo {

        public static void main(String[] args) {

        Demo  demo = new Demo();

        Message initiator = null;
        Message receiver = null;        

       // Here is where I am stuck up. The below line is not correct
       // as some command is expected here I believe.  

        ProcessBuilder p1 = new ProcessBuilder(initiator).start();

       // Objects initiator and receiver should be instantiated in separate
       // processes and their methods should inter communicate.
       // what am I doing wrong?
       // Is there any other way to do this?
    }

}

I tried to use ProcessBuilder, Runtime.exec and Process syntaxes but it all mentions to use commands when creating process

for example:

ProcessBuilder p1 = new ProcessBuilder(some commands).start;

How can this instantiation in two different processes can be achieved?

I referred several threads related to this Process creation but none of them has solutions to my requirement.

Any help would be appreciated.

ProcessBuilder helps launch a new process. The command you give it is much the same as you type in at the command line. If that's a java app you'd use something like 'java -jar mayapp.jar'. Once the app is launched it will be running in a separate process, so also a separate JVM (if it's a java app). If you need to communicate to that separate process after you have started it then there are a number of options including.

  • Java Remote method invocation (RMI)
  • Java Message Service (JMS)
  • Http using Restful calls

There are plenty of examples of using these technologies so have a look.

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