July 5, 2010

Your First Cup of Coffee

A friend of mine asked me to provide a simple getting started guide for a first time Java programmer and it so happened it were other people that also wanted the same thing, so all of you out there here it comes, a simple learning by example guide in Java.

The example is a simple Web application that do simple Create, Retrieve, Update and Delete (CRUD) operation against a MySQL database.

Before starting writing you need to install the following on your system.
Installing programs on Windows is a pain and I will leave that as an exercise and instead show you how that this is done on a Linux platform, Ubuntu (ubuntu.com).
$ sudo apt-get install sun-java6-jdk mysql-server mysql-query-browser maven2 


That's it! That took 3 minutes on a modest Internet connection. Are you still stuck on downloading the files on your Windows machine? And thereafter looking forward to reboot you system after each installation?

The Eclipse Ubuntu installation bundle is sadly quite old and therefore it is better to download the zip file manually and extracted it anywhere of you liking and run eclipse.exe (eclipse) in the eclipse folder.

Now lets create your database. Start the MySQL Query Browser and right click in the Schemata dockable window and then select Create Schema.... Enter simpledb.
Now double click on the newly created schema and then right click again to create a new table.



From here you can take several paths, either create a new project with maven archetype 'simple-webapp' (http://www.sonatype.com/books/mvnex-book/reference/web-sect-creating-project.html) or you can install the m2eclipse plugin (http://m2eclipse.sonatype.org/installing-m2eclipse.html) and create your project from Eclipse. Here, I will hopefully, choose the easiest way and do it manually, since we only will be needing two source files, where one will be empty – web.xml and index.jsp.
$ cd [your local workspace directory] 
$ mkdir -p first-coffee/src/main/webapp/WEB-INF


Now create you Maven Project Object Model file pom.xml in first-coffee/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelversion>4.0.0</modelVersion>
 <groupid>se.msc.example</groupId>
 <artifactid>first-coffee</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>first-coffee Maven Webapp</name>
 <url>http://www.msc.se/example</url>
 <dependencies>
  <!-- mysql jdbc driver -->
  <dependency>
   <groupid>mysql</groupId>
   <artifactid>mysql-connector-java</artifactId>
   <version>5.1.13</version>
  </dependency> 
  <!-- test support -->
  <dependency>
   <groupid>junit</groupId>
   <artifactid>junit</artifactId>
   <version>4.8.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
 <build>
  <finalname>first-coffee</finalName>
  <pluginmanagement>
   <plugins>
    <!-- to compile with JDK 1.6 -->
    <plugin>
     <groupid>org.apache.maven.plugins</groupId>
     <artifactid>maven-compiler-plugin</artifactId>
     <configuration>
      <source>1.6</source>
      <target>1.6</target>
     </configuration>
    </plugin>
   </plugins>
  </pluginManagement>
  <plugins>
   <!-- built-in web server 'mvn jetty:run' -->
   <plugin>
    <groupid>org.mortbay.jetty</groupId>
    <artifactid>maven-jetty-plugin</artifactId>
    <configuration>
     <scanintervalseconds>1</scanIntervalSeconds>
     <stopkey>foo</stopKey>
     <stopport>9999</stopPort>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>


Now create your first-coffee/src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 version="2.5">
</web-app>


And finally your dynamic web page (first-coffee/src/main/webapp/index.jsp), where all your work be done.
<%@ page import="java.sql.*" %>
<html>
<body>
<%
 // Step 1: Load the JDBC driver. 
 Class.forName("com.mysql.jdbc.Driver");
 // Step 2: Establish the connection to the database. 
 String url = "jdbc:mysql://localhost:3306/simpledb";
 Connection conn = DriverManager.getConnection(url, "root", "root");
 // Step 3: Do possible posted logic - Create, Update or Delete
 if ("Create".equals(request.getParameter("submitCreate"))) {
  String name = request.getParameter("Name");
  Statement statement = conn.createStatement();
  statement.executeUpdate("INSERT INTO Person (Name) VALUES ('" + name + "')");
 } else if ("Update".equals(request.getParameter("submitUpdate"))) {
  String personId = request.getParameter("PersonId");
  String name = request.getParameter("Name");
  Statement statement = conn.createStatement();
  statement.executeUpdate("UPDATE Person SET Name='" + name + "' WHERE PersonId=" + personId);  
 } else if ("Delete".equals(request.getParameter("submitDelete"))) {
  String personId = request.getParameter("PersonId");
  Statement statement = conn.createStatement();
  statement.executeUpdate("DELETE FROM Person WHERE PersonId=" + personId);  
 }
%>
<table border="1">
<tr>
<th>PersonId</th>
<th>Name</th>
<th>Action</th>
</tr>
<%
 // Step 4: Load all content from table
 Statement stmt = conn.createStatement();
 ResultSet rs = stmt.executeQuery("SELECT PersonId, Name FROM Person");
 while (rs.next()) {
%>
<!-- Here we display the content of the DB table. -->
<form action="index.jsp" method="get">
<input type="hidden" name="PersonId" value='<%=rs.getString("PersonId")%>' />
<tr>
<td><%=rs.getString("PersonId")%></td>
<td><input type="text" name="Name" value="<%=rs.getString("Name")%>" /></td>
<td>
<!-- Provide simple Update and Delete Action for each DB table row. -->
<input type="submit" name="submitUpdate" value="Update" />
<input type="submit" name="submitDelete" value="Delete" />
</td>
</tr>
</form><%
 }
%>
<!-- Provide an empty row for creating new DB table row.  -->
<form action="index.jsp" method="get">

<tr>
<td> </td>
<td><input type="text" name="Name" /></td>
<td><input type="submit" name="submitCreate" value="Create" /></td>
</tr>
</form></table></body>
</html>


To test your application, simply start the Maven built in web server, from your first-coffee directory.

$ mvn jetty:run


Now open your web browser and enter the address http://localhost:8080/first-coffee/.

If you want to continue to further evolve this simple example, you can leave the jetty server running and simply refresh you web server, after saving your changes.

July 1, 2010

Getting Started with Apache Camel and Prepare for Apache ServiceMix Deployment

Apache Camel is dream framework for any developers that is confronting with any type of integration task. What makes Apache Camel so great is:
  • Testability. It is design from the beginning to be a framework that going to support Test Driven Development. With this follows a fast development cycle, since all code can be debugged and executed from a simple JUnit test case.
  • Solves the two foremost problem that an integrator faces:
  • Seamless protocol crossing, i.e. no coding for fetching data from e.g. HTTP and forwarding it to JMS.
  • Default implementation of Enterprise Integration Pattern (http://www.eaipatterns.com/toc.html) that is easy to extend and configure. If you are not familiar with EIP, I recommend you to take the time and read the EIP homepage or if you do not want to miss a thing read the complete book written by Hophe and Woolf (http://www.amazon.com/Enterprise-Integration-Patterns-Designing-Deploying/dp/0321200683).

The next best thing is that Apache Camel is a POJO based solution, which means that it can be deployed and ran in any deployment environment:
  • Standalone Java Application
  • Web Container
  • J2EE Container

When talking of deployment and integration the most common question is. What about ESB? And this is also when Apache Camel shines. Apache Camel is fully supported by Apache Camel parent project Apache ServiceMix, that supports the industry standard Java Business Integration standard. The JBI specification has been criticized about being a slow way of developing integration software and in many aspect that is true, but with Apache Camel as core developing framework that is no longer true.

The reason to select an ESB platform to deploy your integration software is:
  • Management. The ESB platform is specially design to manage integration programs.
  • Redundancy and Scalability. A good ESB platform is specifically design to handle these issues when the load and uptime requirements increases.

But one thing, that in my opinion, the Apache Camel homepage lack is good starting example of showing the above strength. So lets get started with NOT writing a production code example, but instead lets write a JUnit test case that you can debug, then you can continue of writing your production code.

In the below example I will also take into account that one day, you might want or NOT take the next step and move your Apache Camel code into a full blown ESB environment.

Lets first look at your Maven pom.xml.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>se.msc.example</groupId>
 <artifactId>camel</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 <!-- <packaging>jbi-service-unit</packaging> -->
 <name>MSC Example :: Camel</name>
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <servicemix-version>4.2.0</servicemix-version>
  <!--
   servicemix 4.2.0 ships with 'servicemix-camel', thats support camel
   2.2.0
  -->
  <camel-version>2.2.0</camel-version>
  <!--
   'servicemix-camel' has a deviating version compared with 'camel-core'
  -->
  <servicemix-camel-version>2010.01</servicemix-camel-version>
  <!-- supported version for tooling in servicemix 4.2.0 -->
  <servicemix-jbi-plugin-version>4.3</servicemix-jbi-plugin-version>
 </properties>
 <build>
  <pluginManagement>
   <plugins>
    <!-- to compile with JDK 1.6 -->
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <!--
      Use version
      $M2_HOME/lib/maven-<version>-uber.jar/org/apache/maven/project/pom-4.0.0.xml
     -->
     <configuration>
      <source>1.6</source>
      <target>1.6</target>
      <!-- Plugin ignores default value 'project.build.sourceEncoding' -->
      <encoding>${project.build.sourceEncoding}</encoding>
     </configuration>
    </plugin>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-jar-plugin</artifactId>
     <!--
      Use version
      $M2_HOME/lib/maven-<version>-uber.jar/org/apache/maven/project/pom-4.0.0.xml
     -->
     <configuration>
      <archive>
       <manifest>
        <!-- To include versionnumber in MANIFEST.MF -->
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
       </manifest>
      </archive>
     </configuration>
    </plugin>
   </plugins>
  </pluginManagement>
  <plugins>
   <!--
    creates the JBI deployment unit <plugin>
    <groupId>org.apache.servicemix.tooling</groupId>
    <artifactId>jbi-maven-plugin</artifactId>
    <version>${servicemix-jbi-plugin-version}</version>
    <extensions>true</extensions> </plugin>
   -->
  </plugins>
 </build>
 <dependencies>
  <!-- servicemix camel core -->
  <dependency>
   <groupId>org.apache.servicemix</groupId>
   <artifactId>servicemix-camel</artifactId>
   <version>${servicemix-camel-version}</version>
   <scope>provided</scope>
  </dependency>
  <!-- optional camel components -->
  <dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-http</artifactId>
   <version>${camel-version}</version>
  </dependency>
  <!-- test support -->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <!-- version inherited from 'camel-core' -->
   <version>3.8.2</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-core</artifactId>
   <version>${camel-version}</version>
   <type>test-jar</type>
   <scope>test</scope>
  </dependency>
 </dependencies>
</project>


package se.msc.example.camel;

import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;

public class MyFirstCamelTest extends ContextTestSupport {

 @Override
 protected RouteBuilder createRouteBuilder() {
  return new RouteBuilder() {
   public void configure() {
    from("direct:start").to("file:///tmp?fileName=foo.txt").to("mock:result");
    // another example, without mocking start endpoint
    // from("http://camel.apache.org/http.html").to("file:///tmp?fileName=foo.txt").to("mock:result");
   }
  };
 }

 public void test_OK() throws Exception {
  MockEndpoint result = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
  result.expectedMessageCount(1);
  // send somtething from 'direct:start'
  String ls = System.getProperty("line.separator");
  String txt = "Dummy Body åäö";
  template.sendBody("direct:start", txt + ls + txt + ls + txt + ls);
  // execute and assert above 'result.expectedMessageCount(1);'
  result.assertIsSatisfied();
 }

}


To read more about all configuration options for the above endpoints, see http://camel.apache.org/components.html

An Apache Camel route can also be configured in a XML file, which is the natural choice since you want  things to configurable and not static defined in a Java class, but more about in my next blog.