Search in sources :

Example 1 with MessageTransportException

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

the class PassthroughTransport method createOutbound.

@Override
public SenderFactory createOutbound(DempsyExecutor executor, final StatsCollector statsCollector) {
    return new SenderFactory() {

        private volatile boolean isStopped = false;

        private StatsCollector sc = statsCollector;

        @Override
        public Sender getSender(Destination destination) throws MessageTransportException {
            if (isStopped == true)
                throw new MessageTransportException("getSender called for the destination " + SafeString.valueOf(destination) + " on a stopped " + SafeString.valueOfClass(this));
            PassthroughSender ret = (PassthroughSender) (((PassthroughDestination) destination).sender);
            ret.statsCollector = sc;
            return ret;
        }

        @Override
        public void stop() {
            isStopped = true;
        }

        // There is no 'stop' on a PassthroughSender so there's nothing to do to reclaim it.
        @Override
        public void reclaim(Destination destination) {
        }
    };
}
Also used : Destination(com.nokia.dempsy.messagetransport.Destination) StatsCollector(com.nokia.dempsy.monitoring.StatsCollector) MessageTransportException(com.nokia.dempsy.messagetransport.MessageTransportException) SenderFactory(com.nokia.dempsy.messagetransport.SenderFactory)

Example 2 with MessageTransportException

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

the class TcpReceiver method bind.

protected void bind() throws MessageTransportException {
    if (serverSocket == null || !serverSocket.isBound()) {
        try {
            InetSocketAddress inetSocketAddress = new InetSocketAddress(destination.inetAddress, destination.port < 0 ? 0 : destination.port);
            serverSocket = new ServerSocket();
            // this allows the server port to be bound to even if it's in TIME_WAIT
            serverSocket.setReuseAddress(true);
            serverSocket.bind(inetSocketAddress);
            destination.port = serverSocket.getLocalPort();
        } catch (IOException ioe) {
            throw new MessageTransportException("Cannot bind to port " + (destination.isEphemeral() ? "(ephemeral port)" : destination.port), ioe);
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) MessageTransportException(com.nokia.dempsy.messagetransport.MessageTransportException) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException)

Example 3 with MessageTransportException

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

the class TcpSenderFactory method getSender.

@Override
public Sender getSender(Destination destination) throws MessageTransportException {
    if (isStopped == true)
        throw new MessageTransportException("getSender called for the destination " + SafeString.valueOf(destination) + " on a stopped " + SafeString.valueOfClass(this));
    TcpSender sender;
    synchronized (senders) {
        sender = senders.get(destination);
        if (sender == null) {
            sender = makeTcpSender((TcpDestination) destination);
            senders.put(destination, sender);
        }
    }
    return sender;
}
Also used : MessageTransportException(com.nokia.dempsy.messagetransport.MessageTransportException)

Example 4 with MessageTransportException

use of com.nokia.dempsy.messagetransport.MessageTransportException 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

MessageTransportException (com.nokia.dempsy.messagetransport.MessageTransportException)4 Destination (com.nokia.dempsy.messagetransport.Destination)2 SenderFactory (com.nokia.dempsy.messagetransport.SenderFactory)2 Listener (com.nokia.dempsy.messagetransport.Listener)1 StatsCollector (com.nokia.dempsy.monitoring.StatsCollector)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1