Search in sources :

Example 1 with TCPObjectServer

use of org.nustaq.net.TCPObjectServer in project kontraktor by RuedigerMoeller.

the class AsyncServerSocketTest method serialSync.

@Test
public void serialSync() throws Exception {
    AtomicInteger COUNT = new AtomicInteger(0);
    TCPObjectServer tcpObjectServer = new TCPObjectServer(8082);
    RateMeasure measure = new RateMeasure("count");
    tcpObjectServer.start((client) -> {
        try {
            while (true) {
                Object request = client.readObject();
                if (request == null)
                    // connection closed
                    return;
                COUNT.incrementAndGet();
                measure.count();
            }
        } catch (EOFException eof) {
        // e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    Thread.sleep(1000);
    HashMap testMap = new HashMap();
    testMap.put("pok", 13);
    testMap.put("yes", "no");
    testMap.put("true", "maybe");
    ExecutorService executorService = Executors.newCachedThreadPool();
    int MSG_COUNT = 10_000_000;
    int NUM_CLIENTS = 100;
    for (int ii = 0; ii < NUM_CLIENTS; ii++) {
        final int finalIi = ii;
        executorService.execute(() -> {
            TCPObjectSocket sock = null;
            try {
                sock = new TCPObjectSocket("localhost", 8082);
                // System.out.println("start "+finalIi);
                for (int i = 0; i < MSG_COUNT / NUM_CLIENTS; i++) {
                    sock.writeObject(testMap);
                    sock.flush();
                }
                sock.close();
            // System.out.println("finished "+ finalIi);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    Thread.sleep(3000);
    executorService.shutdown();
    executorService.awaitTermination(10, TimeUnit.DAYS);
    int to = 0;
    while (to < 7 && COUNT.get() < MSG_COUNT) {
        Thread.sleep(1000);
        System.out.println("COUNT " + COUNT.get());
        to++;
    }
    Assert.assertTrue(COUNT.get() == MSG_COUNT);
}
Also used : TCPObjectServer(org.nustaq.net.TCPObjectServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) ExecutorService(java.util.concurrent.ExecutorService) RateMeasure(org.nustaq.kontraktor.util.RateMeasure) TCPObjectSocket(org.nustaq.net.TCPObjectSocket) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1 RateMeasure (org.nustaq.kontraktor.util.RateMeasure)1 TCPObjectServer (org.nustaq.net.TCPObjectServer)1 TCPObjectSocket (org.nustaq.net.TCPObjectSocket)1