尝试同时读取时 Java 滚动文件创建失败
我正在使用 java.util 日志记录类来创建滚动文件附加程序。我想创建一个日志读取器,当数据写入这些日志时,它会从这些日志中读取数据。
滚动日志附加器代码本身工作得很好。但是一旦我启动阅读器线程,就不会创建新文件,即如果滚动日志附加程序设置为使用 5 个文件,它将创建 1og.0、log.1、log.2 等,但如果阅读器线程启动,那么它只会create log.0 没有创建其他文件。我在 java 日志记录和 log4j 中都注意到了这一点。
我正在使用 nio 来读取日志阅读器。所以我的疑问是,在同一个文件上创建另一个 FileChannel 是否会产生一些问题?或者我错过了一些导致问题的基本 nio 东西。
这是代码..
public class Test {
private static final long initialTime = System.currentTimeMillis();
private static Logger logger = Logger.getLogger(Test.class.getName());
public static void main(String[] args) {
logger.setLevel(Level.INFO);
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(
new File(System.getProperty("user.dir")+File.separator+"input.dat"))));
FileHandler handler = new FileHandler("log/test.log", 1024, 5, true);
handler.setFormatter(new SimpleFormatter());
logger.addHandler(handler);
logger.setUseParentHandlers(false);
//If I comment the next two lines the rolling appender works as expected both in java logging
//and when using log4j. however once I start the log reader only one file is created.
Thread logReader = new Thread(new LogReader("log/test.log.0"));
logReader.start();
while(reader.ready())
{
String strToLog = reader.readLine();
logger.info(strToLog);
//Only want to run this for 10 secs.
if(System.currentTimeMillis() - initialTime > 10000)
System.exit(0);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
}
}
}
class LogReader implements Runnable {
private final int BUFFER_SIZE = 1024;
private RandomAccessFile file;
private FileChannel chan;
private long offset;
private ByteBuffer buf;
private Charset charset = Charset.forName("UTF-8");
private String filename = "output.log";
public LogReader(String logfile) throws IOException {
System.out.println("Starting log reader from " + logfile);
file = new RandomAccessFile(logfile, "r");
offset = file.length();
chan = file.getChannel();
chan.position(offset);
buf = ByteBuffer.allocateDirect(BUFFER_SIZE);
System.out.println("Started log reader from " + logfile);
}
public void run() {
//Even if I have nothing here..the problem exists..
}
}
I am using java.util logging classes to create a rolling file appender. I want to create a log reader that reads from these logs as data is written to them.
The rolling log appender code works fine on its own. But once I start the reader thread new files are not created i.e. if rolling log appender is set to use 5 files it will create 1og.0, log.1, log.2 and so on but if the reader thread starts then it will only create log.0 no other files are created. I notice this in both java logging and log4j.
I am using nio to read in the log reader. So my doubt is, is creating another FileChannel on the same file creating some problem? or have I missed some basic nio stuff here that is causing problems.
here is the code..
public class Test {
private static final long initialTime = System.currentTimeMillis();
private static Logger logger = Logger.getLogger(Test.class.getName());
public static void main(String[] args) {
logger.setLevel(Level.INFO);
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(
new File(System.getProperty("user.dir")+File.separator+"input.dat"))));
FileHandler handler = new FileHandler("log/test.log", 1024, 5, true);
handler.setFormatter(new SimpleFormatter());
logger.addHandler(handler);
logger.setUseParentHandlers(false);
//If I comment the next two lines the rolling appender works as expected both in java logging
//and when using log4j. however once I start the log reader only one file is created.
Thread logReader = new Thread(new LogReader("log/test.log.0"));
logReader.start();
while(reader.ready())
{
String strToLog = reader.readLine();
logger.info(strToLog);
//Only want to run this for 10 secs.
if(System.currentTimeMillis() - initialTime > 10000)
System.exit(0);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
}
}
}
class LogReader implements Runnable {
private final int BUFFER_SIZE = 1024;
private RandomAccessFile file;
private FileChannel chan;
private long offset;
private ByteBuffer buf;
private Charset charset = Charset.forName("UTF-8");
private String filename = "output.log";
public LogReader(String logfile) throws IOException {
System.out.println("Starting log reader from " + logfile);
file = new RandomAccessFile(logfile, "r");
offset = file.length();
chan = file.getChannel();
chan.position(offset);
buf = ByteBuffer.allocateDirect(BUFFER_SIZE);
System.out.println("Started log reader from " + logfile);
}
public void run() {
//Even if I have nothing here..the problem exists..
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
RandomAccessFile
可能存在一些并发问题。我建议您使用不同的类来读取文件。例如java.io.FileReader
I think there might be some concurrency issues with
RandomAccessFile
.I suggest you use a different class for reading the file. E.g.
java.io.FileReader