Wednesday, May 30, 2012

Set System property on Maven pom.xml for test

Hi, I made maven test class that makes internet connection like Soap, http connection so on that verifies all test is successful. but, my local internet connection requires Proxy server configuration. If I run it on my computer, it won't run because I get connection refused error. If I set system property on my java source, it is not pretty and I might need to change source code when I deploy on production level.

There is the solution :
I need to add maven-surefire-plugin for setting system property on pom.xml file. That is only applies for test classes system property. You can set skip test if you change value of skipTests element true/false below

<project ...>
 <dependencies>
 ...
 </dependencies>

 <build>
    <plugins>
       <plugin> ....      </plugin>

       <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.12</version>
  <configuration>
    <skipTests>true</skipTests>
    <systemPropertyVariables>
      <http.proxyHost>106.101.5.61</http.proxyHost>
      <http.proxyPort>8080</http.proxyPort>
    </systemPropertyVariables>
  </configuration>
       </plugin>
     </plugins>  
 </build>
</project>


For more detail please refer maven surefire plugin document

Friday, May 4, 2012

Spring 3 Task Execution and Scheduling


I have downloaded Spring3 and Quartz and Spring batch Framework.
After running a simple example, It was not easy, not working well with autowired annotation and many things to learn before use giant functions. currently, Spring batch depend on spring 2.5 that I am not sure, it will work well with Spring 3.1
It is also causing to make big packaging jar/war file

I just wanted to make a very simple batch program that I can use based on SpringFramwork which can autowire beans and DB resources.
I decide to use Spring Task. It just need Spring framework, nothing else
It is extreamly simple and works perfect as I expected.

Just type 3 more line on you spring xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


<context:component-scan base-package="monitor"/>

<mvc:annotation-driven />

<!--  Omit all the WEB MVC Handler and View Resolver Configuration -->


<!--  Scheduler define start -->
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>

<task:executor id="myExecutor" pool-size="5"/>

<task:scheduler id="myScheduler" pool-size="10"/>
<!--  Scheduler define End -->

</beans>

Make one Class that is Component annotated. The Class must be in base-package or sub package.
and make method that is Scheduled annotated with Cron Expression.
That's it. that it will run every 5 minutes from 2 min of the hour.
You can autowire beasn which you like.

package monitor.batch;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class HarvestHttpStatusJob {

 private final Log logger = LogFactory.getLog(this.getClass());

 //To-do  @Autowired beans as usual you need.

 @Scheduled(cron = "0 2/5 * * * *")
 public void harvestStatus() {

  logger.info(this.getClass().getName() + " Start");

  //To do Implement your code

  logger.info(this.getClass().getName() + " Finish");
 }

}

For more Detail Information. Please refer below document from SpringFramwork.
http://static.springsource.org/spring/docs/3.1.1.RELEASE/spring-framework-reference/html/scheduling.html

If you are interested in Spring batch and quartz, Please refer below link
Spring batch and quartz



Use of Apache commons configuration in Spring framework

I can get benefit if I use Apache commons configuration.
The reason is

I can choose plain text property file or xml based property file. but, mostly better to use xml based property
I can use UTF-8 Encoding, so that I can type most of language characters.
I can set same key name to get list or array or collection so on. So, I can simply get it and run looping clause.
I can get property value based on hierarchy.

I wanted to use on Spring framework. I just set one Spring bean
※ it worked in Spring3, I guess it will work lower version of Spring as well.

 <bean id="xmlConfig" class="org.apache.commons.configuration.XMLConfiguration">
  <constructor-arg type="java.lang.String">
   <value>commons-config.xml</value>
  </constructor-arg>
 </bean>


commons-config.xml file, you can change root element "configuration" as you like

<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
 <countries>
  <country>
   <code>gbr</code>
  </country>
  <country>
   <code>ger</code>
  </country>
  <country>
   <code>ita</code>
  </country>
  <country>
   <code>aut</code>
  </country>
  <country>
   <code>che</code>
  </country>
  <country>
   <code>chfr</code>
  </country>
  <country>
   <code>swe</code>
  </country>
  <country>
   <code>esp</code>
  </country>  
 </countries>
</configuration>

Make a Spring controller and getStringArray from property
package monitor.controller;

import java.util.List;

import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {

 private final Log logger = LogFactory.getLog(this.getClass());

 @Autowired private XMLConfiguration config;

 @RequestMapping(value = { "/", "/index" })
 public ModelAndView index() {

  ModelAndView mnv = new ModelAndView();
  String[] countries = config.getStringArray("countries.country.code");
  mnv.addObject("countries", countries);

  mnv.setViewName("index");

  return mnv;
 }

}

Thursday, May 3, 2012

How to configure apt-get command to let work behind proxy on Ubuntu 11

apt-get doesn't send packet via system proxy which you might have set in system setting, you need to put it manually.It require to create one file on "/etc/apt/apt.conf" You just create file and type 3 lines and save it, that's it.
/etc/apt$ sudo vi apt.conf

1.type in below
Acquire::http::proxy "http://[[user][:pass]@]host[:port]/";
Acquire::ftp::proxy "ftp://[[user][:pass]@]host[:port]/";
Acquire::https::proxy "https://[[user][:pass]@]host[:port]/";

Actual example from my configuration
Acquire::http::proxy "http://192.168.0.1:8080/";
Acquire::ftp::proxy "ftp://192.168.0.1:8080/";  
Acquire::https::proxy "https://192.168.0.1:8080/";

2.save it, if you don't feel comfortable to use vi, you can use any text editor.
:wq!

※More detail information
man apt.conf