Starting A Web Application Based On ZK CDI JPA and Jetty"

From Documentation
Line 55: Line 55:
 
Please follow the steps bellow to configure the downloaded demo project.
 
Please follow the steps bellow to configure the downloaded demo project.
 
# '''Import project into your Eclipse workspace'''. You can import it by mouse right clicking in ''Package Explorer'', select ''import...'', then chose ''Existing Projects into Workspace'' and follow the indication to reference the project archive.
 
# '''Import project into your Eclipse workspace'''. You can import it by mouse right clicking in ''Package Explorer'', select ''import...'', then chose ''Existing Projects into Workspace'' and follow the indication to reference the project archive.
# '''fine tune settings in pom.xml'''. by now (when this article has been written) the ZK version is 6.0.2, you can change it by configuring:
+
# '''fine tune settings in pom.xml'''. by now (when this article has been written) the ZK version is 6.0.2, you can change it by configuring:<source  lang="xml">
<source  lang="xml">
 
 
<properties>
 
<properties>
 
<zk.version>6.0.2</zk.version>
 
<zk.version>6.0.2</zk.version>
Line 62: Line 61:
 
</properties>
 
</properties>
 
</source>
 
</source>
 +
# ''' Run Jetty Run Runtime configuration'''.
  
 
=Configuration & Composition=
 
=Configuration & Composition=

Revision as of 05:13, 8 August 2012

WarningTriangle-32x32.png This page is under construction, so we cannot guarantee the accuracy of the content!

DocumentationSmall Talks2012AugStarting A Web Application Based On ZK CDI JPA and Jetty
Starting A Web Application Based On ZK CDI JPA and Jetty

Author
Ian YT Tsai, Engineer, Potix Corporation
Date
Aug 06, 2012
Version
ZK 6

Introduction

Building a Java Web application is always started with a bunch of choices: Choices of which software architecture should pick, layers of architecture should define, and possible solutions or frameworks that each layer should use. And that's why we welcome a common practice; a "default stack" like "Struts+Spring+Hibernate" that we can start our project and evaluating if this stack fits our requirement or some part of it need to be swapped.

So, in this series of article, I want to propose a stack which is based on CDI (Weld), JPA (Hibernate) and ZK running on a simple web container (Jetty or Tomcat), and this could be a good start for your ZK-based application.

In the first article, I'll introduce the aiming of this stack and a little part of Java CDI technology by walking through a development Environment setup and application stack build-up.

In the second article: Practices Of Using CDI In ZK I'll introduce some programming practices to each layer and some possible solutions for some user scenarios.

Application Stack Design

In tradition, a web application consists 3 tiers/layers, which are: Presentation, Logic and Persistence. In our application stack we use:

  • ZK as Presentation
  • CDI as Logic
  • JPA as Persistence

File:3 tier archi.jpg

Now let's see the aiming of our stack:

Using light-weight Web Container

I use Jetty rather than a heavy duty application server because:

  • Fast to reboot. especially in Eclipse, you can always restart it in 5 secs.
  • Popular and friendly to everyone. A lot of people hesitate to learn CDI because though it's designed for general purpose like Spring, but it is included in Java EE specification which means you need to use an application server to play with it. Studying how to use an application server nowadays is not a big problem but the natural barrier still higher than studying a simple web container.

Based on Java EE

As a default stack of our ZK-based application, using Java EE standard technology in each layer grants you good flexibility to swap the implementation and even the container.

  • CDI: we only use Weld here rather than the entire Seam framework because we don't want to make this stack to be too addict to vendor specific feature at beginning. we can always make such decision when we hit by some critical issues that require such features.
  • JPA: Hibernate is very powerful and proven to use, we use JPA's interface to access it by default.

In Memory Persistence

in order to start a project as quick as possible but still be flexible when we need better performance and functionality, we use in-memory DB for our default stack. I use HSQLDB here which requires minimal configuration. And because we use JPA as the persistence interface, it is very easy to swap HSQLDB with other DB.

Development Environment Setup

First, let's prepare our development environment.

There are two ways to download the demo web project:

  1. git clone [email protected]:zanyking/smalltalk.git, the demo project is under /CDI_Integration/backend_demo.
  2. download it from: [1]. Unpack the downloadable zip file and you got the project archive.

let's see the development environment setup of this project:

Eclipse IDE Preparation

If you don't have an Eclipse IDE, please download Eclipse 3.6 or 3.7 JavaEE Developer's package. The required plugins are listed bellow:

  1. M2Eclipse: in order to shrink the project size and manage the project well, I use Maven to manage my project, and M2Eclipse can help you import the project into your eclipse workspace much easier.
  2. Run Jetty Run: a very light weight Jetty server runner to host the runtime of your web application.

Project Configuration

Please follow the steps bellow to configure the downloaded demo project.

  1. Import project into your Eclipse workspace. You can import it by mouse right clicking in Package Explorer, select import..., then chose Existing Projects into Workspace and follow the indication to reference the project archive.
  2. fine tune settings in pom.xml. by now (when this article has been written) the ZK version is 6.0.2, you can change it by configuring:
    	<properties>
    		<zk.version>6.0.2</zk.version>
    		...
    	</properties>
    
  3. Run Jetty Run Runtime configuration.

Configuration & Composition

CDI in Jetty

JNDI Settings

Weld Settings

How about Tomcat

JPA Configuration

Persistence Unit

Entity Manager in CDI

Summery

Next Article