Running a PyAMF WSGI server in Apache Ant using Jython

Apache Ant is a Java-based build tool. Ant build files are created using XML but it also support Python scripts using Jython.

There are 2 ways to execute Python scripts in Ant:

JSR-223 enables dynamic languages to be callable via Java in a seamless manner. Unfortunately this is currently only supported with Jython 2.2.1 and not the latest 2.5.0. Since PyAMF only supports Jython 2.5 or newer, you have to use the PythonInterpreter class directly. This style of embedding code is very similar to making use of a scripting engine, but it has the advantage of working with Jython 2.5.

Here you see the gateway running in Ant:

Buildfile: build.xml clean: compile: [mkdir] Created dir: build/classes [javac] Compiling 1 source file to build/classes jar: [mkdir] Created dir: build/jar [jar] Building jar: build/jar/HelloWorld.jar run: [java] *sys-package-mgr*: processing new jar, 'build/jar/HelloWorld.jar' [java] Running AMF gateway on http://localhost:8000

Read the complete howto, including a sample server/client, on http://pyamf.org/tutorials/jython/ant.html.

Using PyAMF with Apache Tomcat

Today a new tutorial was added that shows you how to deploy your PyAMF applications with Apache Tomcat and modjy. modjy is an implementation of a WSGI compliant gateway/server for Jython, built on Java/J2EE servlets. This allows you to run Jython WSGI applications inside a Java/J2EE servlet container, e.g. Apache Tomcat. modjy is part of the Jython project.

Tomcat PyAMF

PyAMF server/client using Jython with Swing

Now that the upcoming Jython 2.5 is getting closer to it's final release, I thought it would be nice to create an example that shows PyAMF running on top of Jython. Jython is an implementation of the high-level, dynamic, object-oriented language Python written in 100% Pure Java, and seamlessly integrates with the Java platform. It thus allows you to run Python scripts on any Java platform.

Below you see a screenshot of the example, which was created using Java's native Swing UI toolkit. It allows you to start a simple AMF server on localhost:8000 and embeds an AMF client that makes calls to that server. This particular example doesn't interact with an Adobe Flash Player application, it simply shows it's possible to run PyAMF servers and clients on top of the upcoming Jython release.

Screenshot of Jython/PyAMF example

You can browse or download the source from the Subversion repository. You can find instructions on how to run the example yourself in the documentation.

Getting started with Jython and Apache Ant

Today I decided to introduce some Jython in one of our build scripts at work because it was simply too painful to use a pure Ant script. It would/ve been nice to avoid the Jython dependency but after screwing around with Ant for a few hours I decided to write a Python script to do the job.

Although the Ant documentation for the Script task is pretty good it wasn't dead simple to hook up Jython to Ant, and because I couldn't find any examples online I decided to create a simple example so others might benefit from it. All files (except for the .jar's) are available in the sandbox of the PyAMF repository.

Steps:

  1. Download Jython and the JSR 223 scripting engine .jar files for Jython
  2. Externalize some paths in a build.properties file (optional)
  3. Create a Python test script that prints out some property defined in the Ant build file
  4. Create the Ant build.xml file that defines a Script task and includes the build.properties file

Run the Ant script on the console and it should output something like:

Buildfile: build.xml jython: [script] Apache Ant told Jython: Hello World! BUILD SUCCESSFUL Total ...