Search in sources :

Example 1 with Listener

use of com.nokia.dempsy.messagetransport.Listener in project Dempsy by Dempsy.

the class BlockingQueueAdaptor method run.

@Override
public void run() {
    synchronized (this) {
        if (running == null)
            // this is done in case start() wasn't used to start this thread.
            running = Thread.currentThread();
        else if (running != Thread.currentThread()) {
            logger.error(BlockingQueueAdaptor.class.getSimpleName() + " was started in a Runnable more than once. Exiting the additional thread.");
            return;
        }
    }
    while (!shutdown.get()) {
        try {
            byte[] val = destination.queue.take();
            Listener curListener = listener.get();
            boolean messageSuccess = curListener == null ? false : curListener.onMessage(val, failFast);
            if (overflowHandler != null && !messageSuccess)
                overflowHandler.overflow(val);
        } catch (InterruptedException ie) {
            // if we were interrupted we're probably stopping.
            if (!shutdown.get())
                logger.warn("Superfluous interrupt.", ie);
        } catch (Throwable err) {
            logger.error("Exception while handling message.", err);
        }
    }
}
Also used : Listener(com.nokia.dempsy.messagetransport.Listener)

Example 2 with Listener

use of com.nokia.dempsy.messagetransport.Listener in project Dempsy by Dempsy.

the class TcpTransportTest method transportLargeMessage.

/**
    * Test the sending and recieving of a large message (10 meg).
    * @throws Throwable
    */
@Test
public void transportLargeMessage() throws Throwable {
    int port = -1;
    boolean localhost = false;
    SenderFactory factory = null;
    TcpReceiver adaptor = null;
    TcpSenderFactory senderFactory = null;
    try {
        //===========================================
        // setup the sender and receiver
        adaptor = new TcpReceiver(null, getFailFast());
        // distruptible sender factory
        factory = makeSenderFactory(false, null, 500);
        receiveLargeMessageLatch = new CountDownLatch(1);
        adaptor.setListener(new Listener() {

            @Override
            public boolean onMessage(byte[] messageBytes, boolean failfast) throws MessageTransportException {
                receivedByteArrayMessage = messageBytes;
                receiveLargeMessageLatch.countDown();
                return true;
            }

            @Override
            public void shuttingDown() {
            }
        });
        if (port <= 0)
            adaptor.setUseEphemeralPort(true);
        if (port > 0)
            adaptor.setPort(port);
        if (localhost)
            adaptor.setUseLocalhost(localhost);
        //===========================================
        // start the adaptor
        adaptor.start();
        // double start ... just want more coverage.
        adaptor.start();
        // get the destination
        Destination destination = adaptor.getDestination();
        if (port > 0)
            adaptor.setPort(port);
        if (localhost)
            adaptor.setUseLocalhost(localhost);
        senderFactory = makeSenderFactory(false, null, 500);
        int size = 1024 * 1024 * 10;
        byte[] tosend = new byte[size];
        for (int i = 0; i < size; i++) tosend[i] = (byte) i;
        TcpSender sender = (TcpSender) senderFactory.getSender(destination);
        // extend the timeout because of the larger messages
        sender.setTimeoutMillis(100000);
        sender.send(tosend);
        assertTrue(receiveLargeMessageLatch.await(1, TimeUnit.MINUTES));
        assertArrayEquals(tosend, receivedByteArrayMessage);
    } finally {
        if (factory != null)
            factory.stop();
        if (senderFactory != null)
            senderFactory.stop();
        if (adaptor != null)
            adaptor.stop();
        receivedByteArrayMessage = null;
    }
}
Also used : Destination(com.nokia.dempsy.messagetransport.Destination) Listener(com.nokia.dempsy.messagetransport.Listener) MessageTransportException(com.nokia.dempsy.messagetransport.MessageTransportException) SenderFactory(com.nokia.dempsy.messagetransport.SenderFactory) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Listener (com.nokia.dempsy.messagetransport.Listener)2 Destination (com.nokia.dempsy.messagetransport.Destination)1 MessageTransportException (com.nokia.dempsy.messagetransport.MessageTransportException)1 SenderFactory (com.nokia.dempsy.messagetransport.SenderFactory)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1