package com.email.service.impl;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
@Configuration
@RefreshScope
@Data
public class FileNameConfig {
@Value("${fileName}")
private String fileName;
}
package com.email.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class ScheduledLogUrl {
@Autowired
private FileNameConfig fileNameConfig;
private String fileNameCompare;
private Integer count=0;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private LogView logView2;
@Scheduled(fixedDelay = 1000)
public void scheduledLogUrl(){
try {
if (count==0){
fileNameCompare=fileNameConfig.getFileName();
count++;
logView2.Init();
}else {
if (!fileNameConfig.getFileName().equals(fileNameCompare)){
fileNameCompare=fileNameConfig.getFileName();
System.out.println("路径发生改变=================");
stringRedisTemplate.opsForValue().set("isStatus","1");
ThreadPoolExecutor executor = new ThreadPoolExecutor(1,1,1, TimeUnit.HOURS,new SynchronousQueue<>(),new ThreadPoolExecutor.AbortPolicy());
executor.submit(new ThreadPoolExecutorDemo());
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
package com.email.service.impl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class ThreadPoolExecutorDemo implements Runnable{
private StringRedisTemplate stringRedisTemplate;
private LogView logView;
@Override
public void run() {
try {
while (true) {
stringRedisTemplate=SpringContextUtils2.getApplicationContext().getBean(StringRedisTemplate.class);
logView = SpringContextUtils2.getApplicationContext().getBean(LogView.class);
String s = stringRedisTemplate.opsForValue().get("isStatus");
if ("2".equals(s)){
Thread.sleep(5000);
logView.Init();
System.out.println("监控系统启动中=======================");
return;
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
package com.email.service.impl;
import com.email.dao.LogViewDao;
import com.email.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@RefreshScope
@Component
public class LogView {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private Map<String, LogViewDao> logViewMap;
@Value("${fileName}")
private String fileName;
@Autowired
private EmailService emailService;
public void realtimeShowLog(File logFile,String mapKey) throws IOException {
System.out.println("启动完成=======================");
LogViewDao logViewMaps1 = logViewMap.get(mapKey);
final RandomAccessFile randomFile = new RandomAccessFile(logFile, "rw");
ScheduledExecutorService exec = Executors.newScheduledThreadPool(2);
exec.scheduleWithFixedDelay(new Runnable() {
public void run() {
try {
if (logViewMaps1.getCount()!=0){
randomFile.seek(logViewMaps1.getLastTimeFileSize());
String tmp = "";
while ((tmp = randomFile.readLine()) != null) {
String s = new String(tmp.getBytes("ISO8859-1"));
System.out.println(s);
emailService.sendEmail("程序异常警告",s);
}
String isStatus = stringRedisTemplate.opsForValue().get("isStatus");
if ("1".equals(isStatus)){
System.out.println("收到关闭命令=======================");
exec.shutdown();
randomFile.close();
stringRedisTemplate.opsForValue().set("isStatus","2");
return;
}
}else {
logViewMaps1.setCount(1);
}
logViewMaps1.setLastTimeFileSize(randomFile.length());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}, 0, 5, TimeUnit.SECONDS);
exec.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
randomFile.seek(logViewMaps1.getLastTimeFileSize());
String msg=format.format(new Date())+"-----------------系统报错---------------"+mapKey;
randomFile.write(msg.getBytes());
randomFile.write("\r\n".getBytes());
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
},1,10,TimeUnit.SECONDS);
}
public void Init(){
try {
System.out.println("正在启动中=======================");
String[] split = fileName.split(",");
stringRedisTemplate.opsForValue().set("isStatus","0");
for (int i=0;i<split.length;i++) {
logViewMap=new HashMap<>();
logViewMap.put("log"+i,new LogViewDao());
File tmpLogFile = new File(split[i]);
realtimeShowLog(tmpLogFile,"log"+i);
} } catch (Exception e) {
e.printStackTrace();
}
}
}