5 minutes tutorial
From UbikWiki
Contents |
Setup Project
- 1) In your Eclipse workspace base folder run:
mvn archetype:create -DgroupId=com.ubikingenierie -DartifactId=MyFirstBatch -DarchetypeGroupId=com.ubikingenierie.console -DarchetypeArtifactId=UbikBarArtifact -DarchetypeVersion=6.0.0
- 2) Go to created folder
- 3) Run:
mvn eclipse:eclipse
- 4)Open Eclipse and import created project
Create a Batch
Extend com.ubikingenierie.console.apiconsole.BaseBatch:
Configure the Batch
package com.ubikingenierie; import java.text.ParseException; import org.quartz.CronTrigger; import org.quartz.Trigger; import salto.batch.appender.JdbcErrorAppender; import salto.batch.lanceur.BatchException; import salto.batch.lanceur.builder.TraitementConfiguration; import salto.batch.lanceur.init.SqlBatchStart; import com.ubikingenierie.console.apiconsole.BaseBatch; /** * */ public class MyFirstBatch extends BaseBatch { /** * */ public MyFirstBatch() { super(); } /* (non-Javadoc) * @see com.ubikingenierie.console.apiconsole.BaseBatch#createConfiguration() */ @Override public TraitementConfiguration createConfiguration() throws BatchException { try { TraitementConfiguration lanceur = new TraitementConfiguration(); lanceur.setGroup("ORDER_MANAGEMENT"); lanceur.setName("MyFirstBatch"); lanceur.setBatchAppender(new JdbcErrorAppender()); lanceur.setTrtClassName(MyFirstBatch.class.getName()); SqlBatchStart batchStart = new SqlBatchStart( "jdbc/orderManagementPool", "select COLUMN1, COLUMN2 as PAR_VALUE from TABLE1 t1 INNER JOIN TABLE2 t2 ON t1.id=t2.fk_id", false, new String[] {"COL1, COL2"}); // Work with 100 lines in memory max batchStart.setNbTraitement(100); lanceur.setBatchStart(batchStart); CronTrigger firstDayOfEveryMonth = new CronTrigger("ORDER_MANAGEMENT", "MyFirstBatch_TRIGGER", "MyFirstBatch", "ORDER_MANAGEMENT", "0 0 5 1 * ?"); lanceur.setTriggers(new Trigger[]{firstDayOfEveryMonth}); return lanceur; } catch (ParseException e) { throw new BatchException("Error creating CronTrigger ", e); } } /* (non-Javadoc) * @see com.ubikingenierie.console.apiconsole.BaseBatch#executeAfterDataEnd() */ @Override protected void executeAfterDataEnd() throws Exception { } /* (non-Javadoc) * @see com.ubikingenierie.console.apiconsole.BaseBatch#executeBeforeData() */ @Override protected void executeBeforeData() throws Exception { // TODO Auto-generated method stub } /* (non-Javadoc) * @see com.ubikingenierie.console.apiconsole.BaseBatch#executeOnBlock(java.lang.Object[]) */ @Override protected void executeOnBlock(Object[] data) throws Exception { // Code my business logic using data } }
Notice:
- SqlBatchStart : Is the data extractor that handles extraction of data from a source (Here a database) and gives it to Batch class in executeOnBlock() method
- CronTrigger : The Trigger that starts the batch (in this example, Fire at 10:15am on the last Friday of every month)
Configure Project Metadata
Edit file src/main/resources/bar.xml and modify:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE bar-configuration PUBLIC "-//Ubik-Ingenierie //DTD BAR Configuration 1.0//EN" "http://www.ubik-ingenierie.com/consolebatch/config/bar.dtd"> <bar-configuration> <!-- The display-name must be unique for all bars deployed in the console server. It is recommanded that display-name is the same as the BAR file name for better convenience. --> <display-name>ECommerceBatches</display-name> <!-- Groups of batches --> <groups> <group name="ORDER_MANAGEMENT"> <batches> <batch-classname>com.ubikingenierie.MyFirstBatch</batch-classname> </batches> </group> </groups> <!-- Resources --> <bar-resources> <bar-resource name="jdbc/orderManagementPool" type="javax.sql.DataSource" /> </bar-resources> <!-- Log4J Configuration --> <logging-file>log.xml</logging-file> </bar-configuration>
Notice:
- jdbc/orderManagementPool : The pool that will be used to access database
- group : You can group batches business logic
Develop your business Logic
Code at least executeOnBlock() method:
- The method has only one parameter which contains part of the result of the SQL Query execution
- Batch server handles extraction in data block of 100 lines
And optionally :
- executeBeforeData : Called at batch startup before Query execution
- executeAfterDataEnd : Called at end of batch
Create the Deployment unit
Run:
mvn clean deploy
This generates a BAR that you can give to your deployment team for PRODUCTION deployment.
That's it
Now here is what you get:
- Automatic error management
- Multithreading of your batch
- Hot management of your batch configuration
- History of your batch execution and its status
- Alert if your batch takes too much time to execute
- ... see features