HostAliveTask
repeat
180
WebpageRespTask
repeat
180
AlarmNumTask
daily
16:45:00
import global.Global;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.apache.log4j.Logger;
/**
* schedule the checking task.
*/
public class Scheduler {
//private static final String TAG_SERVICE = "service";
private static final String TAG_ITEM = "item";
private static final String TAG_TASK = "task";
private static final String TAG_STRATEGY = "strategy";
private static final String STRATEGE_REPEAT = "repeat";
private static final String STRATEGE_DAILY = "daily";
private static final String TAG_REPEATFREQ = "repeatFreq";
private static final String TAG_DAILYRUNTIME = "dailyRuntime";
private static Logger _logger = Logger.getLogger(Scheduler.class);
void readConfig(){
try {
// First create a new XMLInputFactory
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty("javax.xml.stream.isCoalescing", true); //否则读取多行数据时出问题
// Setup a new eventReader
InputStream in = new FileInputStream(Global.SERVICE_CONFIGURE_FILE);
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
// One specification
String task = null;
String strategy = null;
Integer freq = null;// by second
// for daily job
Date firstTime = null;
Calendar calendar = Calendar.getInstance();
// Read the XML document
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
if (startElement.getName().getLocalPart().equals(TAG_ITEM)) {
/*
Iterator attributes = startElement.getAttributes();
while (attributes.hasNext()) {
attributes.next();
// skip
}
*/
}
if (event.asStartElement().getName().getLocalPart().equals(TAG_TASK)) {
event = eventReader.nextEvent();
task = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart().equals(TAG_STRATEGY)) {
event = eventReader.nextEvent();
strategy = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart().equals(TAG_REPEATFREQ)) {
event = eventReader.nextEvent();
freq = Integer.parseInt(event.asCharacters().getData());
continue;
}
if (event.asStartElement().getName().getLocalPart().equals(TAG_DAILYRUNTIME)) {
event = eventReader.nextEvent();
// hour:min:sec
Scanner scanner = new Scanner(event.asCharacters().getData());
scanner.useDelimiter(":");
int hour = scanner.nextInt();
int minute = scanner.nextInt();
int second = scanner.nextInt();
// firstTime (today)
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
firstTime = calendar.getTime();
continue;
}
}
// If we reach the end of an item element we add it to the list
if (event.isEndElement()) {
EndElement endElement = event.asEndElement();
if (endElement.getName().getLocalPart().equals(TAG_ITEM)) {
Timer timer = new Timer();
if(strategy.equals(STRATEGE_REPEAT)) {
Class> clz = Class.forName(Global.TASK_PACKAGE_PREFIX + task);
TimerTask ctr = (TimerTask)clz.newInstance();
timer.schedule(ctr, 0, freq*1000);
} else if(strategy.equals(STRATEGE_DAILY)){
Class> clz = Class.forName(Global.TASK_PACKAGE_PREFIX + task);
TimerTask ctr = (TimerTask)clz.newInstance();
timer.schedule(ctr, firstTime, Global.PERIOD_DAY);
} else {
_logger.error("Unexpected configuration option "+ strategy +" in " + Global.SERVICE_CONFIGURE_FILE);
}
}
}
}
} catch (FileNotFoundException ex) {
_logger.error(ex.getLocalizedMessage(), ex);
} catch (XMLStreamException ex) {
_logger.error(ex.getLocalizedMessage(), ex);
} catch (Exception ex) {
_logger.error(ex.getLocalizedMessage(), ex);
}
}
}
//===================================================================================================================================
Event Type
Description
StartDocument
Reports the beginning of a set of XML events, including encoding, XML version, and standalone properties.
StartElement
Reports the start of an element, including any attributes and namespace declarations; also provides access to the prefix, namespace URI, and local name of the start tag.
EndElement
Reports the end tag of an element. Namespaces that have gone out of scope can be recalled here if they have been explicitly set on their corresponding StartElement.
Characters
Corresponds to XML CData sections and CharacterData entities. Note that ignorable whitespace and significant whitespace are also reported as Character events.
EntityReference
Character entities can be reported as discrete events, which an application developer can then choose to resolve or pass through unresolved. By default, entities are resolved. Alternatively, if you do not want to report the entity as an event, replacement
text can be substituted and reported as Characters.
ProcessingInstruction
Reports the target and data for an underlying processing instruction.
Comment
Returns the text of a comment
EndDocument
Reports the end of a set of XML events.
DTD
Reports as java.lang.String information about the DTD, if any, associated with the stream, and provides a method for returning custom objects found in the DTD.
Attribute
Attributes are generally reported as part of a StartElement event. However, there are times when it is desirable to return an attribute as a standalone Attribute event; for example, when a namespace is returned as the result of an XQuery or XPath expression.
Namespace
As with attributes, namespaces are usually reported as part of a StartElement, but there are times when it is desirable to report a namespace as a discrete Namespace event.
As an example of how the event iterator API maps an XML stream, consider the following XML document:
Yogasana Vijnana: the Science of Yoga81-40-34319-4
11.50
This document would be parsed into eighteen primary and secondary events, as shown below. Note that secondary events, shown in curly braces ({}), are typically accessed from a primary
event rather than directly.
#
Element/Attribute
Event
1
version="1.0"
StartDocument
2
isCData = false
Characters
data = "\n"
IsWhiteSpace = true
3
qname = BookCatalogue:http://www.publishing.org
StartElement
attributes = null
namespaces = {BookCatalogue" -> http://www.publishing.org"}
4
qname = Book
StartElement
attributes = null
namespaces = null
5
qname = Title
StartElement
attributes = null
namespaces = null
6
isCData = false
Characters
data = "Yogasana Vijnana: the Science of Yoga\n\t"
IsWhiteSpace = false
7
qname = Title
EndElement
namespaces = null
8
qname = ISBN
StartElement
attributes = null
namespaces = null
9
isCData = false
Characters
data = "81-40-34319-4\n\t"
IsWhiteSpace = false
10
qname = ISBN
EndElement
namespaces = null
11
qname = Cost
StartElement
attributes = {"currency" -> INR}
namespaces = null
12
isCData = false
Characters
data = "11.50\n\t"
IsWhiteSpace = false
13
qname = Cost
EndElement
namespaces = null
14
isCData = false
Characters
data = "\n"
IsWhiteSpace = true
15
qname = Book
EndElement
namespaces = null
16
isCData = false
Characters
data = "\n"
IsWhiteSpace = true
17
qname = BookCatalogue:http://www.publishing.org
EndElement
namespaces = {BookCatalogue" -> http://www.publishing.org"}
18
EndDocument