<code>package org.ramer.diary.util;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.
Date
;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.ramer.diary.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
@Controller
public
class
ScheduleWork
implements
ServletContextListener {
private
long getTimeMillis(String time) {
try
{
DateFormat dateFormat =
new
SimpleDateFormat(
"yy-MM-dd HH:mm:ss"
);
DateFormat dayFormat =
new
SimpleDateFormat(
"yy-MM-dd"
);
Date
curDate = dateFormat.parse(dayFormat.format(
new
Date
()) +
" "
+ time);
return
curDate.getTime();
}
catch
(Exception e) {
e.printStackTrace();
}
return
0;
}
@Override
public
void contextDestroyed(ServletContextEvent arg0) {
}
@Autowired
TopicService topicService;
@Value(
"#{diaryProperties['tags.xml.position']}"
)
private
String file;
@Override
public
void contextInitialized(ServletContextEvent servletContextEvent) {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
long oneDay = 10 * 1000;
SimpleDateFormat simpleDateFormat =
new
SimpleDateFormat(
"HH:mm:ss"
);
long initDelay = getTimeMillis(simpleDateFormat.format(
new
Date
()))
- System.currentTimeMillis();
initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;
executor.scheduleAtFixedRate(() -> {
System.out.println(
"----------start update tags-----------"
);
System.out.println(topicService);
System.out.println(file);
List<String> tags = topicService.getAllTags();
StringBuilder stringBuilder =
new
StringBuilder();
for
(String string : tags) {
stringBuilder.append(string +
";"
);
}
String[] strings = stringBuilder.toString().split(
";"
);
List<String> tagslist = Arrays.asList(strings);
tagslist =
new
ArrayList<>(tagslist);
for
(int i = 0; i < tagslist.size(); i++) {
for
(int j = i + 1; j < tagslist.size(); j++) {
if
(tagslist.get(i).equals(tagslist.get(j))) {
tagslist.remove(j);
j--;
}
}
}
tags = tagslist;
System.out.println(
"tags in database: "
);
for
(String string : tags) {
System.out.println(
"\t"
+ string);
}
List<String> tagsInFile =
new
ArrayList<>();
try
{
tagsInFile = FileUtils.readTag(file, servletContextEvent.getServletContext());
}
catch
(Exception e) {
System.out.println(
"Exception ScheduleWork(Line 111)"
);
e.printStackTrace();
}
System.out.println(
"文件中的tags: "
);
for
(String string : tagsInFile) {
System.out.println(
"\t"
+ string);
}
for
(int i = 0; i < tags.size(); i++) {
if
(!tagsInFile.contains(tags.get(i))) {
tagsInFile.add(tags.get(i));
i++;
}
}
System.out.println(
"update tags: "
);
for
(String string : tagsInFile) {
System.out.println(
"\t"
+ string);
}
try
{
FileUtils.writeTag(tagsInFile, file, servletContextEvent.getServletContext());
}
catch
(Exception e) {
System.out.println(
"Exception ScheduleWork(Line 129)"
);
e.printStackTrace();
}
}, initDelay, oneDay, TimeUnit.MILLISECONDS);
}
}</code>