Search in sources :

Example 81 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project spf4j by zolyfarkas.

the class GraphiteUdpStoreTest method runUdpServer.

@BeforeClass
public static void runUdpServer() {
    server = org.spf4j.concurrent.DefaultExecutor.INSTANCE.submit(new AbstractRunnable(true) {

        @Override
        public void doRun() throws IOException, InterruptedException {
            DatagramChannel channel = DatagramChannel.open();
            final InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 1976);
            channel.socket().bind(inetSocketAddress);
            ByteBuffer bb = ByteBuffer.allocate(512);
            while (!terminated) {
                bb.rewind();
                try {
                    channel.receive(bb);
                } catch (ClosedByInterruptException ex) {
                    // this we receive when we interrupt this exec with server.cancel.
                    break;
                }
                byte[] rba = new byte[bb.position()];
                bb.rewind();
                bb.get(rba);
                String receivedString = new String(rba, StandardCharsets.UTF_8);
                String[] lines = receivedString.split("\n");
                for (int i = 0; i < lines.length; i++) {
                    LOG.debug("{}: {}", i, lines[i]);
                }
                for (String line : lines) {
                    QUEUE.put(line);
                }
            }
        }
    });
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer) BeforeClass(org.junit.BeforeClass)

Aggregations

ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)81 IOException (java.io.IOException)48 ByteBuffer (java.nio.ByteBuffer)15 ClosedChannelException (java.nio.channels.ClosedChannelException)11 SocketTimeoutException (java.net.SocketTimeoutException)9 InetSocketAddress (java.net.InetSocketAddress)7 MappedByteBuffer (java.nio.MappedByteBuffer)7 SocketChannel (java.nio.channels.SocketChannel)7 File (java.io.File)6 ServerSocketChannel (java.nio.channels.ServerSocketChannel)6 ServerSocket (java.net.ServerSocket)5 FileChannel (java.nio.channels.FileChannel)5 FileLockInterruptionException (java.nio.channels.FileLockInterruptionException)5 InterruptedIOException (java.io.InterruptedIOException)4 Path (java.nio.file.Path)4 Test (org.junit.Test)4 BuildId (com.facebook.buck.model.BuildId)3 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 SocketException (java.net.SocketException)3