Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java program knows it is already running?

Options
  • 07-05-2007 6:37am
    #1
    Registered Users Posts: 21,264 ✭✭✭✭


    I want a java program to check to see if it is already running and then pass a variable onto the already running program.

    Only way I can think of this is to update a file and monitor it, but there must be a nicer way of doing this?


Comments

  • Closed Accounts Posts: 619 ✭✭✭Afuera


    Hobbes wrote:
    I want a java program to check to see if it is already running and then pass a variable onto the already running program.

    Only way I can think of this is to update a file and monitor it, but there must be a nicer way of doing this?
    JMX might be worth checking out to see if it matches your needs.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    The dirt simple way of doing it would be to use sockets. You could get your program to listen on the multicast channel, and to detect it you could send out a multicast message saying "here i am, here's my IP:Port, contact me if you're there".


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    I don't know anything about java but i guess you could use shared memory too?


  • Registered Users Posts: 995 ✭✭✭cousin_borat


    Maybe you could use some sort of Interceptor Class. This could Inject your Java Program' Setter method for the variable you want to change during runtime with the new value. This is part of the Spring Framework that can be used when designing Spring Beans in place of regular EJBs.

    Edit: It may also be a good idea to take a look at Java Reflection. It's a topic Ive been meaning to look at but haven't had time or a need yet.


  • Posts: 0 [Deleted User]


    A singleton class?

    For a database pool I did I wanted to make sure there was only 1 instance, i.e. that there was one pool running.

    The pool class had a private constructor and a public static method to grab the instance, so here's the rough skeleton.

    [PHP]
    public class Pool
    {
    private Pool()
    {
    // construct it
    }

    public static Pool getInstance()
    {
    // If the pool hasn't been initialised, do so
    if (this == null)
    this = new Pool(); // etc..
    else
    return this; // Already initialised, so return object ref.
    }

    }
    [/PHP]


  • Advertisement
  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Pure Java solutions by and large aren't going to be able to span JVM instances, which is what my understanding of the problem is. Either Hobbes original suggestion, Mutant_Fruit's suggestion, or something based around the RPC capabilities are probably the only ways to go about it.


  • Registered Users Posts: 995 ✭✭✭cousin_borat


    Pure Java solutions by and large aren't going to be able to span JVM instances,
    If it's a Java program looking at itself then would spanning JVM instances be an issue?


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    If it's a Java program looking at itself then would spanning JVM instances be an issue?
    No. There is one JVM instance per application instance.

    If you were to launch the same application twice, then you would have two JVM instances.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    I don't know what inter-app communication frameworks java has, hence the reason i suggested sockets. That's the easiest way i can think of to transfer data from one program to another.

    It's not ideal, but it's better than using a file as the temporary storage. If your program crashes, it's possible that the file will be left in a state which means you mightn't be able to start another instance of your program.


  • Registered Users Posts: 995 ✭✭✭cousin_borat


    No. There is one JVM instance per application instance.

    If you were to launch the same application twice, then you would have two JVM instances.
    I guess we're talking about two different concepts. In your case two running instances of the same application, whereas I mean an instance of the Application/Program that has in built reflection that allows it to hook into objects at run time.


  • Advertisement
Advertisement