See how Spring Python works with Jython

By Greg Turnquist

Greg is a member of the Spring team, an author of several books on Spring Boot, conference speaker, and the lead for Spring Data JPA.

October 15, 2009

I recently completed a patch that replaces the amara library with python’s default elementTree library. As much as I like amara’s API, its C-extension basis was unacceptable. I just merged the patch into the trunk, and verified it works with Jython 2.5.1.FINAL. When Spring Python’s 1.1.0.M2 release comes out, we will be Jython-compatible.

With this change, there were only a couple of tweaks to the rest of Spring Python.

What does this mean? How about looking at some code to see how we can start with a Java app, and migrate things over to Python. To kick this off, let’s start with an uber-contrived example Java app: a wiki engine. The wiki engine calls into a data access layer in order to look up some statistics on the site.

public interface DataAccess {

public void statistics();

}
public class MySqlDataAccess implements DataAccess {

public void statistics() {
System.out.println("MySQL: You are calling for some statistics.");
}

}
public class WikiService {

private DataAccess dataAccess;

public DataAccess getDataAccess() {
return this.dataAccess;
}

public void setDataAccess(DataAccess dataAccess) {
this.dataAccess = dataAccess;
}

public void calculateWikiStats() {
System.out.println("You are passing through a pure Java WikiService...");
dataAccess.statistics();
}

}

Let’s assume this is a Spring app. That means we need an XML application context. Let’s create one and call it javaBeans.xml.



xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">








We can start up our pure java app along with its context by writing jython script javabeans.py. It uses Spring Python’s SpringJavaConfig parser.

if __name__ == "__main__":
from springpython.context import ApplicationContext
from springpython.config import SpringJavaConfig
ctx = ApplicationContext(SpringJavaConfig("javaBeans.xml"))
service = ctx.get_object("wikiService")
service.calculateWikiStats()

We just need to compile the java code, and then run our script using jython.

gregturn@startrek:~$ javac *.java
gregturn@startrek:~$ jython javabeans.py
You are passing through a pure Java WikiService...
MySQL: You are calling for some statistics.
gregturn@startrek:~$

Okay, we’re off to a good start. We have tapped a running java system. For our next step, let’s write a pure python version of the data access code and put it into Py.py.

import DataAccess

class MySqlDataAccess(DataAccess):
def statistics(self):
print "PyMySQL: You are calling for some statistics."

class StubDataAccess(DataAccess):
def statistics(self):
print "PyStub: You are calling for some statistics."

Now, to really crank things up a bit, let’s migrate from the Spring Java XML configuration over to Spring Python’s pure Python container.

from springpython.config import PythonConfig, Object
from Py import MySqlDataAccess
import WikiService

class WikiProductionAppConfig(PythonConfig):
def __init__(self):
PythonConfig.__init__(self)

@Object
def data_access(self):
return MySqlDataAccess()

@Object
def wiki_service(self):
results = WikiService()
results.dataAccess = self.data_access()
return results

if __name__ == "__main__":
from springpython.context import ApplicationContext
ctx = ApplicationContext(WikiProductionAppConfig())
service = ctx.get_object("wiki_service")
service.calculateWikiStats()

The top half of our mixed.py script contains an IoC container, except there is no XML whatsoever. Instead, its raw python. You can see how we are also using our python version of MySqlDataAccess.

gregturn@startrek:~$ jython mixed.py
You are passing through a pure Java WikiService...
PyMySQL: You are calling for some statistics.
gregturn@startrek:~$

This shows that we are still using the pure java WikiService class, and it calls into our pure python MySqlDataAccess.

Another useful feature that Spring Python provides is easy access to Pyro, the Python Remote Objects library. This library is for making RPC calls from Python-to-Python. Spring Python makes it easy to link clients and services together with Pyro.

First, let’s take our wiki service, and export it with a server script.


from springpython.config import PythonConfig, Object
from springpython.remoting.pyro import *

from Py import MySqlDataAccess
import WikiService

class WikiProductionAppConfig(PythonConfig):
def __init__(self):
PythonConfig.__init__(self)

@Object
def data_access(self):
return MySqlDataAccess()

@Object
def wiki_service(self):
results = WikiService()
results.dataAccess = self.data_access()
return results

@Object
def exported_wiki_service(self):
return PyroServiceExporter(service=self.wiki_service(), service_name="wiki", service_port=9000)

if __name__ == "__main__":
from springpython.context import ApplicationContext
print "Starting up exported WikiService..."
ctx = ApplicationContext(WikiProductionAppConfig())

We made some slight tweaks to the IoC container by adding exported_wiki_service. It targets wiki_service, and links it with a name and port to expose it as a Pyro service. Spring Python by default will instantiate every object, so in this case, it creates the PyroProxyExporter. Because this isn’t the caller, we don’t need to fetch anything from the context.

Next, let’s code a client to call our service.


from springpython.config import PythonConfig, Object
from springpython.remoting.pyro import *

class WikiProductionAppConfig(PythonConfig):
def __init__(self):
PythonConfig.__init__(self)

@Object
def wiki_service(self):
results = PyroProxyFactory()
results.service_url="PYROLOC://localhost:9000/wiki"
return results

if __name__ == "__main__":
from springpython.context import ApplicationContext
ctx = ApplicationContext(WikiProductionAppConfig())
service = ctx.get_object("wiki_service")
service.calculateWikiStats()

Now, let’s start up the server.


gregturn@startrek:~$ jython mixed_with_pyro_server.py &
[1] 4953
Starting up exported WikiService...

The script is backgrounded, because it is running a Pyro daemon advertising our service. Now, let’s launch the client script.


gregturn@startrek:~$ jython mixed_with_pyro_client.py
You are passing through a pure Java WikiService...
PyMySQL: You are calling for some statistics.
gregturn@startrek:~$

As you can see, our client makes remote to the exported WikiService, which happens to be a pure java service. The java service then calls our python version of MySqlDataAccess to do its statistics call. This goes to show how easy Spring Python makes it mix and match Java and Python.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *