views:

433

answers:

11

I have a really simple Java class that effectively decorates a Map with input validation, with the obvious void set() and String get() methods.

I'd like to be able to effectively call those methods and handle return values and exceptions from outside the JVM, but still on the same machine Update: the caller I have in mind is not another JVM; thanks @Dave Ray

My implementation considerations are typical

  • performance
  • ease of implementation and maintenance (simplicity?)
  • reliability
  • flexibility (i.e. can I call from a remote machine, etc.)

Is there a 'right way?' If not, what are my options, and what are the pro/cons for each?

(Stuff people have actually done and can provide real-life feedback on would be great!)

+1  A: 

Will you be calling from another JVM-based system, or is the client language arbitrary? If you're calling from another JVM, one of the simplest approaches is to expose your object as an MBean through JMX. The canonical Hello World MBean is shown here. The pros are:

  • Really easy to implement
  • Really easy to call from other JVMs
  • Support for remote machines
  • jconsole allows you to manually test your MBean without writing a client

Cons:

  • Client has to be on a JVM (I think)
  • Not great for more complicated data structures and interactions. For example, I don't think an MBean can return a reference to another MBean. It will serialize and return a copy.
Dave Ray
thanks, the caller I have in mind is not another JVM but good answer I think
Brabster
A: 

JNI (Java Native Interface) allows access to java code from C or C++.

QuickRecipesOnSymbianOS
my class is in a running JVM; I want to call methods on the class from outside. I think JNI is a way to make native code usable from a Java class, so I don't think it's what I'm looking for...
Brabster
Ah? I thought it was the reverse. Ie. the Java program must initiate the communication/instantiate the native code.
PhiLho
The poster is correct, you can start a JVM and invoke Java classes from native code: http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html
McDowell
A: 

I have an Inno Setup script (installing a Java program) which calls some Java methods to perform some operations or check some conditions.
I (actually my predecessor) just instanciate java.exe on each call. Which is, obviously, costly, although not critical in my case (and the Windows cache kicks in, I suppose).

An alternative is to use some inter-language communication/messaging, your Java program acting as a server. Corba comes to mind, as it is language agnostic. But a bit heavy, perhaps. You can use sockets. RPC is another buzzword too, but I haven't much experience in the field.

PhiLho
+2  A: 

Since your callers are not Java apps and you're already foreseeing networked callers, RMI-IIOP (CORBA) might be an option. Though it's definitely not easy to implement, it has the advantage of being a widely-recognized standard.

Michael Borgwardt
+2  A: 

Since your caller is not JVM-based, this is a question of inter-process communication with JVM. The options I have in mind are:

  1. Communicate over a socket: make your JVM listen to incoming connections and caller send commands
  2. Communicate using shared files (caller writes to file, JVM polls and updates)
  3. Using JNI, start JVM inside a callers process and then use RMI/MBeans to communicate with the first ("server") JVM. Caller will have access to results using JNI

Option 3 IMO is the most "Java" way of doing this, and is the most complex/error-prone. Option 2 is ugly but simple Option 1 is moderately easy (java part) and otherwise ok.

Yoni Roit
+1  A: 

For ease of use, I would use Spring Remoting. If you are already using Spring in your project, that's a no brainer. If you arent ... well you should have a look anyway.

Spring provides an abstraction that allow you to switch remoting protocols easily. It supports the most widely deployed protocols (SOAP, Hessian, Burlap, RMI, ...). If you are calling from non Java code, Hessian has support in a number of other languages, is known to be more efficient than SOAP and easier than CORBA.

Guillaume
A: 

What you want is the Java Native Interface (JNI), despite the difficulties that it may present. There is no other equivalent technology that will be as easy to implement.

As mentioned in the comments for the preceding answer, the JNI is optimized for calling native code from Java, but it can also be used for the reverse with a little work. In your native code you'll need to implement the JNI entry point--something like SetMapPointer()--then call that function from the Java code once the Map is built. The implementation of SetMapPointer() should save the Java object pointer someplace accessible, then the native code can invoke Java methods on it as needed.

You'll need to make sure that this happens in the right order (i.e. the native code doesn't try to access the Map before it's been built and passed to native code), but that shouldn't be an especially hard problem.

JSBangs
+3  A: 

Ok. Here's another try now that I know the client is not Java. Since you want out-of-process access and possibly remote machine access, I don't think JNI is what you want since that's strictly in-process (and a total hassle). Here are some other options:

Raw Sockets : just set up a listener socket in Java and accept connections. When you get a connection read the request and send back a response. Almost every language can use sockets so this is a pretty universal solution. However, you'll have to define your own marshalling scheme, parsing, etc.

XML-RPC : this isn't as hip these days, but it's simple and effective. There are Java libraries as well as libraries in most other languages.

CORBA : as mentioned above, CORBA is an option, but it's pretty complicated and experts are getting harder to come by.

Web Server : set up an embedded web server in your app and handle reqests. I've heard good things about Jetty or you can use the one provided with Java. I've used the latter successfully to server KML files to Google Earth from a simulation written in Java. Most other languages have libraries for making HTTP requests. How you encode the data (XML, text, etc) is up to you.

Web Services : This would be more complicated I think, but you could use JAX-WS to expose you objects as web services. NetBeans has pretty nice tools for building Web Services, but this may be overkill.

Dave Ray
A: 

Another alternative to consider if the other process will be on the same machine and the OS is POSIX-compliant (not Windows) is Named Pipes.

The outside process writes the operations, as strings or some other agreed-upon byte encoding, to the named pipe while the Java application is reading from the pipe, parsing up the incoming operations and executing them against your object.

This is the same strategy that you would use for socket connections, just instead of a SocketInputStream you'd be reading from a FileInputStream that is attached to a named pipe.

A: 

An alternative to CORBA is ICE, unless the licence is a problem (it's GPL, but you can also buy a commercial licence).

It has pretty much all the benefits of CORBA, but ZeroC, the vendor, provides bindings for many different languages. CORBA vendors tend to only provide one or two language bindings, and then you start finding compatibility problems.

The documentation is also excellent. I wouldn't have said it was particularly easy to pick up, but probably easier than CORBA.

Otherwise, another option I don't think has been mentioned is the new middleware/RPC framework developed by Cisco, now donated to Apache, called Etch. It's still pretty new though, and documentation is sparse.

Mike Houston
A: 

Beanshell is a shell-like java interpreter that can be exposed over a network socket. Basically you do this from java:

i = new bsh.Interpreter();
i.set( "myapp", this );  // Provide a reference to your app
i.eval("server(7000)");

and then you do this from anywhere else:

telnet localhost 7001
myapp.someMethod();

This little utility does remote java invocations much more easily than JNI or RMI ever has.

For more, start at: http://www.beanshell.org/manual/remotemode.html

Travis Wilson