Freigeben über


Deploying the Java sample app to Windows Azure using the Starter Kit & Eclipse Plugin

In a previous post, I have presented a little sample Java web application I have written to demonstrate how you can use Windows Azure Platform resources from a Java web application. It’s a simple starting point that includes the basic building blocks like the Windows Azure Java SDK, the SQL Azure JDBC driver, a sample Hibernate configuration, etc. You can find it on GitHub where I will try to keep it up to date.

This sample application can run in standalone fashion within your Eclipse development environment, because it does not use any Windows Azure features that would require running within the Compute Emulator (like multiple roles).

However, at some point your goal is probably to actually deploy this application to Windows Azure! In order to do that, you need to create a Windows Azure specific package (called a Cloud Service Package or CSPKG), and you probably want to test it on the local Compute Emulator before deploying it to a hosted service.

Because Windows Azure does not natively host a Java Runtime Environment nor any servlet container, you need to package your application together with these components; this will make sure that they get deployed to your Windows Azure virtual machines. This is where the Windows Azure for Java Starter Kit & its associated Eclipse Plugin can help you.

The Windows Azure Starter Kit is a skeleton Java project, ready to deploy a generic non-.NET application to Windows Azure. You need to add two things: 1) your runtime environment (i.e. a JRE and a servlet container) and 2) your application! The process is described in detail on the Java/Cloud interoperability blog.

The approach I took to package & deploy my sample application is the following: I kept my sample untouched, and I created a separate project using the Eclipse Plugin. I then modified this separate project in order to add my runtime components. This way, I have two cleanly separated projects: one is my application, and the other is the Windows Azure deployment “shell”. I simply add an Ant task to copy my application’s WAR file to the deployment project before running the packaging tasks.

Let’s see how I organized my deployment project. Here is an overview of its structure:

image

As you can see, it’s basically the structure that the Eclipse Plugin generated when I created a new Windows Azure project. I simply customized the the default build & startup scripts provided.

Let’s start wit the application WAR file, since this is what we want to deploy! As you can see, I have a “foo.war” file at the root level of my approot folder. This is the application I want deployed to my Tomcat application server before it gets started. I could copy it there manually, but it’s of course better to automate it using the package.xml Ant build script:

 <!-- Copy the WAR file to deploy to the approot folder -->
<copy file="${basedir}\..\foo\foo.war" todir="${basedir}\WorkerRole1\approot" />

You should insert this line in the package.xml file, just before calling the windowsazurepackage task. This way, your latest WAR file will automatically be included. You will of course need to change its relative path (i.e., ..\foo\foo.war in my case).

Now, let’s see the startup script (startup.cmd). This is where I will extract all my runtime components and prepare the application for execution.

   1: SET APPROOT=%CD%
  2: 
  3: cscript /B /Nologo %APPROOT%\util\unzip.vbs apache-tomcat-6.0.32-windows-x86.zip %APPROOT%
  4: 
  5: cscript "util\download.vbs" "https://tcontepub.blob.core.windows.net/packages/jre6.zip"
  6: cscript /B /Nologo %APPROOT%\util\unzip.vbs jre6.zip %APPROOT%
  7: 
  8: copy %APPROOT%\foo.war %APPROOT%\apache-tomcat-6.0.32\webapps
  9:  
 10: cd %APPROOT%\apache-tomcat-6.0.32\bin
 11: set JRE_HOME=%APPROOT%\jre6
 12: startup.bat

You can refer to the original Eclipse Plugin blog post for some background for this setup, but are some comments.

  • Line 3: extracting my Tomcat archive to the approot folder ; this is a standard Tomcat distribution with no modifications
  • Line 5: because I find that bundling the JRE makes the cloud package too big, I have pushed my jre6.zip archive to Blob Storage (using CloudXPlorer), and the download.vbs utility will download it automatically when the server starts. It is then extracted to the approot folder as well (line 6).
  • Line 8: this is where I copy my application WAR file to the Tomcat webapps directory, where it will be loaded automatically
  • Lines 10-12: start the Tomcat instance

And that’s it! I can now run my application locally using the scripts in the emulatorTools folder.

When I am ready to deploy to Windows Azure, I can change my windowsazurepackage task in the Ant build file to specify packagetype=”cloud” , and it will generate a CSPKG file ready to deploy to the production Windows Azure infrastructure (typically using the Web Administration Portal).

When you do that, do not forget to change the settings in your Web Application to point to the real Windows Azure resources (i.e. Storage & SQL) instead of the local development instances!

Comments

  • Anonymous
    June 19, 2011
    How can I do this deployment from TFS 2010 Team Build? How can I have continuous deployment for each source code change?

  • Anonymous
    January 12, 2012
    Hello,     Thanks for your tutorial, instead of using WindowsAzureStarteKit i have created my own WindowsAzureProject , i have added tomcat zip,JRE zip and war file the i build the application my application bu ilds successfully .  But when i trying to launch in windows azure emulator my fabric is not gets started . what is going wrong please advice me.