Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

java - Spring Batch and Spring Integration

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...