Q: How to enforce singleton/get object handle between two separate processes?

J

Jeff

Hi, we are trying to implement the following:

Conditions: Both processes on same machine.

Process A - starts up independently, testfile CTGrabber.java
attempts to get the CTTest singleton object via class static method
and utilize accessor methods. Example could be a GUI process.

Process B - starts up independently, testfile CTTest.java starts up
on cmd line. Example could be a data gathering process.

Result: Process A is unable to get a handle to Process B object
(retrieveHandle function returns null).

Q: Is there a simple way of getting a handle to a object (in
another process) other than using RMI or ? The classfile is
accessible to both. We don't even have to control the creation, just
getting a handle is the main requirement.

I have enclosed copies of our test programs. However, I believe that
there must be a another way fundamentally to do this (ex. worst case,
somehow push reference externally out to file, read,etc.).

We are open to any suggestions that do not increase complexity or
force us to implement messaging software.

Thank you very much,
jeff

Test Code follows:
===========================================

<File: CTGrabber.java>

package CI;

import CI.*;

public class CTGrabber
{
public CTGrabber() {}

public static void main(String[] args)
{
CTTest test = CTTest.getCTTestHandle();
System.out.println(test);
System.out.println(test.getName());

}
} // END Class CombatGrabber

=============================================

<File: CTTest.java>

package CI;
public class CTTest
{
public static CTTest com;
public String name;

public static void main(String[] args)
{
if (args.length < 1)
{
System.out.println("--- Error \n Usage: java CTTest
<name>");
System.exit(0);
}
com = new CTTest();
com.setName(args[0]);
System.out.println("name is " + com.name + ", waiting....");
try{
Thread.sleep(60000);
}
catch (Exception e){
System.out.println("Exception E caught.");
}
}
private CTTest()
{
com = this;
name = new String("THIS DIDN'T WORK");
}
public static CTTest getCTTestHandle()
{
if (com != null)
return com;
else
return new CTTest();
}
public String getName(){
return name;
}
public void setName(String namestr){
name = namestr;
}
} // END class CTTest

===========================================
 
S

Shawn McDermott

remember, each process is going to be in its own jvm. This makes it so
you can't get a handle to an object without using rmi.

Shawn
 
A

A Bag Of Memes

Jeff said:
Hi, we are trying to implement the following:

Conditions: Both processes on same machine.

Process A - starts up independently, testfile CTGrabber.java
attempts to get the CTTest singleton object via class static method
and utilize accessor methods. Example could be a GUI process.

Process B - starts up independently, testfile CTTest.java starts up
on cmd line. Example could be a data gathering process.

Result: Process A is unable to get a handle to Process B object
(retrieveHandle function returns null).

Q: Is there a simple way of getting a handle to a object (in
another process) other than using RMI or ?

No. Each Java object exists inside a single VM.
The classfile is
accessible to both. We don't even have to control the creation, just
getting a handle is the main requirement.

I have enclosed copies of our test programs. However, I believe that
there must be a another way fundamentally to do this (ex. worst case,
somehow push reference externally out to file, read,etc.).

We are open to any suggestions that do not increase complexity or
force us to implement messaging software.

How do you expect different processes to communicate without some form of
messaging?
Thank you very much,
jeff

Test Code follows:
===========================================

<File: CTGrabber.java>

package CI;

import CI.*;

public class CTGrabber
{
public CTGrabber() {}

public static void main(String[] args)
{
CTTest test = CTTest.getCTTestHandle();
System.out.println(test);
System.out.println(test.getName());

}
} // END Class CombatGrabber

=============================================

<File: CTTest.java>

package CI;
public class CTTest
{
public static CTTest com;
public String name;

public static void main(String[] args)
{
if (args.length < 1)
{
System.out.println("--- Error \n Usage: java CTTest
<name>");
System.exit(0);
}
com = new CTTest();
com.setName(args[0]);
System.out.println("name is " + com.name + ", waiting....");
try{
Thread.sleep(60000);
}
catch (Exception e){
System.out.println("Exception E caught.");
}
}
private CTTest()
{
com = this;
name = new String("THIS DIDN'T WORK");
}
public static CTTest getCTTestHandle()
{
if (com != null)
return com;
else
return new CTTest();

You return a CTTest without assigning it to com. I doubt that's what you
meant to do.

The most reliable way to implement singletons in Java is like this:

class SomeSingleton {
private static SomeSingleton theOnlyInstance = new SomeSingleton();

public static SomeSingleton getInstance() {
return theOnlyInstance;
}
}

theOnlyInstance is guaranteed to be initialized when the class is loaded.
Your solution above isn't thread safe.
 
R

Roedy Green

We are open to any suggestions that do not increase complexity or
force us to implement messaging software.

What happens if you exec the two from a common JVM? What if you run
them both in the same JVM on two threads? Just use a kicker that
starts a thread that invokes the two main methods.


If you literally want to share objects, the only way I know to do it
is via RMI. You can pass a serialiased object down a raw socket, but
it has no connection with the original. It is a pure copy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
Martindap

Latest Threads

Top