Tuesday, May 22, 2012

Different Serialization - Java Serialization, Jboss Serilaization & Externalization


Hi I have just compared 3 serialization strategies - only in terms of payload data and here are the results entitiy includes - Few Longs, Strings, BigDecimal, Dates , Enums etc Default Java Serialization
 @Test
 public void testJavaSerialization() throws IOException, ClassNotFoundException {
        Trade trade = new BuyTrade();
        trade.setOid(128245);
        trade.setVersion(10);
        
        trade.setOrderId(13212);
        trade.setProductId(876);
        trade.setAccountId(1234L);
        
        trade.setDateMatched(org.joda.time.DateTime.now());
        
        trade.setPrice(new BigDecimal("213.23"));
        trade.setVolume(10);
        trade.setState(TradeState.PENDING);
        trade.setOpenVolume(10);
        trade.setType(TradeType.SETTLEMENT);
 
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream oout = new ObjectOutputStream(bout);
        oout.writeObject(trade);
        byte[] bytes = bout.toByteArray();
        System.out.println("Java Serialization :" + bytes.length);
        
        Trade deSerializedTrade = null;
        
        //deserialze
        ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
        ObjectInputStream out = new ObjectInputStream(bin);
        deSerializedTrade = (Trade) out.readObject();
        
   
 }

Default Java Serialization - 1127 bytes Jboss Serialization
 @Test
 public void testJbossSerialization() throws IOException, ClassNotFoundException {
        Trade trade = new BuyTrade();
        trade.setOid(128245);
        trade.setVersion(10);
        
        trade.setOrderId(13212);
        trade.setProductId(876);
        trade.setAccountId(1234L);
        
        trade.setDateMatched(org.joda.time.DateTime.now());        
        trade.setPrice(new BigDecimal("213.23"));
        trade.setVolume(10);
        trade.setState(TradeState.PENDING);
        trade.setOpenVolume(10);
        trade.setType(TradeType.SETTLEMENT);
 
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        JBossObjectOutputStream oout = new JBossObjectOutputStream(bout);
        oout.writeObject(trade);
        byte[] bytes = bout.toByteArray();
        System.out.println("JBoss Serialization :" + bytes.length);
        
        Trade deSerializedTrade = null;
        
        //deserialze
        ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
        JBossObjectInputStream out = new JBossObjectInputStream(bin);
        deSerializedTrade = (Trade) out.readObject();
        
    }

Jboss Serialization – 1125 bytes Externalization
 @Test
 public void testExternalizableSerialization() throws IOException, ClassNotFoundException {
        ExternalizedTrade trade = new ExternalizedTrade();
        trade.setOid(128245);
        trade.setVersion(10);
        trade.setWritable(false);
        
        trade.setOrderId(13212);
        trade.setProductId(876);
        trade.setAccountId(1234L);
        
        trade.setDateMatched(org.joda.time.DateTime.now());
        trade.setPrice(new BigDecimal("213.23"));
        trade.setVolume(10);
        trade.setState(TradeState.PENDING);
        trade.setOpenVolume(10);
        trade.setType(TradeType.SETTLEMENT);
 
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream oout = new ObjectOutputStream(bout);
        oout.writeObject(trade);
        byte[] bytes = bout.toByteArray();
        System.out.println("Externalizable Serialization :" + bytes.length);
        
        //deserialze
        Trade deSerializedTrade = null;
        ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
        ObjectInputStream out = new ObjectInputStream(bin);
        deSerializedTrade = (ExternalizedTrade) out.readObject();
  
 }


 @Override
 public void readExternal(ObjectInput input) throws IOException,
   ClassNotFoundException {
  setBias(OrderBias.lookupOrderBiasById((new Long(input.readByte()))));

  setOid(input.readLong());
  setVersion((new Long(input.readInt())));
  setWritable(input.readBoolean());
  
  setOrderId(input.readLong());
  setProductId((new Long(input.readInt())));
  setAccountId(input.readLong());
  setDateMatched((org.joda.time.DateTime)input.readObject());
  setPrice(new BigDecimal((String)input.readObject()));
  setVolume((new Long(input.readInt())));
  setState(TradeState.lookupTradeStateById((new Long(input.readByte()))));
  setOpenVolume((new Long(input.readInt())));
  setType(TradeType.lookupTradeStateById((new Long(input.readByte()))));
  
 }
 
 @Override
 public void writeExternal(ObjectOutput output) throws IOException {
  output.writeByte(((Long)getBias().getId()).intValue());
  
  output.writeLong(getOid());
  output.writeInt(((Long)getVersion()).intValue());
  output.writeBoolean(isWritable());

  output.writeLong(getOrderId());
  output.writeInt(((Long)getProductId()).intValue());
  output.writeLong(getAccountId());
  output.writeObject(getDateMatched());
  output.writeObject(getPrice().toString());
  output.writeInt(((Long)getVolume()).intValue());
  output.writeByte(((Long)getState().getId()).intValue());
  output.writeInt(((Long)getOpenVolume()).intValue());
  output.writeByte(((Long)getType().getId()).intValue());
 }

 
