顯示具有 Software Development 標籤的文章。 顯示所有文章
顯示具有 Software Development 標籤的文章。 顯示所有文章

2008年7月23日 星期三

Build Spring & Hibernate based application from scratch

Introduce
I was used to develop my system based on some well designed and integrated framework such as Appfuse. The integrated framework can shorten a lot of development time. So, I never think about build my own application from scratch. Until I starting to code the system for my PHD dissertation.
The major goal of dissertation is to develop a manufacture information platform for distributed mass customization by using inteligence agent. In order to let my agent can inline with my previous development effort, Spring and Hibernate framework integration become my first step to conquer.

Project Layout
Figure 1. shows the project layout. The most important things in Figure 1 is the lib folder.


Figure 1. Project layout
In order to use Spring and Hibernate in my project, I have to import these related jar files into the build path of my project.
The most easy way to test the project layout is correct or not is write a small program to load the Spring configuration.
For Example, create a Java class with a static main function and in the declare area put the following code.


protected final static ApplicationContext ctx;
static{
String[] paths = {"applicationContext.xml",};
ctx = new FileSystemXmlApplicationContext(paths);
}

public static void main(String[] args)
throws IOException
{
}

Well, you will need the Spring configuration file. I named this configuration as ApplicationContext.xml and list as following:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/agentpjm?useUnicode=true&amp;characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="h300"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="1000"/>
<property name="defaultAutoCommit" value="true"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.query.substitutions=true 'Y', false 'N'
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
<!-- Turn batching off for better error messages under PostgreSQL -->
<!-- hibernate.jdbc.batch_size=0 -->
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>

Put this file in the root of the project folder and run the program, there should be no exception appear in the window, then we can start to add Hibernate configuration and prepare the data access object(DAO).

Reference.
  1. http://www.findjar.com
  2. http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/
  3. http://www.hibernate.org/hib_docs/reference/en/html/session-configuration.html

2008年6月1日 星期日

How to run your java application as windows service

I developed a Java program for RFID reader, the program run as a server to continuous listen to the response from readers. The program was run as console application. So, it need manually startup. This is very troublesome when deploy to commercial environment. So, I want to refactor the program as windows service.
I found Java Service Wrapper is what I need. this package to let your Java application run as windows service.
It provides three kinds of integration. You don't need to code any thing by using WrapperSimpleApp and WrapperStartStopApp. However, I prefer the third method that is implement the WrapperListener. The following section depicts the detail programming model of the using WrapperListener.
We have to do two things before the program can run as windows service.
The first thing is to code, since I choose implementation the WrapperListener interface. So the first thing to do is adding the interface code to my program with following steps.
  1. Add wrapper.jar into you eclipse project as reference.
  2. Implements interface WrapperListener
  3. make your class constructor to private
  4. Register this class to WrapperManager by the following code: WrapperManager.start( new UPortController(), args );
  5. Put your startup logic in public Integer start(String[] arg)
  6. Put your shutdown logic in public int Close(int arg0)
Be aware you have to return a null value when the start up action is success or the wrapper will consider your program fail to startup.

The second steps is setting up the wrapper environment and configuration.
The most easy way to do this is copy the directory structure from the wrapper package.
Here is my file structure.
App
|-bin (the startup bat/sh files and wrapper executable file should inside this folder)
|-lib (your jar files and wrapper.dll)
|-conf (contain only one file named wrapper.conf that described the way to load your class)

Be aware to check your Java class path setting and MANIFEST.MF in jar file.

I list my class path setting as following:
%CLASSPATH%=
.;C:\java\jre1.5.0_09\lib\rt.jar;C:\java\jre1.5.0_09\lib\jsse.jar;C:\java\jre1.5
.0_09\lib\jce.jar;

and MANIFEST.MF content in my jar file.

Manifest-Version: 1.0
Created-By: 1.5.0_09 (Sun Microsystems Inc.)
Class-Path: rt.jar activemq-all-5.0.0.jar commons-lang.jar commons-logging-1.1.jar commons-net-1.4.1.jar spring.jar wrapper.jar
Main-Class: xxx.yyy.zzz.UPortController



Reference
  1. http://wrapper.tanukisoftware.org/doc/english/introduction.html

2008年5月29日 星期四

Test Driven Development with NUnit

Test Driven Development (TDD) [1] is a methodology to develop software system. The methodology is an evolutionary approach to development which combines test-first development where you write a test before you write just enough production code to fulfill that test and refactoring.
NUnit is a tool for .Net Platform to execute the TDD. You can download the install package from Nunit website [2].

The following section describe the basic steps for NUnit[2] integration.
  1. Import NUnit.framework.dll into project reference reference.
  2. Create test class with "using NUnit.Framework;"
  3. Add [TestFixture] attribute to identify the class is a test class
  4. Add [SetUp] attribute to a function which is used to setup the environment before test. (The test function will be called before each test function)
  5. Add [Test] attribute to a function which is used to identify the function is a unit test function.
  6. Run the test with NUnit Test Runner user interface. The runner programmer will ask you where is your test DLL, before you start to run the test.
In our project, we want to test our data access object(DAO). So, in the spring context will be initial in the setup function.

  1. #region declare dields
  2. private IApplicationContext ctx;
  3. #endregion

  1. [SetUp]
  2. public void SetUp()
  3. {
  4. BasicConfigurator.Configure();
  5. ctx =
  6. new XmlApplicationContext("assembly://PROJECT_NAME/NAME_SPACE/ApplicationContext.xml");
  7. }


Reference
  1. http://www.agiledata.org/essays/tdd.html
  2. http://www.nunit.org/index.php