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

Discussion in 'Java' started by Jeff, Jul 31, 2003.

  1. Jeff

    Jeff Guest

    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

    ===========================================
     
    Jeff, Jul 31, 2003
    #1
    1. Advertising

  2. Re: Q: How to enforce singleton/get object handle between two separateprocesses?

    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
    >
    > ===========================================
     
    Shawn McDermott, Jul 31, 2003
    #2
    1. Advertising

  3. Re: How to enforce singleton/get object handle between two separate processes?

    "Jeff" <> wrote in message
    news:...
    > 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.
     
    A Bag Of Memes, Jul 31, 2003
    #3
  4. Jeff

    Roedy Green Guest

    On 30 Jul 2003 17:59:58 -0700, (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.
     
    Roedy Green, Jul 31, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EvgueniB
    Replies:
    1
    Views:
    655
    Anthony Borla
    Dec 15, 2003
  2. Frank Fredstone
    Replies:
    1
    Views:
    460
    Jean-Francois Briere
    Jun 27, 2006
  3. Charles Fox (Sheffield)
    Replies:
    9
    Views:
    747
    Adam Skutt
    Feb 12, 2011
  4. aaa
    Replies:
    0
    Views:
    128
  5. Paul McMahon
    Replies:
    3
    Views:
    210
    David A. Black
    Jun 9, 2008
Loading...

Share This Page