0

i am using following code for read log file and matched pattern store in database.

public class MIScript {

//DB
public static void db(String email, String ip, String pdate, String hostname, String im) {


   // DATABASE INSERT


}

public void pop(File f, String IM) throws FileNotFoundException, IOException, InterruptedException {
    int pos = 0;
    RandomAccessFile file = new RandomAccessFile(f, "r");
    pos = (int) file.length() - (int) Math.min(file.length() - 1, file.length());

    file.seek(pos);
    for (; true; Thread.currentThread().sleep(1000)) {
        int l = (int) (file.length() - pos);
        if (l <= 0) {
            continue;
        }
        byte[] buf = new byte[l];
        int read = file.read(buf, 0, l);
        String out = new String(buf, 0, l);
        //   System.out.println(out);
        InputStream is = new ByteArrayInputStream(out.getBytes());
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        String line = null;
        while (((line = in.readLine()) != null)) {
            if (line.contains("LOG")) {

               // SOME CODE 

                //INSERT INTO DATABASE
                MIScript.db(// parameters //);

            }
        }
    }
}

public static void main(String[] args) {

    try {
        File pop = new File("d://ABC.log");
        MIScript tail1 = new MIScript();
        tail1.pop(pop, "TEST");


    } catch (ArrayIndexOutOfBoundsException ar) {
        System.out.println("Errrrr------" + ar);
        System.exit(1);
    } catch (Exception io) {
        io.printStackTrace();
        System.out.println("Errrrr2------" + io);
        System.exit(1);
    }
}

}

it works great on single file but i need to 4 file to read synchronously please give me the way to do this . i tried to do this with 2 files but that's not working

  • You could improve the way you're polling the file: suggestions here http://stackoverflow.com/questions/3893236/java-detecting-modification-to-a-file-file-polling or http://stackoverflow.com/questions/3387634/java-detect-changes-in-filesystem and from Java 7 you can get the filesystem to call you back when a file changes http://openjdk.java.net/projects/nio/javadoc/java/nio/file/WatchService.html – Andrew Spencer Jun 14 '13 at 08:49

1 Answers1

0

You need to read each file in a separate thread, and ensure that the code to write to database is thread safe.

Edit: I put this in a comment, but actually it's part of the answer: from Java 7 you can get the filesystem to call you back when a file changes http://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html That way you don't need to poll the file size like you're doing... but you do still need 1 thread per file.

Tutorial for WatchService is here: http://docs.oracle.com/javase/tutorial/essential/io/notification.html

Andrew Spencer
  • 15,164
  • 4
  • 29
  • 48
  • Java 1.5 is end-of-life and has been since late 2009. Even java 1.6 is end-of-life now! Although of course corporates can still get security patches. Anyway, it means you need to stick to polling - though you could poll on last-modified time instead of keeping the file handle open - it'd scale better if you had to read many many log files. – Andrew Spencer Jun 14 '13 at 09:26