How to write Python middleware for Java/Scala backend? How to connect Java and Python?

697 Views Asked by At

Let's say I've got a Java/Scala backend and I want to develop middleware in Python for it. I have no experience with developing middleware, but I presume it means that from within Python I would need to issue commands to the running Java/Scala backend programs, and in turn receive feedback from that backend. For this to happen I need to interface the two languages and there are several options for interfacing Python and Java. According to these pages there are some options:

  • JPI
  • JNI
  • JPype
  • Jepp
  • Jython
  • JCC

Unfortunately, JPI, JNI, JPype and Jepp do not seem to be actively developed anymore (last update more than a year ago). Jython is still active, but I read that it is not possible to import all Python modules from within Jython. JCC seems to be some kind of C++ layer between Java and Python, which also seems counter intuitive.

I've also found some questions on SO about the topic, but most seem more than 3 years old, which in internet/Python land is of course a long long time.

Since I've got some experience with the ZeroMQ messaging library I could use that, but I'd have to adjust the Java programs for it (which wouldn't be that big of a trouble, but if there's a better solution..).

So my question is: taking into account that I have control over a Java/Scala backend, what is currently the best way to write a Python middleware layer for that backend?

1

There are 1 best solutions below

1
On

You haven't given a lot of details about what you're trying to do. If your middleware runs in a separate process then it probably won't make a lot of difference what language your middleware is written in as long as they can talk to each other—and you can do that with ZMQ as you suggested.

If you're actually just making calls into Java/Scala libraries from a layer you're wanting to write in Python, I'd go with Jython. "Not being able to load all the modules" is only an issue if you were planning on using some of the modules that aren't available. Even for those modules that aren't available, you should be able to find suitable Java substitutes you can call.

Finally, if you really want to directly interface between Python and Java, you could look into Babel. I know some people who have used it for interfacing C, Java and Python programs successfully, and it seemed to work quite well for them.