Search in sources :

Example 16 with AbstractRunnable

use of org.spf4j.base.AbstractRunnable 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, Charsets.UTF_8);
                String[] lines = receivedString.split("\n");
                LOG.debug("Received = {}", lines);
                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

AbstractRunnable (org.spf4j.base.AbstractRunnable)16 IOException (java.io.IOException)6 JmxExport (org.spf4j.jmx.JmxExport)4 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 MeasurementRecorder (org.spf4j.perf.MeasurementRecorder)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 Test (org.junit.Test)2 TObjectLongMap (gnu.trove.map.TObjectLongMap)1 TObjectLongHashMap (gnu.trove.map.hash.TObjectLongHashMap)1 File (java.io.File)1 UncheckedIOException (java.io.UncheckedIOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 DatagramChannel (java.nio.channels.DatagramChannel)1 ArrayList (java.util.ArrayList)1 ForkJoinPool (java.util.concurrent.ForkJoinPool)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 BeforeClass (org.junit.BeforeClass)1