/**
* 启动时间的改变
* @param id
*/
@Override
public void changeConfig(Serializable id) {
SpiderList spiderList = spiderListMapper.get(id);
String method = spiderList.getMethod();
String startTime = spiderList.getStartTimeSet();
String[] split = startTime.split(",", 4);
String string3 = null;
for (int i = 0; i < split.length; i++) {
char[] charArray = split[i].toCharArray();
String cron = ConfigUtil.charToString(charArray);
// char temp = charArray[0];
// charArray[0] = charArray[3];
// char temp2 = charArray[1];
// charArray[1] = charArray[4];
// charArray[3] = temp;
// charArray[4] = temp2;
// string3 = new String(charArray);
// String replace = charToString.replace(":", " ");
// String cron = "00 " + replace + " * * ?";
// 读取XML文件,获得document对象
SAXReader reader = new SAXReader();
URI uri = null;
Document document = null;
try {
uri = Thread.currentThread().getContextClassLoader().getResource("applicationContext-quartz.xml")
.toURI();
// File file =new );
document = reader.read(new File(uri));
} catch (Exception e) {
e.printStackTrace();
}
// 获取文档的根节点
Element root = document.getRootElement();
// 取得某个节点的子节点.
List<Element> childElements = root.elements("scheduled-tasks");
for (Element child : childElements) {
List<Element> elements = child.elements();
for (Element element : elements) {
Attribute cronAtt = element.attribute("cron");
Attribute methAtt = element.attribute("method");
String method1 = methAtt.getValue();
if (method.equals(method1)) {
element.setAttributeValue("cron", cron);
}
}
}
// 重新写入xml文件
FileWriter newFile;
try {
newFile = new FileWriter(new File(uri));
XMLWriter newWriter = new XMLWriter(newFile);
newWriter.write(document);
newWriter.close();
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String currentTimeStr = sdf.format(date);
Date currentTimeDate = sdf.parse(currentTimeStr);
Date coreTime = sdf.parse(split[i]);
long time = coreTime.getTime();
long time2 = currentTimeDate.getTime();
if (currentTimeDate.getTime() < coreTime.getTime()) {
//循环 如果当前的时间 比定时时间小 就是 在定时时间之前 就把 数据库定时时间 设置到定时的xml文件中
AbstractRefreshableApplicationContext abstractRefreshableApplicationContext = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
abstractRefreshableApplicationContext.refresh();
return;
}else if(currentTimeDate.getTime() > coreTime.getTime()) {
System.out.println("时间初始化设置!!!");
//如果当前的时间都比 定时时间大 那么就把 定时的时间 设置为第一个 最早的时间
char[] splitInit = split[0].toCharArray();
String coreInit = ConfigUtil.charToString(splitInit);
System.out.println("coreInit======="+coreInit);
for (Element child : childElements) {
List<Element> elements = child.elements();
for (Element element : elements) {
Attribute cronAtt = element.attribute("cron");
Attribute methAtt = element.attribute("method");
String method1 = methAtt.getValue();
if (method.equals(method1)) {
element.setAttributeValue("cron", coreInit);
AbstractRefreshableApplicationContext abstractRefreshableApplicationContext = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
abstractRefreshableApplicationContext.refresh();
return;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}