Spring定时器

--------配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- 配置触发器 -->
<bean id="hour" class="com.hh.dao.HourDao" />

<bean id="method"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="hour"/>
<property name="targetMethod" value="evaluate"/>
</bean>

<bean id="triger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="method" />
<property name="cronExpression">
<value>0,15,30,45 * * * * ?</value>
</property>
</bean>

<!-- 启动触发器 -->
<bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="triger" />
</list>
</property>
</bean>


</beans>

----------定时调用配置文件
package com.hh.dao;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hh.entity.HourBean;

public class HourDao {
private HourBean hb;
XmlDao x=new XmlDao();
ApplicationContext context=new ClassPathXmlApplicationContext("Config.xml");
public HourDao(){
hb=(HourBean) context.getBean("con");
}

//判断(设置)尖峰平谷.有无功表码.功率(值)
public void evaluate(){
XmlDao x=new XmlDao();
hb.setTime(new Date());

int nowTime=new Date().getHours();
//谷
if(nowTime>=10 && nowTime<11){
hb.setValley(hb.getValley()+new Float(30+Math.random()*0.7).floatValue());
hb.setUnValley(hb.getUnValley()+new Float(20+Math.random()*0.6).floatValue());
}
//平
else if(nowTime>=11 && nowTime <12){
hb.setPing(hb.getPing()+new Float(Math.random()*0.8).floatValue());
hb.setUnPing(hb.getUnPing()+new Float(Math.random()*1).floatValue());
}
//峰
else if(nowTime>=12 && nowTime<13){
hb.setApex(hb.getApex()+new Float(Math.random()*3).floatValue());
hb.setUnApex(hb.getUnApex()+new Float(Math.random()*2).floatValue());
}
//尖
else{
hb.setNeedle(hb.getUnNeedle()+new Float(Math.random()*5).floatValue());
hb.setUnNeedle(hb.getNeedle()+new Float(Math.random()*4).floatValue());
}

//有功表码
hb.setPowerNum(new Float(hb.getNeedle()+hb.getApex()+hb.getPing()+hb.getValley()).floatValue());
//无功表码
hb.setUnPowerNum(new Float(hb.getUnNeedle()+hb.getUnApex()+hb.getUnPing()+hb.getUnValley()).floatValue());
//有功功率
hb.setPower(new Float(Math.random()*20).floatValue());
//无功功率
hb.setUnPower(new Float(-20+Math.random()*10).floatValue());

//设置ABC电压电流
hb.setAVoltage(new Float(220+Math.random()*100).floatValue());
hb.setBVoltage(new Float(220+Math.random()*100).floatValue());
hb.setCVoltage(new Float(220+Math.random()*100).floatValue());

hb.setACurrent(new Float(20+Math.random()*50).floatValue());
hb.setBCurrent(new Float(20+Math.random()*50).floatValue());
hb.setCCurrent(new Float(20+Math.random()*50).floatValue());


try {
Document doc=x.addRoot(hb);
this.keepData("E:\\workspace7.0\\变压器时数据\\"+new SimpleDateFormat("yyyy").format(new Date())+"年"
+new SimpleDateFormat("MM").format(new Date())
+"月"+new SimpleDateFormat("dd").format(new Date())+"日"+hb.getTranId()+"小时数据"+".xml", doc, hb);
System.out.println("OK!");
} catch (Exception e) {
e.printStackTrace();
}

}
//写XML
public void keepData(String file,Document doc,HourBean hb)throws Exception{
if(new File(file).exists()){
SAXReader read=new SAXReader();

Document document=read.read(new File(file));

Element ele=document.getRootElement();

x.createHourEle(ele,hb);

FileWriter fw=new FileWriter(new File(file));
OutputFormat of=OutputFormat.createPrettyPrint();
of.setEncoding("gbk");
of.setIndent(true);
XMLWriter writer=new XMLWriter(fw,of);
writer.write(document);
writer.flush();
writer.close();
}else{
FileWriter fw=new FileWriter(new File(file));
OutputFormat of=OutputFormat.createPrettyPrint();
of.setEncoding("gbk");
of.setIndent(true);
XMLWriter writer=new XMLWriter(fw,of);
writer.write(doc);
writer.flush();
writer.close();
}
}
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("HourTrigger.xml");


}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值