2
votes

I want to use Spring Batch and Spring Integration to import data from database and write them into a file and ftp them to a remote server.

But I guess my problem is I don't want to create Domain Object for my table. My queries are random and I want something that just reads the data and writes it to files and transfer.

Can I use Spring Batch and Integration without creating respective domain objects?

2

2 Answers

5
votes

Absolutely. You can use either of the JDBC ItemReaders or the JPA ItemReader with a ColumnMapRowMapper to retrieve a Map of the result set. You can use the FlatFileItemWriter pretty simply to output the data in whatever format you like (delimited being very easy with the provided classes; fixed width means writing one class to translate the Map to your fixed width string).

I do this pretty often with Spring Batch and it's pretty much just a matter of wiring things up.

Aside from defining a resource, data source, and providing the SQL, this (untested) configuration would pretty much do exactly what you're asking:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

    <job-repository id="jobRepository"
        data-source="jobDataSource"/>

    <beans:bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="jobDataSource" />

    <beans:bean id="extractReader" scope="step"
        class="org.springframework.batch.item.database.JdbcCursorItemReader">
        <beans:property name="dataSource" ref="appDataSource" />
        <beans:property name="rowMapper">
            <beans:bean
                class="org.springframework.jdbc.core.ColumnMapRowMapper" />
        </beans:property>
        <beans:property name="sql">
            <beans:value>
                . . .
            </beans:value>
        </beans:property>
    </beans:bean>
    <beans:bean id="extractWriter"
        class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
        <beans:property name="resource" ref="fileResource" />
        <beans:property name="lineAggregator">
            <beans:bean
                class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                <beans:property name="delimiter">
                    <util:constant
                        static-field="org.springframework.batch.item.file.transform.DelimitedLineTokenizer.DELIMITER_TAB" />
                </beans:property>
                <beans:property name="fieldExtractor">
                    <beans:bean
                        class="org.springframework.batch.item.file.transform.PassThroughFieldExtractor" />
                </beans:property>
            </beans:bean>
        </beans:property>
    </beans:bean>

    <job id="extractJob" restartable="true">
        <step id="extractStep" >
            <tasklet>
                <chunk reader="extractReader" writer="extractWriter"
                    commit-interval="100" />
            </tasklet>
        </step>
    </job>

</beans:beans>
0
votes

With my current experiance with Spring Batch. If you are going do handle your database call using JdbcTemplate or old way then same POJOs can be reused in different application layers. However, if you paln to use JAXB, JIXB, etc for xml and Hinernate, MyBatis, etc for persiatance then you will run into issues. The POJOs get tighly coouple with underlying specific annotations and api contraints.