项目主要描述是:同步平台之间的数据:
1:Spring applicationContext.xml配置文件如下(只描述了定时器配置):
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 action名称-->
<property name="targetObject">
<ref bean="synchroDataAction" />
</property>
<!-- 调用类中的方法 :action中的方法execute -->
<property name="targetMethod">
<value>execute</value>
</property>
</bean>
<!-- 触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 指向我们的任务 -->
<property name="jobDetail" ref="jobtask" />
<!-- 每周一上午1:00 执行 -->
<!-- <property name="cronExpression" value="0 0 1 ? * MON" /> -->
<!-- 测试五秒执行一次-->
<!-- <property name="cronExpression" value="*/50 * * * * ?" /> -->
<property name="cronExpression" value="0 0 */1 * * ?" />
</bean>
<!-- 调度器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no">
<property name="triggers">
<list>
<!-- 触发器列表 -->
<ref bean="cronTrigger" />
</list>
</property>
</bean>
2:action层简单的调用Service层的方法:
synchroDataService.synchrodata();
3:service层:
InputStream inputStream = null;
OutputStream fos = null;
try{
SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// inputStream = new FileInputStream(this.getClass().getClassLoader().getResource("webData.properties").getPath());
inputStream = this.getClass().getClassLoader().getResourceAsStream("webData.properties");
Properties properties = new Properties();
properties.load(inputStream);
String updateID = new String(properties.getProperty("updateID").getBytes("ISO8859_1"), "UTF-8");
String registerID = new String(properties.getProperty("registerID").getBytes("ISO8859_1"), "UTF-8");
if(inputStream != null){
inputStream.close();
}
String code_update[] = { "SYS302001", "SYS302003" };
String code_register[] = { "SYS302005", "SYS302006" };
fos = new FileOutputStream(this.getClass().getClassLoader().getResource("webData.properties").getPath());
if (StringUtils.isNotEmpty(updateID)) {
List<PlatformUser> listU = getListData(code_update, updateID,sdformat);
dataInsert(listU,sdformat);
properties.setProperty("updateID", MaxID);
}
if (StringUtils.isNotEmpty(registerID)) {
List<PlatformUser> listR = getListData(code_register, registerID,sdformat);
dataInsert(listR,sdformat);
properties.setProperty("registerID", MaxID);
}
properties.store(fos, null);
}finally{
if(fos != null){
fos.flush();
fos.close();
}
}