Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Q: How to enforce singleton/get object handle between two separate processes?

Reply
Thread Tools

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

 
 
Jeff
Guest
Posts: n/a
 
      07-31-2003
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

===========================================
 
Reply With Quote
 
 
 
 
Shawn McDermott
Guest
Posts: n/a
 
      07-31-2003
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

Jeff wrote:
> 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
>
> ===========================================


 
Reply With Quote
 
 
 
 
A Bag Of Memes
Guest
Posts: n/a
 
      07-31-2003

"Jeff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.




 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-31-2003
On 30 Jul 2003 17:59:58 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Jeff) wrote or
quoted :

>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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Separate Tabs, Separate Sessions BigAndy Firefox 0 05-09-2007 09:27 AM
Separate Tabs, Separate Sessions BigAndy Firefox 0 05-09-2007 09:26 AM
Using separate classpaths for separate classes? Frank Fredstone Java 1 06-27-2006 06:46 AM
How to handle Header - Detail in two separate Datagrids aaa ASP .Net Web Controls 0 04-08-2005 03:49 PM
How to use several separate classes (separate files) to be executed in one class (another file) EvgueniB Java 1 12-15-2003 01:18 AM



Advertisments