提交 1bc40fee 编写于 作者: O o2null

修正log引起的console命令无法执行

上级 5f565993
......@@ -9,71 +9,74 @@ import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.channels.FileLock;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.tools.DateTools;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
public class Shadow {
private static boolean tag = true;
private static Logger logger = LoggerFactory.getLogger(Shadow.class);
public static void main(String[] args) throws Exception {
tag = true;
File logFile = new File(Config.base(), "logs/" + DateTools.format(new Date(), "yyyy_MM_dd") + ".out.log");
public static void main(String... args) throws Exception {
File logFile = new File(Config.base(), "logs/out.log");
if (!logFile.exists()) {
System.out.println("can not find log file,server not running.");
logger.print("can not find log file,server not running.");
} else {
new Thread() {
@Override
public void run() {
try (FileReader fr = new FileReader(logFile); BufferedReader br = new BufferedReader(fr)) {
System.out.println("console start, type close to exit console.");
br.skip(logFile.length());
String line = null;
while (tag) {
if ((line = br.readLine()) != null) {
System.out.println(line);
continue;
}
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
new Thread(() -> {
try (FileReader fr = new FileReader(logFile); BufferedReader br = new BufferedReader(fr)) {
logger.print("console start, type close to exit console.");
br.skip(logFile.length());
String line = null;
while (true) {
if ((line = br.readLine()) != null) {
logger.debug("line:{}.", line);
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
sleep();
}
} catch (Exception e) {
e.printStackTrace();
}
}.start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
String cmd = "";
while (tag) {
cmd = reader.readLine();
if ((null == cmd) || (StringUtils.equalsIgnoreCase("close", StringUtils.trim(cmd)))) {
tag = false;
System.exit(0);
}
try (RandomAccessFile raf = new RandomAccessFile(getBasePath() + "/command.swap", "rw")) {
FileChannel fc = raf.getChannel();
MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, 256);
FileLock flock = null;
flock = fc.lock();
mbb.put(cmd.getBytes());
flock.release();
}
}, Shadow.class.getName()).start();
readCommand();
}
}
private static void readCommand() {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
String cmd = "";
while (true) {
cmd = reader.readLine();
if ((null == cmd) || (StringUtils.equalsIgnoreCase("close", StringUtils.trim(cmd)))) {
System.exit(0);
break;
}
try (RandomAccessFile raf = new RandomAccessFile(getBasePath() + "/command.swap", "rw")) {
FileChannel fc = raf.getChannel();
MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, 256);
FileLock flock = null;
flock = fc.lock();
mbb.put(cmd.getBytes());
flock.release();
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void sleep() {
try {
Thread.sleep(1500L);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private static String getBasePath() throws Exception {
private static String getBasePath() {
String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File file = new File(path);
if (!file.isDirectory()) {
......@@ -86,7 +89,7 @@ public class Shadow {
}
file = file.getParentFile();
}
throw new Exception("can not define o2server base directory.");
throw new IllegalStateException("can not define o2server base directory.");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册