简体   繁体   English

Spring Batch和Spring集成

[英]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. 我想使用Spring Batch和Spring Integration从数据库导入数据,并将它们写入文件,然后通过ftp将其传输到远程服务器。

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? 是否可以在不创建各自的域对象的情况下使用Spring Batch和Integration?

Absolutely. 绝对。 You can use either of the JDBC ItemReader s or the JPA ItemReader with a ColumnMapRowMapper to retrieve a Map of the result set. 您可以使用的JDBC ItemReader S或JPA的ItemReaderColumnMapRowMapper检索Map的结果集。 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). 您可以非常简单地使用FlatFileItemWriter以所需的任何格式输出数据(使用提供的类很容易定界;固定宽度表示编写一个类以将Map转换为固定宽度字符串)。

I do this pretty often with Spring Batch and it's pretty much just a matter of wiring things up. 我经常在Spring Batch中执行此操作,这几乎只是将事情连接起来的问题。

Aside from defining a resource, data source, and providing the SQL, this (untested) configuration would pretty much do exactly what you're asking: 除了定义资源,数据源和提供SQL之外,这种(未经测试的)配置几乎可以完全满足您的要求:

<?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>

With my current experiance with Spring Batch. 根据我目前对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. 如果要使用JdbcTemplate或旧方法处理数据库调用,则可以在不同的应用程序层中重用相同的POJO。 However, if you paln to use JAXB, JIXB, etc for xml and Hinernate, MyBatis, etc for persiatance then you will run into issues. 但是,如果您选择将JAXB,JIXB等用于xml,将Hinernate,MyBatis等用于持久性,则会遇到问题。 The POJOs get tighly coouple with underlying specific annotations and api contraints. POJO与潜在的特定注释和api约束紧密相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM