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.
Product.java
Here i use plain JDBC but you can use JPA, Hibernate, etc.
ProductDAO.java
ProductServices.java
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.
BlazeDSServer.java
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
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:
EmbedJetty.mxml
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:
EmbedJetty.mxml
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:
args.push("-classpath");
args.push(".;lib/jetty-all-7.4.0.RC0.jar;lib/servlet-api-2.5.jar;lib/mysql-connector.jar");
args.push("BlazeDSServer");
- Once it connects you can use RemoteObjects to make rpc call to your Java code.
- You can use messaging too.
When you click the start server button, in the text area you see the log comming from the server.
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
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...
ResponderEliminar