Question

I have a Perl script that uses Inline::Java and just has to fork (it is a server and I want it to handle multiple connections simultaneously).

So I wanted to implement this solution which makes use of a shared JVM with SHARED_JVM => 1. Since the JVM is not shutdown when the script exits, I want to reuse it with START_JVM => 0. But since it might just be the first time I start the server, I would also like to have a BEGIN block make sure a JVM is running before calling use Inline.

My question is very simple, but I couldn't find any answer on the web: How do I simply start a JVM? I've looked at man java and there seems to be just no option that means "start and just listen for connections".

Here is a simplified version of what I'm trying to do in Perl, if this helps:

BEGIN {
    &start_jvm unless &jvm_is_running;
}

use Inline (
    Java       => 'STUDY',
    SHARED_JVM => 1,
    START_JVM  => 0,
    STUDY      => ['JavaStuff'],
);

if (fork) {
    JavaStuff->do_something;
    wait;
}
else {
    Inline::Java::reconnect_JVM();
    JavaStuff->do_something;
}

What I need help with is writing the start_jvm subroutine.

Était-ce utile?

La solution

If you've got a working jvm_is_running function, just use it to determine whether Inline::Java should start the JVM.

use Inline (
    Java       => 'STUDY',
    SHARED_JVM => 1,
    START_JVM  => jvm_is_running() ? 0 : 1,
    STUDY      => ['JavaStuff'],
);

Autres conseils

Thanks to details provided by tobyink, I am able to answer my own question, which was based on a the erroneous assumption that the JVM itself provides a server and a protocole.

As a matter of fact, one major component of Inline::Java is a server, written in Java, which handles request by the Inline::Java::JVM client, written in Perl.

Therefore, the command-line to launch the server is:

$ java org.perl.inline.java.InlineJavaServer <DEBUG> <HOST> <PORT> <SHARED_JVM> <PRIVATE> <NATIVE_DOUBLES>

where all parameters correspond to configuration options described in the Inline::Java documentation.

Therefore, in my case, the start_jvm subroutine would be:

sub start_jvm {
    system
      'java org.perl.inline.java.InlineJavaServer 0 localhost 7891 true false false';
}

(Not that it should be defined: tobyink's solution, while it did not directly address the question I asked, is much better.)

As for the jvm_is_running subroutine, this is how I defined it:

use Proc::ProcessTable;

use constant {
    JAVA          => 'java',
    INLINE_SERVER => 'org.perl.inline.java.InlineJavaServer',
};

sub jvm_is_running {
    my $pt = new Proc::ProcessTable;
    return grep {
        $_->fname eq JAVA && ( split /\s/, $_->cmndline )[1] eq INLINE_SERVER
    } @{ $pt->table };
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top