Externalizable - 999 bytes

  Notes :

1. BigDecimal - when serialized as Strings has 60% less payload when compared to bytes.

 2. Date - Calendar is a very heavy to serialize when compared to its counterpart Joda Date time

 3. Enums - can be write efficiently serialized. Ref :

Wednesday, May 16, 2012

Spring Configurations for JTA Transaction - XA datasource (Mysql) + Jboss Transaction manager(Arjuna) + Spring Transaction annotations + DBCP Connection pooling

Mysql complaint mysql xa datasource and Atomikos connection pooling

     
   
   
   
     
  
   
        
        
    

    
     
     
    
    
    
        
        
        
        
        false
        true
    

    
    
        
  
    


Jboss transaction manager settings

 
 


 
 

 


 
  
   
  
  
   
  
 

Maven dependencies for dbcp and jboss transaction manager

              4.16.4.Final

  
    commons-dbcp
    commons-dbcp
    1.4
  

  
   org.jboss.jbossts
   jbossjta
   ${jboss.jta.version}
  




Friday, May 11, 2012

Spring Configuratiosn for JTA Transaction - XA datasource (Mysql) + Atomikos Transaction manager + Spring Transaction annotations





Mysql complaint mysql xa datasource  and Atomikos connection pooling



 <bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
  init-method="init" destroy-method="close">
  <property name="uniqueResourceName" value="MAIN-ATOMIKOS-CONNECTION" />
  <property name="poolSize" value="${initialSize}" />
  <property name="xaDataSourceClassName"
   value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
  <property name="xaProperties" ref="databaseProperties" />
  <property name="testQuery" value="select 1" />
 </bean>




database.properties


<util:properties id="databaseProperties"
  location="classpath:database.properties" />



url=jdbc\:mysql\://localhost\:3306/<DBNAME>
user=root
password=
autoReconnect=true
autoReconnectForConnectionPools=true
autoReconnectForPools=true
pinGlobalTxToPhysicalConnection=true


Transaction specific Config

Following settings are for enabling transactions

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" 
  init-method="init" destroy-method="close"> 
           <property name="forceShutdown"> <value>true</value> </property> 
</bean>

<bean id="atomikosUserTransaction" 
  class="com.atomikos.icatch.jta.UserTransactionImp">
            <property name="transactionTimeout" 
  value="300"/> 
</bean> 

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> 
  <property name="transactionManager"> <ref bean="atomikosTransactionManager" 
  /> </property> 
              <property name="userTransaction"> <ref bean="atomikosUserTransaction" 
  /> </property> 
</bean> 




Just adding spring Spring annotation - will now enable transaction on required methods.


 
Free Domain Names @ .co.nr!