Introduction
While the support has been out for a while, not every Java developer knows that “Azure”, the cloud computing platform from Microsoft, supports a variety of languages and development platforms including .NET, Java, Hadoop, NodeJS, iOS, Android, PHP, Python and Ruby.
In this article, we will learn how we can migrate a Java application to Windows Azure.
Prerequisites
To get started with having Java applications running on Windows Azure, you need to have the prerequisites installed. We are assuming that you have Eclipse and Apache Tomcat server installed and configured.
Azure Plugin for Eclipse
The majority of Java developers use Eclipse as the IDE for development. The Azure Plugin for Eclipse provides templates and functionality that enabled ease of creation, development, testing and deployment of applications using Eclipse IDE.
If you do not have Eclipse, you can download Eclipse from http://www.eclipse.org/downloads/
Once downloaded, you can start Eclipse and click Help -> Install New Software.
Install New Software
If you have already installed the plugin, you can skip this step, otherwise you will need to add a new location to search for plugins and install them. The Azure plugins are located at http://dl.msopentech.com/eclipse by clicking Add on the available Software window and following the steps to add the repository.
Add Repository
Once the repository is added, you will see the screen below.
Install
Follow the steps to install the plugins (including accepting Terms and Conditions of installing the plugins). If prompted, restart Eclipse.
Tomcat Server
Download Apache Tomcat server from http://tomcat.apache.org/download-70.cgi.
Once extracted, configure Tomcat server in Eclipse.
Building a Java Application
We will now proceed to build a simple Java application.
Click File- > New -> Project -> Dynamic Web project.
New Dynamic Web Project
Provide the name of the project as “testDynamicProject” and click Next.
Java: Configure Project
Click Next.
Web Module
Leave the defaults unchanged and click Finish.
This will create the skeleton of the project.
Next, we need to create a Java Servlet in Eclipse.
Right click “testDynamicProject” in the Project Explorer and select New ->Servlet.
New Servlet
The Create Servlet Wizard will be displayed.
Create Servlet
Provide the name of your servlet as “myServlet” and click Next.
Name Servlet
Leave the default unchanged and click Next.
Create Servlet Defaults
On the final screen, click Finish.
This will create a skeletal template in the Eclipse IDE in a file called myServlet.java.
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import javax.servlet.http.*; import javax.servlet.*; /** * Servlet implementation class myServlet */ @WebServlet("/myServlet") public class myServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public myServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
We will now implement the doGet function to say “Hello World”.
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import javax.servlet.http.*; import javax.servlet.*; /** * Servlet implementation class myServlet */ @WebServlet("/myServlet") public class myServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public myServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter out = response.getWriter(); out.println("Hello, world!"); out.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
Our simple HttpServlet is “code complete”. If we run this application, we will see the following
Run the Application
Append myServlet to the URL, and we will see our “Hello World” webpage.
Hello World Webpage
Now that our test application is complete, we will build a local Azure deployment project for it.
Click the “New Azure Deployment Project” icon.
Azure Deployment Project Icon
If the Azure SDK is not installed, you will be prompted as under.
Azure SDK Not Installed
Follow these steps to install the SDK.
Windows Azure SDK
Accept the Terms and Conditions by clicking “I Accept”.
Accept Terms
Once the installation is complete, you might need to click the “New Azure Deployment Project” icon again.
New Azure Deployment Project Icon
This will start the Azure Deployment Project Wizard.
Azure Deployment Project
Give the name of the project (in my case, I am naming it as “deployAzure”.
Specify the JDK
For the JSK settings page, I checked the “Use the JDK from this file path for testing locally”.
Click Next to go to Server settings.
Select the Type of Server
For the emulator deployment, check the “Use the server from this file path for testing locally and select Apache Tomcat 7.
Select Apache Tomcat 7
Click Next to configure Applications settings.
Select the Application to be Deployed
Select the “testDynamicProject” as the application to be deployed and click Finish.
Now that the deployment project has been created, select it and click “Run in Azure Emulator” icon.
Run in Azure Emulator Icon
The Azure Storage Emulator will launched (you will be asked for Administrator credentials if you are not running as Administrator).
Once the emulator starts running, open your browser and direct it to
http://localhost:8080/testDynamicProject/myServlet to see your web application running as a deployed project.
You will see the screen below:
Web Application Running
Now that we have our Java application running as the deployed package, our last step is to deploy into the actual cloud and run it from there. For that, click the “Publish to Azure Cloud” icon.
Publish to Azure Cloud Icon
The Publish Wizard will be launched.
Publish Wizard
Click Import from PUBLISH-SETTINGS file… to extract the subscription information.
Import Subscription Information
Click Download PUBLISH-SETTINGS File…
Once you enter your Microsoft account credentials, you will be prompted to download the file.
Select the path where you have downloaded the PUBLISH-SETTINGS file and click OK.
Location of Download
You will come back to Publish to Azure dialog in the Publish Wizard.
Publish to Azure
To be able to publish, we will need a cloud storage account as well as a cloud service.
If you do not have a storage account, you will need to create one, which you can do directly from the wizard by clicking “New..” next to the storage account label.
Storage Account
I am naming my storage account as “teststoragevipul”. Click OK.
Publish a Cloud Service Name
Next, we will create a new Cloud Service by clicking “New…” next to the service name label.
New Cloud Service
I am naming my service as testJavaAzure. Click OK to return to Publish Wizard.
Publish to Azure
Click Publish to begin publishing our Java application to Windows Azure.
You will notice a lot of activity happening in your IDE (which indicates Azure deployment is in progress).
Azure Deployment in Progress
After about 5 minutes, your deployment should be complete.
Azure Deployment Complete
At this moment, our application is now running in the cloud in a staging environment (because that is the setting we chose).
Then, go to your Windows Azure Management portal at https://manage.windowsazure.com
You should see that the cloud service and the storage account we created are shown in the list of “all items”.
List of All Items
Click on the testJavaAzure (or whatever your cloud service name is) name. You will be taken to the home page of the cloud service.
Home Page of the Cloud Service
Click Dashboard and then click Staging.
Staging
You will notice a URL for the SITE at the bottom of the page.
URL for the Site
This is the link to our service in the staging realm.
The URL for my service is
http://31991864cb2e4856975debd71d9321b4.cloudapp.net/
When I click the link, I see the default Apache home page.
Default Apache Home Page
To go to my service, I need to append “testDynamicProject/myServlet” to the URL.
http://31991864cb2e4856975debd71d9321b4.cloudapp.net/testDynamicProject/myServlet
When I go to http://31991864cb2e4856975debd71d9321b4.cloudapp.net/testDynamicProject/myServlet, I see my “Hello World” project that I had built in my Eclipse running in the cloud.
Hello World Running in the Cloud
We can see that we have successfully migrated a Java web application to Windows Azure, which is what we had intended to do.
Summary
In this article, we learned how to deploy a simple Java application to Windows Azure. I hope you have found this information useful and can use it to deploy your applications to the cloud.
About the Author
Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul.patel@hotmail.com