Custom jdbc item writer - mnogofot.ru

Spring Batch ItemReader and ItemWriter Example DOJ Software

Jdbc.PoolingDataSource" init-method"init" destroy-method"close" property name"className" value"org. H2.jdbcx. JdbcDataSource" / property name"uniqueName" value T(System).currentTimeMillis / property name"allowLocalTransactions" value"true property name"maxPoolSize" value"2" / property name"driverProperties" props prop key"URL" jdbc:h2:mem:b;DB_close_delay-1 /prop /props /property /bean jdbc:initialize-database data-source"dataSource1" jdbc:script location"classpath meta-INF/sql/schema-h2.sql /jdbc:initialize-database jdbc:initialize-database data-source"dataSource2" jdbc:script location"classpath meta-INF/sql/schema-h2.sql /jdbc:initialize-database!- XA transaction - bean id"btmConfig" factory-method"getConfiguration" class".TransactionManagerServices bean id"BitronixTransactionManager" factory-method"getTransactionManager" class".TransactionManagerServices" depends-on"btmConfig" destroy-method"shutdown" / bean id"transactionManager" class"ansaction.

Jdbc.PoolingDataSource" init-method"init" destroy-method"close" property name"className" value"org. H2.jdbcx. JdbcDataSource" / property name"uniqueName" value T(System).currentTimeMillis / property name"allowLocalTransactions" value"true property name"maxPoolSize" value"2" / property name"driverProperties" props prop key"URL" jdbc:h2:mem:a;DB_close_delay-1 /prop /props /property /bean bean id"dataSource2" class"source. Super SimpleEntity delegates; private TaskExecutor taskExecutor; @Override public void write(final List? Extends SimpleEntity item) throws Exception for (final ItemWriter? Super SimpleEntity writer : delegates) taskExecutor. Execute(new Runnable @Override public void run try writer. Here's a simple solution utilizing a TaskExecutor and extending on the mpositeItemWriter. Package pport; import st; import emWriter; import mpositeItemWriter; import re. Task.TaskExecutor; import sert; import mpleEntity; public class ParallelCompositeItemWriter extends CompositeItemWriter SimpleEntity private List ItemWriter?

Write(item catch (Throwable t) rethrow(t private void rethrow(Throwable t) if (t instanceof RuntimeException) throw (RuntimeException) t; else if (t instanceof Error) throw (Error) t; throw new IllegalStateException(t /end for public void setTaskExecutor(TaskExecutor taskExecutor) this. Jta.JtaTransactionManager" property name"transactionManager" ref"BitronixTransactionManager" / property name"userTransaction" ref"BitronixTransactionManager" / /bean this example uses the following; Bitronix JTA to support transactions across multiple databases a very simple model of a simple entity into a simple jdbc record (the stuff in the database is very crude and just an example). Database.JdbcBatchItemWriter" property name"dataSource" ref"dataSource1 property name"sql" value"insert into test_table (idcol, stuff) values idCol stuff property name"itemSqlParameterSourceProvider" bean class"anPropertyItemSqlParameterSourceProvider /property /bean bean class"em. Database.JdbcBatchItemWriter" property name"dataSource" ref"dataSource2 property name"sql" value"insert into test_table (idcol, stuff) values idCol stuff property name"itemSqlParameterSourceProvider" bean class"anPropertyItemSqlParameterSourceProvider /property /bean /util:list util:list id"itemList" bean class"mpleEntity" constructor-arg value"stuff1 /bean bean class"mpleEntity" constructor-arg value"stuff2 /bean bean class"mpleEntity" constructor-arg value"stuff3 /bean.

TaskExecutor taskExecutor; @Override public void setDelegates(List ItemWriter? Super SimpleEntity delegates) legates delegates; tDelegates(delegates @Override public void afterPropertiesSet throws Exception terPropertiesSet tNull(taskExecutor Task executor needs to be set an example configuration would look something like this; batch:job id"simpleJob" batch:step id"ep1" batch:tasklet batch:chunk reader"reader" writer"writer" commit-interval"10 /batch:tasklet /batch:step /batch:job bean id"reader" class"eratorItemReader" constructor-arg ref"itemList /bean bean id"writer" class"rallelCompositeItemWriter" property name"delegates" ref"writerDelegates property.