kullanarak XML dosyasına yazamıyorum şimdi yavaş yavaş Spring Batch
beğeniyorum. İki tablo verisini (Customer
ve Employee
) okuyan ve XML dosyasına yazan Spring Batch
kodunu geliştiriyorum. Kod geliştirdim ve ana programımı çalıştırdığımda boş dosya geliyor görüyorum. Niye ya ?iki toplu tablo verisi bahar Batch
Customers.xml
<?xml version="1.0" encoding="UTF-8"?><customers></customers>
aşağıda referans için geliştirilen kod: 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-kompozit maddelik
Customer.javapublic 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
}
-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 !!");
}
}