spring 定时任务(2)

项目主要描述是:同步平台之间的数据:

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();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值