lunes, 3 de octubre de 2011

Using Embed Jetty and BlazeDS Remoting with Adobe AIR

When we need to communicate with server side data from an Adobe AIR application we usually use HTTP services, Web Services or Remoting this works great, but what happens when we need to create an standalone application? We have options like merapi, flerry and transmission...

I want to show you how to embed Jetty Server in an Adobe Air application and use BlazeDS for communication. The use case is simple access a MySQL server directly from Adobe AIR but with this option you might experiment and create better use cases for this.

We need to download the following:
  • Jetty library (i use jetty-all-7.4.0.RC0.jar)
  • Servlet api (i use servlet-api-2.5.jar)
  • Mysql Connector
  • BlazeDS 4

Let's coding:

First we create a simple table for sample purposes:

Java side:
First we create a simple Java Project and add the 2 jars in the classpath.

Here i use plain JDBC but you can use JPA, Hibernate, etc.

This is a very important class, here we are going to create the Server instance and start it. 
Note: I assume that you know something about embedding servers, if you want some background here you can find a great resource.

Now we need to extract the BlazeDS.war content. create a folder called webapp inside your src folder with this content, your project structure should look like this.

The last thing you need to do is declare a remote destination on your services-config.xml

Now we code the Air side:
Fist create an asset folder inside your air src folder and copy all the bin folder from your java project

As you can see i made the same copy into the bin-debug folder, i don't know why it doesn't export all the files to the bin-debug.

Now let's code the application, first in the descriptor file enable the extended desktop profile so you can use Native Process

Now  the code:


Now let's take a more closer view to the code:
  • First the executable file will be the Java path so it depends on your machine.
  • I use NativeProcess to start the embed Jetty instance and a socket to shutdown the embed jetty instance.
  • The arguments for the NativeProcessStartupInfo are the necessary classes to make the Java desktop app run. In the application:
  •  Once it connects you can use RemoteObjects to make rpc call to your Java code.
  • You can use messaging too.
Before you run check your ip match with the socket connection ip, and verify the JDBC setting in the Java code.

When you click the start server button, in the text area you see the log comming from the server.

Now click the Get Data button and in my case the screen will be the following:

Stop the server by clicking the Stop Server button.

And that's all, hope this will help you, any question or bug please let me know.
Here is the source code for the Java Project
Here is the Source code for the Flex Project

1 comentario:

  1. What if you another executable jar running previously? Air won't be able to run "java" again. At least that's a problem I'm facing currently...