utility Posts

How to Compress a Java Object

Recently i’m developing a simple client server application, using serialized java objects as parameter. To increase performance, im trying to compress my java object. Here’s how i do it.

a simple serialized java bean

package com.edw.bean;

import java.io.Serializable;
import java.math.BigDecimal;

public class TestBean implements Serializable {
    private BigDecimal id;
    private String value;

    public BigDecimal getId() {
        return id;

    public void setId(BigDecimal id) {
        this.id = id;

    public String getValue() {
        return value;

    public void setValue(String value) {
        this.value = value;

and a class to compress my object

package com.edw.main;

import com.edw.bean.TestBean;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigDecimal;
import java.util.zip.Deflater;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.log4j.Logger;

public class Main {

    private final static Logger logger = Logger.getLogger(Main.class);

    public Main() {

    private void startApp() throws Exception {
        // create a very big bean
        TestBean tb = new TestBean();
        tb.setId(new BigDecimal(Double.MAX_VALUE));
        String bigString = "";
        for (int i = 0; i < 5000; i++) {
            bigString += " test " + i;

        // write it to file to see this object's size
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("test.size"), false));

        // get the serialized object
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("test.size")));
        Object regularObject = ois.readObject();
        logger.debug(((TestBean) regularObject).getId());

        // write compressed object to file to see this object's size
        FileOutputStream fos = new FileOutputStream(new File("testCompressed.size"), false);
        GZIPOutputStream gzipos = new GZIPOutputStream(fos) {


        ObjectOutputStream oosCompressed = new ObjectOutputStream(gzipos);

        // get the serialized object
        FileInputStream fis = new FileInputStream(new File("testCompressed.size"));
        GZIPInputStream gzipis = new GZIPInputStream(fis);
        ObjectInputStream oisCompressed = new ObjectInputStream(gzipis);
        Object compressedObject = oisCompressed.readObject();
        logger.debug(((TestBean) compressedObject).getId());

    public static void main(String[] args) throws Exception {
        Main main = new Main();

it can compress up to 75% of size,

Thank you and have a good time coding. (*)

a Simple iBatis Cache using OSCache

Some values in a database are know to change slower than others. To improve performance, many developers like to cache often-used data to avoid making unnecessary trips back to the database. iBATIS provides its own caching system, that you configure through a <cacheModel> element.

The results from a query Mapped Statement can be cached simply by specifying the cacheModel parameter. A cache model is a configured cache that is defined within your DataMapper configuration file. IBATIS cache focuses on caching results within the persistence layer. As such, it is independent of the service or presentation layers, and is not based on object identity.

In this simple example, i’m using OSCache. OSCache is a robust caching framework that is able to perform many of the same type of caching strategies that iBATIS provides in its other cache models. When using the OSCACHE model, you create a dependency on the OSCache JARs. You will need to include the OSCache JAR in your project when you use this setting. This cache is configured in the same manner as a standard OSCache install. This means that you will need to have an oscache.properties file available on the root of your classpath for OSCache to read.

Okay, here’s my code,
a simple “test” database, and a “contoh” table

USE 'test'

CREATE TABLE `contoh` (
  `nama` varchar(30) NOT NULL DEFAULT '',
  `alamat` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`nama`)

insert into contoh (nama, alamat) values ('edwin', 'jakarta');
insert into contoh (nama, alamat) values ('kamplenk', 'ciledug');
insert into contoh (nama, alamat) values ('nugie', 'pamulang');
insert into contoh (nama, alamat) values ('samsu', 'dago');
insert into contoh (nama, alamat) values ('tebek', 'jayapura');

a contoh POJO

package com.edw.bean;

 * @author edw
public class Contoh {
    private String nama;
    private String alamat;

    // other setter and getter

and my sqlmapconfig.xml as my ibatis xml configuration

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"


    <transactionManager type="JDBC" commitRequired="false">
        <dataSource type="SIMPLE">

            <property name="SetAutoCommitAllowed" value="false"/>
            <property name="DefaultAutoCommit" value="false"/>
            <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
            <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/test"/>
            <property name="JDBC.Username" value="root"/>
            <property name="JDBC.Password" value="xxx"/>

    <sqlMap resource="com/edw/sqlmap/contoh.xml"/>

and my xml query, at line 5 to 9 is my OSCache caching, and at line 21 is cacheModel implementation on query “selectAllContoh”. It means that i’m storing “selectAllContoh” resultSet on a cache, so the next time query “selectAllContoh” is called, iBatis will get resultSet from my cache instead of hitting my database.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="contoh" >

    <cacheModel id="contohCache" type="OSCACHE">
        <flushInterval hours="24"/>
        <flushOnExecute statement="contoh.insertContoh"/>
        <property name="reference-type" value="SOFT"/>

    <resultMap id="result" class="com.edw.bean.Contoh">
        <result property="nama" column="NAMA" columnIndex="1"/>
        <result property="alamat" column="ALAMAT" columnIndex="2"/>

    <insert id="insertContoh" parameterClass="com.edw.bean.Contoh" >
    insert into contoh (nama, alamat)
    values (#nama:VARCHAR#, #alamat:VARCHAR#)

    <select id="selectAllContoh" resultMap="result" cacheModel="contohCache">
    select nama, alamat
    from contoh


a class to load my ibatis xml configuration

package com.edw.config;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.Reader;

 * @author edw
public class SqlMapConfig {

    protected static final SqlMapClient sqlMap;

    static {
        try {
            Reader reader = Resources.getResourceAsReader("com/edw/sqlmap/sqlmapconfig.xml");
            sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);       
        } catch (Exception e){
            throw new RuntimeException("Fatal Error.  Cause: " + e, e);

    public static SqlMapClient getSqlMap() {
        return sqlMap;

and my log4j configuration, i named it log4j.properties

# Global logging configuration

# Console output...
log4j.appender.stdout.layout.ConversionPattern=%d [%c{1}] %-5p %c:%L - %m%n

and my oscache.properties


i create a simple JUnit class to test my class

package com.edw.test;

import com.edw.bean.Contoh;
import com.edw.config.SqlMapConfig;
import java.util.List;
import org.apache.log4j.Logger;
import org.junit.Test;
import static org.junit.Assert.*;

 * @author edw
public class TestIbatisCaching {

    public void testIbatisCaching() {
        Logger logger = Logger.getLogger(this.getClass());
        try {
            // repeated queries
            for (int i = 0; i < 5; i++) {
                List<Contoh> contohs = SqlMapConfig.getSqlMap().queryForList("contoh.selectAllContoh");

                // check if it's null

                Object o = new Object();

                for (Contoh contoh : contohs) {

        } catch (Exception ex) {

this is what happen on my console,
on line 24-25 shows how OSCache creating a cache from a query “contoh.selectAllContoh”,
and on line 37-38 shows how query “contoh.selectAllContoh” hitting Ibatis’ cache instead of the database.

2010-10-09 18:49:24,961 [Config] DEBUG com.opensymphony.oscache.base.Config:61 - OSCache: Config called
2010-10-09 18:49:24,968 [Config] INFO  com.opensymphony.oscache.base.Config:147 - OSCache: Properties read {cache.capacity=1000}
2010-10-09 18:49:24,969 [AbstractCacheAdministrator] DEBUG com.opensymphony.oscache.base.AbstractCacheAdministrator:160 - Constructed AbstractCacheAdministrator()
2010-10-09 18:49:24,993 [GeneralCacheAdministrator] INFO  com.opensymphony.oscache.general.GeneralCacheAdministrator:98 - Constructed GeneralCacheAdministrator()
2010-10-09 18:49:24,993 [GeneralCacheAdministrator] INFO  com.opensymphony.oscache.general.GeneralCacheAdministrator:301 - Creating new cache
2010-10-09 18:49:25,028 [AbstractConcurrentReadCache] DEBUG com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache:694 - get called (key=1542486422|5257123254|contoh.selectAllContoh|1339823102|     select nama, alamat     from contoh     |executeQueryForList|0|-999999)
2010-10-09 18:49:25,028 [AbstractConcurrentReadCache] DEBUG com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache:1061 - persistRetrieve called (key=1542486422|5257123254|contoh.selectAllContoh|1339823102|     select nama, alamat     from contoh     |executeQueryForList|0|-999999)
2010-10-09 18:49:25,099 [Cache] DEBUG com.opensymphony.oscache.base.Cache:697 - No cache entry exists for key='1542486422|5257123254|contoh.selectAllContoh|1339823102|     select nama, alamat     from contoh     |executeQueryForList|0|-999999', creating
2010-10-09 18:49:25,106 [CacheModel] DEBUG com.ibatis.sqlmap.engine.cache.CacheModel:27 - Cache 'contoh.contohCache': cache miss
2010-10-09 18:49:25,331 [SimpleDataSource] DEBUG com.ibatis.common.jdbc.SimpleDataSource:27 - Created connection 5230193.
2010-10-09 18:49:25,334 [Connection] DEBUG java.sql.Connection:27 - {conn-100000} Connection
2010-10-09 18:49:25,336 [Connection] DEBUG java.sql.Connection:27 - {conn-100000} Preparing Statement:      select nama, alamat     from contoh     
2010-10-09 18:49:25,381 [PreparedStatement] DEBUG java.sql.PreparedStatement:27 - {pstm-100001} Executing Statement:      select nama, alamat     from contoh     
2010-10-09 18:49:25,405 [PreparedStatement] DEBUG java.sql.PreparedStatement:27 - {pstm-100001} Parameters: []
2010-10-09 18:49:25,406 [PreparedStatement] DEBUG java.sql.PreparedStatement:27 - {pstm-100001} Types: []
2010-10-09 18:49:25,411 [ResultSet] DEBUG java.sql.ResultSet:27 - {rset-100002} ResultSet
2010-10-09 18:49:25,429 [ResultSet] DEBUG java.sql.ResultSet:27 - {rset-100002} Header: [NAMA, ALAMAT]
2010-10-09 18:49:25,429 [ResultSet] DEBUG java.sql.ResultSet:27 - {rset-100002} Result: [edwin, singapore]
2010-10-09 18:49:25,430 [ResultSet] DEBUG java.sql.ResultSet:27 - {rset-100002} Result: [kamplenk, ciledug]
2010-10-09 18:49:25,430 [ResultSet] DEBUG java.sql.ResultSet:27 - {rset-100002} Result: [nugie, pamulang]
2010-10-09 18:49:25,511 [ResultSet] DEBUG java.sql.ResultSet:27 - {rset-100002} Result: [samsu, dago]
2010-10-09 18:49:25,513 [ResultSet] DEBUG java.sql.ResultSet:27 - {rset-100002} Result: [tebek, jayapura]
2010-10-09 18:49:25,514 [AbstractConcurrentReadCache] DEBUG com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache:694 - get called (key=1542486422|5257123254|contoh.selectAllContoh|1339823102|     select nama, alamat     from contoh     |executeQueryForList|0|-999999)
2010-10-09 18:49:25,515 [AbstractConcurrentReadCache] DEBUG com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache:1061 - persistRetrieve called (key=1542486422|5257123254|contoh.selectAllContoh|1339823102|     select nama, alamat     from contoh     |executeQueryForList|0|-999999)
2010-10-09 18:49:25,519 [Cache] DEBUG com.opensymphony.oscache.base.Cache:697 - No cache entry exists for key='1542486422|5257123254|contoh.selectAllContoh|1339823102|     select nama, alamat     from contoh     |executeQueryForList|0|-999999', creating
2010-10-09 18:49:25,520 [AbstractConcurrentReadCache] DEBUG com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache:1108 - persistStore called (key=1542486422|5257123254|contoh.selectAllContoh|1339823102|     select nama, alamat     from contoh     |executeQueryForList|0|-999999)
2010-10-09 18:49:25,521 [AbstractConcurrentReadCache] DEBUG com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache:1087 - persistRetrieveGroup called (groupName=contoh.contohCache)
2010-10-09 18:49:25,521 [AbstractConcurrentReadCache] DEBUG com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache:1127 - persistStoreGroup called (groupName=contoh.contohCache)
2010-10-09 18:49:25,523 [CacheModel] DEBUG com.ibatis.sqlmap.engine.cache.CacheModel:27 - Cache 'contoh.contohCache': stored object '[com.edw.bean.Contoh@12bb7e0, com.edw.bean.Contoh@134f69a, com.edw.bean.Contoh@2a15cd, com.edw.bean.Contoh@fd68b1, com.edw.bean.Contoh@e45076]'
2010-10-09 18:49:25,525 [SimpleDataSource] DEBUG com.ibatis.common.jdbc.SimpleDataSource:27 - Returned connection 5230193 to pool.
2010-10-09 18:49:27,526 [TestIbatisCaching] DEBUG com.edw.test.TestIbatisCaching:34 - edwin
2010-10-09 18:49:27,526 [TestIbatisCaching] DEBUG com.edw.test.TestIbatisCaching:34 - kamplenk
2010-10-09 18:49:27,526 [TestIbatisCaching] DEBUG com.edw.test.TestIbatisCaching:34 - nugie
2010-10-09 18:49:27,526 [TestIbatisCaching] DEBUG com.edw.test.TestIbatisCaching:34 - samsu
2010-10-09 18:49:27,526 [TestIbatisCaching] DEBUG com.edw.test.TestIbatisCaching:34 - tebek
2010-10-09 18:49:27,526 [TestIbatisCaching] DEBUG com.edw.test.TestIbatisCaching:37 - ================================
2010-10-09 18:49:27,536 [AbstractConcurrentReadCache] DEBUG com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache:694 - get called (key=1542486422|5257123254|contoh.selectAllContoh|1339823102|     select nama, alamat     from contoh     |executeQueryForList|0|-999999)
2010-10-09 18:49:27,536 [CacheModel] DEBUG com.ibatis.sqlmap.engine.cache.CacheModel:27 - Cache 'contoh.contohCache': retrieved object '[com.edw.bean.Contoh@12bb7e0, com.edw.bean.Contoh@134f69a, com.edw.bean.Contoh@2a15cd, com.edw.bean.Contoh@fd68b1, com.edw.bean.Contoh@e45076]'
2010-10-09 18:49:29,536 [TestIbatisCaching] DEBUG com.edw.test.TestIbatisCaching:34 - edwin
2010-10-09 18:49:29,536 [TestIbatisCaching] DEBUG com.edw.test.TestIbatisCaching:34 - kamplenk
2010-10-09 18:49:29,536 [TestIbatisCaching] DEBUG com.edw.test.TestIbatisCaching:34 - nugie
2010-10-09 18:49:29,536 [TestIbatisCaching] DEBUG com.edw.test.TestIbatisCaching:34 - samsu
2010-10-09 18:49:29,536 [TestIbatisCaching] DEBUG com.edw.test.TestIbatisCaching:34 - tebek
2010-10-09 18:49:29,536 [TestIbatisCaching] DEBUG com.edw.test.TestIbatisCaching:37 - ================================

this is my Netbeans project structure

Well, i guess it can help others. Thank you and see you again. (H)

How to Convert Java List or ArrayList into ResultSet

At this project i found an obstacle, which i need to convert a reporting module from fetching data from database into fetching data from Webservices. At first I think it was easy, I just fetch strings from Webservice, and put it in a bean or in a map and put it all in an ArrayList, just like JasperReport. But I forgot, this isn’t JasperReport I’m using, I’m using bloody hell CrystalReport. And my Crystal-Report-calling method only accepting ResultSet for parameter, so somehow I need to convert strings from webservice into ResultSet.

My idea is adding strings from webservices into an ArrayList<Object> and adding this ArrayList into ArrayList<ArrayList<Object>> and converting it into rows of ResultSet. And im using MockRunner library to help me. This is how I do it :

I create a class to convert a list into result set

package com.edw.rs;

import com.mockrunner.mock.jdbc.MockResultSet;
import java.sql.ResultSet;
import java.util.List;

 * @author edw
public class MyResultSet {

    public ResultSet getResultSet(List<String> headers, List<List<Object>> data) throws Exception {

        // validation
        if (headers == null || data == null) {
            throw new Exception("null parameters");

        if (headers.size() != data.size()) {
            throw new Exception("parameters size are not equals");

        // create a mock result set
        MockResultSet mockResultSet = new MockResultSet("myResultSet");

        // add header
        for (String string : headers) {          

        // add data
        for (List<Object> list : data) {

        return mockResultSet;

and i tested it with my junit class

package com.edw.rs.test;

import com.edw.rs.MyResultSet;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.junit.Test;

 * @author edw
public class MyResultSetTest {

    private Logger logger = Logger.getLogger(MyResultSetTest.class);

    public MyResultSetTest() {

    public void testMyResultSet() throws Exception {
        MyResultSet myResultSet = new MyResultSet();
        List<String> headers = new ArrayList<String>();

        List<List<Object>> data = new ArrayList<List<Object>>();
        for (int i = 0; i < 4; i++) {
            List<Object> objects = new ArrayList<Object>();
            objects.add(new Integer(i));
            objects.add("name " + i);
            objects.add(new Double(i));
            objects.add(new Integer(20 + i));

        ResultSet rs = myResultSet.getResultSet(headers, data);
        while (rs.next()) {


this is the result on my IDE console

2010-08-18 18:13:09,112 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:42 - 0
2010-08-18 18:13:09,131 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:43 - name 0
2010-08-18 18:13:09,132 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:44 - 0.0
2010-08-18 18:13:09,132 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:45 - 20
2010-08-18 18:13:09,133 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:46 - ------------------------
2010-08-18 18:13:09,133 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:42 - 1
2010-08-18 18:13:09,189 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:43 - name 1
2010-08-18 18:13:09,189 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:44 - 1.0
2010-08-18 18:13:09,189 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:45 - 21
2010-08-18 18:13:09,190 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:46 - ------------------------
2010-08-18 18:13:09,190 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:42 - 2
2010-08-18 18:13:09,191 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:43 - name 2
2010-08-18 18:13:09,218 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:44 - 2.0
2010-08-18 18:13:09,219 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:45 - 22
2010-08-18 18:13:09,219 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:46 - ------------------------
2010-08-18 18:13:09,219 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:42 - 3
2010-08-18 18:13:09,220 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:43 - name 3
2010-08-18 18:13:09,225 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:44 - 3.0
2010-08-18 18:13:09,226 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:45 - 23
2010-08-18 18:13:09,226 [MyResultSetTest] DEBUG com.edw.rs.test.MyResultSetTest:46 - ------------------------

this is my project structure, FYI im using Netbeans 6.9.
Netbeans 6.9 Project Structure

all i can say is, Thank You mockrunner, you saved my life.

Random Date Generator dengan Java

Iseng-iseng bikin class sederhana untuk menghasilkan suatu tanggal random yang terletak antara tanggalSebelum (Date) dan tanggalSesudah (Date). Rencananya ini mo gw pakek bwt testing aplikasi. Sorry klo classnya rada berantakan, bikinnya juga buru-buru, sembari nunggu adzan Maghrib.

package com.edw.random;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

 * @author edwin
public class RandomDateGenerator extends Thread {

	private Date sebelum;
	private Date sesudah;
	private int repetition;

	public Date getSebelum() {
		return sebelum;

	public void setSebelum(Date sebelum) {
		this.sebelum = sebelum;

	public Date getSesudah() {
		return sesudah;

	public void setSesudah(Date sesudah) {
		this.sesudah = sesudah;

	public int getRepetition() {
		return repetition;

	public void setRepetition(int repetition) {
		this.repetition = repetition;

	public RandomDateGenerator() {

	public void run() {
		try {
			for (int i = 0; i < repetition; i++) {
				System.out.println(getRandomDateBetween(sebelum, sesudah));
		} catch (InterruptedException interruptedException) {

	private synchronized Date getRandomDateBetween(Date from, Date to) {
		Calendar cal = Calendar.getInstance();

		BigDecimal decFrom = new BigDecimal(cal.getTimeInMillis());

		BigDecimal decTo = new BigDecimal(cal.getTimeInMillis());

		BigDecimal selisih = decTo.subtract(decFrom);
		BigDecimal factor = selisih.multiply(new BigDecimal(Math.random()));

		return new Date((factor.add(decFrom)).longValue());

	public static void main(String[] args) throws Exception {
		RandomDateGenerator rdg = new RandomDateGenerator();
		rdg.setSebelum(new SimpleDateFormat("dd MM yyyy").parse("17 08 1978"));
		rdg.setSesudah(new SimpleDateFormat("dd MM yyyy").parse("17 08 1990"));

may the SOURCE be with you.