2016-04-06 13 views
0

Ich mag die Spring Batch jetzt allmählich. Ich entwickle Spring Batch Code, der zwei Tabellen Daten (Customer und Employee) liest und in XML-Datei schreibt. Ich entwickelte Code und wenn ich mein main() Programm ausführe, sehe ich leere Datei kommt. Warum ?nicht in der Lage, zwei Datenbanktabellen Daten in XML-Datei schreiben mit Spring Batch

customers.xml

<?xml version="1.0" encoding="UTF-8"?><customers></customers> 

Der Code, den ich als Referenz unten entwickelt: Customer.java

public class Customer implements Serializable{ 
    private static final long serialVersionUID = 1L; 

    private Integer customerNumber; 
    private String customerName; 
    private String contactLastName; 
    private String contactFirstName; 
    private String phone; 
    private String addressLine1; 
    private String addressLine2; 
    private String city; 
    private String state; 
    private String postalCode; 
    private String country; 
    private Integer salesRepEmployeeNumber; 
    private Double creditLimit; 
    // setters and getters 
} 

Employee.java

public class Employee implements Serializable{ 
    private static final long serialVersionUID = 1L; 

    private Integer employeeNumber; 
    private String lastName; 
    private String firstName; 
    private String extension; 
    private String email; 
    private String officeCode; 
    private Integer reportsTo; 
    private String jobTitle; 
    // setters and getters 
} 

xml-jdbc-Composite-Artikel -reader-job.xml

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

    <import resource="classpath:context-datasource.xml" /> 

    <!-- JobRepository and JobLauncher are configuration/setup classes --> 
    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" /> 

    <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> 
     <property name="jobRepository" ref="jobRepository" /> 
    </bean> 


    <!-- Step will need a transaction manager --> 
    <bean id="transactionManager" 
     class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" /> 


    <!-- =========================================================== -->  
    <job id="compositeJdbcReaderJob" xmlns="http://www.springframework.org/schema/batch"> 
     <step id="compositeJdbcReaderStep" next="compositeJdbcReaderStep2"> 
      <tasklet> 
       <chunk reader="itemReader1" writer="itemWriter1" commit-interval="5" /> 
      </tasklet> 
     </step> 

     <step id="compositeJdbcReaderStep2"> 
      <tasklet> 
       <chunk reader="itemWriter2" writer="itemWriter2" commit-interval="5" /> 
      </tasklet> 
     </step> 
    </job> 



    <!-- ========== ItemReader =============== --> 
    <bean id="itemReader1" class="org.springframework.batch.item.database.JdbcCursorItemReader"> 
     <property name="dataSource" ref="dataSource" /> 

     <property name="saveState" value="true" /> 

     <property name="sql"> 
      <value> 
       <![CDATA[ ${select.sql.customers} ]]> 
      </value> 
     </property> 
     <property name="rowMapper"> 
      <bean class="com.common.batch.mapper.CustomerMapper" /> 
     </property> 
    </bean> 


    <bean id="itemReader2" class="org.springframework.batch.item.database.JdbcCursorItemReader"> 
     <property name="dataSource" ref="dataSource" /> 

     <property name="saveState" value="true" /> 

     <property name="sql"> 
      <value> 
       <![CDATA[ ${select.sql.employees} ]]> 
      </value> 
     </property> 
     <property name="rowMapper"> 
      <bean class="com.common.batch.mapper.EmployeeMapper" /> 
     </property> 
    </bean> 


    <!-- ItemWritter --> 
    <bean id="itemWriter1" class="org.springframework.batch.item.xml.StaxEventItemWriter"> 
     <property name="resource" value="file:xml/customers.xml" /> 

     <property name="marshaller" ref="customerUnmarshaller" /> 

     <property name="rootTagName" value="customers" /> 
    </bean> 

    <bean id="itemWriter2" class="org.springframework.batch.item.xml.StaxEventItemWriter"> 
     <property name="resource" value="file:xml/customers.xml" /> 

     <property name="marshaller" ref="employeeUnmarshaller" /> 

     <property name="rootTagName" value="employees" /> 
    </bean> 

    <!-- ======= Employee Unmarshaller ======== --> 
    <bean id="employeeUnmarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"> 
     <property name="aliases"> 
      <util:map id="aliases"> 
       <entry key="employee" value="com.common.batch.model.Employee" /> 
      </util:map> 
     </property> 
    </bean> 

     <!-- ======= Customer Unmarshaller ======== --> 
    <bean id="customerUnmarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"> 
     <property name="aliases"> 
      <util:map id="aliases"> 
       <entry key="customer" value="com.common.batch.model.Customer" /> 
      </util:map> 
     </property> 
    </bean> 


    <bean id="itemCustomerProcessor" class="com.common.batch.processor.CustomerProcessor" /> 
    <bean id="itemEmployeeProcessor" class="com.common.batch.processor.EmployeeProcessor" /> 
</beans> 

CompositeXMLMain.java

public class CompositeXMLMain { 
    @SuppressWarnings("resource") 
    public static void main(String[] args) { 
     ApplicationContext context = new ClassPathXmlApplicationContext("composite/xml-jdbc-composite-item-reader-job.xml"); 

     JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); 
     Job job = (Job) context.getBean("compositeJdbcReaderJob"); 

     JobExecution execution; 
     try { 
      execution = jobLauncher.run(job, new JobParameters()); 
      System.out.println("Job Exit Status : "+ execution.getStatus()); 

     } catch (JobExecutionAlreadyRunningException | JobRestartException 
       | JobInstanceAlreadyCompleteException | JobParametersInvalidException e) { 
      System.out.println(e.getMessage()); 
      e.printStackTrace(); 
     } 
     System.out.println("Done !!"); 
    } 
} 

Antwort

0

ich einen kleinen Fehler machen musste, um das Problem zu beheben, müssen einfach itemWriter2 zu itemReader2

<job id="compositeJdbcReaderJob" xmlns="http://www.springframework.org/schema/batch"> 
     <step id="compositeJdbcReaderStep" next="compositeJdbcReaderStep2"> 
      <tasklet> 
       <chunk reader="itemReader1" writer="itemWriter1" commit-interval="5" /> 
      </tasklet> 
     </step> 

     <step id="compositeJdbcReaderStep2"> 
      <tasklet> 
       <chunk reader="itemWriter2" writer="itemWriter2" commit-interval="5" /> 
      </tasklet> 
     </step> 
    </job> 

zu

<job id="compositeJdbcReaderJob" xmlns="http://www.springframework.org/schema/batch"> 
     <step id="compositeJdbcReaderStep" next="compositeJdbcReaderStep2"> 
      <tasklet> 
       <chunk reader="itemReader1" writer="itemWriter1" commit-interval="5" /> 
      </tasklet> 
     </step> 

     <step id="compositeJdbcReaderStep2"> 
      <tasklet> 
       <chunk reader="itemReader2" writer="itemWriter2" commit-interval="5" /> 
      </tasklet> 
     </step> 
    </job> 
ändern