--------配置文件
<?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");
}
}
分享到:
相关推荐
Spring定时器,也被称为Spring Boot的定时任务,是Spring框架中的一个强大功能,它允许开发者在应用程序中安排周期性任务的执行。这个功能基于Java的`java.util.concurrent.ScheduledExecutorService`,并通过Spring...
Java定时器和Spring定时器是Java开发中用于执行周期性任务的重要工具,它们在系统维护、数据同步、报告生成等场景中发挥着关键作用。本文将深入探讨这两个概念,以及如何在Spring框架中配置和使用定时器。 首先,...
Spring提供了Spring Task模块来实现定时任务,也就是我们常说的Spring定时器。这个"spring定时器简单的demo"应该包含了一个使用Spring Task实现简单定时任务的例子。 首先,Spring Task的配置通常在`...
以上内容详细介绍了Spring定时器的相关知识点,包括其基本概念、不同类型的定时器以及实际应用中的配置和实现方法。通过理解和掌握这些知识,开发者可以更高效地利用Spring框架来实现复杂的定时任务功能。
Spring定时器,也被称为Spring Boot的定时任务,是Spring框架中的一个强大功能,它允许开发者在特定的时间间隔执行任务,而无需手动管理线程。在实际的开发中,这一特性常用于实现数据清理、统计计算、发送邮件等...
### Spring 定时器的使用 #### 背景与需求 在开发应用程序时,并非所有操作都需要用户主动触发。有些任务需要系统自动执行,比如数据同步、定期备份等。例如,电力行业的集抄系统(一种自动收集电表读数的系统)...