Created
April 5, 2012 07:09
-
-
Save nschlimm/2308688 to your computer and use it in GitHub Desktop.
LoggingClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyLoggingClient { | |
private static AtomicInteger fileindex = new AtomicInteger(0); | |
private static final String FILE_URI = "file:/E:/temp/afile.out"; | |
public static void main(String[] args) throws IOException { | |
new Thread(new Runnable() { // arbitrary thread that writes stuff into an asynchronous I/O data sink | |
@Override | |
public void run() { | |
try { | |
for (;;) { | |
GracefulAsynchronousFileChannel.get(FILE_URI).write(ByteBuffer.wrap("Hello".getBytes()), | |
fileindex.getAndIncrement() * 5); | |
} | |
} catch (NonWritableChannelException e) { | |
System.out.println("Deal with the fact that the channel was closed asynchronously ... " | |
+ e.toString()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}).start(); | |
Timer timer = new Timer(); // asynchronous channel closer | |
timer.schedule(new TimerTask() { | |
public void run() { | |
try { | |
GracefulAsynchronousFileChannel.get(FILE_URI).close(); | |
long size = Files.size(Paths.get("E:/temp/afile.out")); | |
System.out.println("Expected file size (bytes): " + (fileindex.get() - 1) * 5); | |
System.out.println("Actual file size (bytes): " + size); | |
if (size == (fileindex.get() - 1) * 5) | |
System.out.println("No write operation was lost!"); | |
Files.delete(Paths.get("E:/temp/afile.out")); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, 1000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment