Wednesday, May 15, 2013

TomEE in the cloud with Azure and Azure SQL DB

This blog post shows how to deploy the FaceID project on +Windows Azure. We will use +Apache TomEE, Ubuntu 12.04 and the Windows Azure SQL Database.

We are going to:

  • Prepare a Linux box on Azure
  • Install Java
  • Install TomEE 1.6 Snapshot
  • Install our FaceID application
  • Create an Azure SQL Database
  • Install MS SQL Server JDBC driver
  • Link the FaceID application and the Azure SQL Database
  • Deal with the clustered index issue
  • Have fun! 

Build the VM and install FaceID

First, create your virtual machine on Azure. This is the official tutorial: http://www.windowsazure.com/en-us/manage/linux/tutorials/virtual-machine-from-gallery/. Once you have it running, open a ssh session (Windows users can use putty) and upgrade your server.


After restarting your virtual machine, install java.


Some of the features used by the FaceID project are available on TomEE 1.6 Snapshot (and older). The latest snapshot versions of the server are available here. Go ahead and download TomEE Plus 1.6 Snapshot.


Unzip the distribution file.


Start your TomEE server.


You can also tail the log file.


Your TomEE server is now running but it is still not visible from the outside world. You will need to open the port 80. You can map the public port 80 to the private port 8080. The official endpoints how-to tutorial is available here: http://www.windowsazure.com/en-us/manage/windows/how-to-guides/setup-endpoints/.

After creating the endpoints, you will be able to access your server.


It is time to install the FaceID application on it. You can build it yourself (see previous blog posts) or you can download this pre-built war file: http://people.apache.org/~tveronezi/posts/20130514-azuredb/faceid.war.


Move the war file to the webapps directory.


If you access the faceid (in the case above it would be http://tomee-on-azure.cloudapp.net/faceid/) application now, you should see the popup security window.


We have no user for this application yet. Shutdown your server and update the /apache-tomee-plus-1.6.0-SNAPSHOT/conf/ tomcat-users.xml file. You should create an user with the solution-admin role.

 <?xml version='1.0' encoding='utf-8'?>  
 <tomcat-users>  
  <role rolename="solution-admin" />  
  <user username="myadmin" password="mypassword" roles="solution-admin" />  
 </tomcat-users>  

After restarting TomEE, you will be able to access your application.


Create and link an Azure SQL Database

The official how-to tutorial on how to use an Azure SQL Database with java is available here: http://www.windowsazure.com/en-us/develop/java/how-to-guides/using-sql-azure-in-java/.

Create your database.




Download Microsoft JDBC Driver 4.0 for SQL Server (http://www.microsoft.com/en-us/download/details.aspx?id=11774) and upload it to your virtual machine.



Uploading file with filezilla

Extract the file.


Copy the driver to the lib directory.


The FaceID points to a datasource called userJTA. If you don't define this datasource, TomEE will do it for you. The generated datasources use the default hsqldb database. In order to use Windows Azure SQL Database, you need to specify the datasource yourself. Edit the conf/tomee.xml file.


Replace the content of this file by...

 <?xml version="1.0" encoding="UTF-8"?>  
 <tomee>  
  <Resource id="userJTA" type="javax.sql.DataSource">  
 JdbcDriver=com.microsoft.sqlserver.jdbc.SQLServerDriver  
 JdbcUrl=jdbc:sqlserver://s9oprf6lip.database.windows.net:1433;database=tomee-azure;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;  
 UserName=tomee  
 Password={your password here}  
  </Resource>  
 </tomee>  

Restart your server. The application is connected to the Azure DB server, but you cannot persist data yet. As soon as you try to create a new user, TomEE throws an exception.


Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again. {prepstmnt 221417245 INSERT INTO User_securityGroups (USER_UID, element) VALUES (?, ?) [params=?, ?]} [code=40054, state=S0001]  

Go to the Azure Console and connect to your database.



Create the clustered index for the table that Azure is complaining about.

 CREATE CLUSTERED INDEX User_securityGroups_Index ON User_securityGroups (USER_UID)  


If you try to use your application again, you should be able to persist data on it.


Read more about Datasource Configurationhttp://tomee.apache.org/datasource-config.html.
FaceID source code: https://github.com/tveronezi/faceid
Apache TomEE: http://tomee.apache.org/

The Apache TomEE project always welcomes contributions. Join the fun!

[]s!

Thursday, May 9, 2013

Maven Archetype for our Scala JavaEE project


As requested, we now have a Maven Archetype for our Scala/JavaEE application. It makes easier to generate new projects by using the boxdata as template.

Due to the incompatibility of licenses, I cannot propose it as a TomEE sub-module; I plan to create other examples of Rich Internet Applications with Apache friendly licenses and include them in the TomEE project as soon as my free time permits. ExtJS and HighCharts are free of charge for personal use only. If you want to develop a commercial application with them, you will need to buy the commercial licenses for it. Regardless, you are free to hack and use the code that I created in your own projects (Apache License). In fact, I will be glad to have your contribution!

Let's build our application.
  • First, locate the ".m2" directory. It should be one of the children of our home directory. 
  • Create an archetype-catalog.xml file with the following content. 
 <archetype-catalog xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd">  
  <archetypes>  
   <archetype>  
    <groupId>boxdata</groupId>  
    <artifactId>boxdata-archetype</artifactId>  
    <version>1.0-SNAPSHOT</version>  
    <repository>http://people.apache.org/~tveronezi/m2repository</repository>  
    <description>boxdata-like application template</description>  
   </archetype>  
  </archetypes>  
 </archetype-catalog>  

Go to the directory where you want to create your new project and execute...
 mvn archetype:generate  


You should see the list of all the archetypes available in your machine. The last one in this list should be boxdata-archetype.


Now choose your parameters...


Done. Your project is ready!


Linux users can run the application by executing...
 make start-tomee  

Windows users can follow this post: http://buildnplay.blogspot.ca/2012/12/building-jee-application-with-intellij.html. It will show how to run the TomEE server with IntelliJ.

Importing an existing project with IntelliJ

Tip. You don't need to download TomEE. When you run "mvn install" maven will download the bleeding-edge version of the server for you. The file will be available under <YOUR PROJECT>-resources\target\tomee-runtime.tar.gz.

In case you don't want to create the archetype-catalog.xml file, you can clone and build the archetype in your localhost. The source-code is available here: https://github.com/tveronezi/boxdata-archetype. Feel free to clone it. Please contribute!

The TomEE project is always looking for new contributions. Go check it out. Lots of fun! http://tomee.apache.org/

[]s,