Today am going to explain how to insert fixed length records
into db.
Here is the example:
For processing fixed length file, we need BeanIO or camel BeanIO,
in this example am using bean IO.
Bean IO is open source frame work for processing all type of
files like fixed length, csv, xml, separated by special characters.
1.
Fixed length file looks like below
Based on file we have to define pojo class like below:
package order.sample.com.orderitemsdb;
public class OrerItems {
String
ORDERNUMBER;
String
DATE1;
String
DATE2;
String
AMOUNT;
String
REFNUM;
…………………,
…………………,
…….etc.
Getter and Setter methods here
}
Add BeanIO dependency in pom.xml

Mapping File for converting fixed length file to xml
Mapping file looks like below, in this mapping file we have to create the stream like input stream
and out stream and each stream use to refer pojo class for fetching property
values and we have to mention what is root element for input record .here we
can give any name. But if you’re getting inputs from service we have to
mentioned exact name of xsd root element.
Call Mapping File from Exchange Process Class
Here am using camel processor for calling java class.
package order.sample.com.orderitemsdb;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.beanio.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.io.*;
import java.util.ArrayList;
import javax.inject.Named;
@Named("OrderInput")
public class OrderInputFileProcessor implements Processor {
//
public static void main(String[] args) throws Exception
@SuppressWarnings("null")
@Override
public
void process(Exchange exchange) throws Exception {
System.out.println("OrderInputFileProcessor fixedlenght
File coverting into xml file");
//
create a StreamFactory
StreamFactory
factory = StreamFactory.newInstance();
//
load the mapping file
factory.load(/order/MappingFile/OrderPaymentInboundMapping.xml);
//
use a StreamFactory to create a BeanReader
BeanReader
in = factory.createReader("OrderPaymentInboundRequest",new
File(/u01/app/jboss/order/FIN/order.txt));
// OrderPaymentInboundRequest this name should match
with mapping file input stream name
Object record = null;
ArrayList< OrerItems >
collectionlist = null;
collectionlist = new ArrayList< OrerItems
>();
// OrerItems collection
= null;
// read records from "input fixed
length file"
Configuration cfg = new Configuration();
cfg.configure("OrderInbound.cfg.xml");
Here am using hibernate for
inserting records into DB.
SessionFactory sessionfactory =
cfg.buildSessionFactory();
Session session =
sessionfactory.openSession();
while ((record = in.read()) != null) {
// process each record
OrerItems
collection = (OrerItems) record;
if ("OrderPaymentRequest".equals(in.getRecordName()))
Note: OrderPaymentRequest this name should match with input record
in the mapping file.
{
// process the OrerItems
collectionlist.add(collection);
try{
session.save(collection);
session.flush();
session.beginTransaction().commit();
session.clear();
}
catch(Exception e)
{
System.out.println("----------ErrorTrace-------------"+e);
}
}
} try{
session.close();
/*Transaction tx =
session.beginTransaction();
tx.commit();
session.close();
sessionfactory.close();*/
}
catch(Exception e)
{
System.out.println("----------ErrorTrace-------------"+e);
}
}
Deploying the Project into the JBOSS Server
Before deploying this project add jboss-deployment-structure.xml in
below folder structure

Adding external jar files into the sever
If you want add any external jar files into the jboss eap
server follow the below steps.
Go to jboss eap folder -> module folder-> system->
\jboss-eap-6.1\modules\system\layers\base
In above location you can see org folder inside create BeanIO
folder next create main inside this folder place BeanIO jar.
Next create module.xml like below:

Note: if you are getting class not found exception means some
dependency missed in your module folder….
Hope you guys understand this code.
Cheers,
Madhu