Search in sources :

Example 1 with Destination

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

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

the class TestStatsCollectorCoda method testEnvPrefixFromSystemProperty.

@Test
public void testEnvPrefixFromSystemProperty() throws Throwable {
    tearDown();
    System.setProperty(StatsCollectorFactoryCoda.environmentPrefixSystemPropertyName, "Hello.");
    setUp();
    StatsCollectorFactoryCoda.MetricNamingStrategy strategy = statsCollectorFactory.getNamingStrategy();
    ClusterId clusterId = new ClusterId("app", "cluster");
    MetricName name = strategy.createName(clusterId, "metric");
    assertEquals("metric", name.getName());
    assertEquals("app-cluster", name.getGroup());
    assertTrue(strategy.buildPrefix(clusterId, new Destination() {

        @Override
        public String toString() {
            return "destination";
        }
    }).startsWith("Hello."));
    // make sure setting the environment prefix doesn't effect the -D option
    statsCollectorFactory.setEnvironmentPrefix("otherEnvPrefix");
    assertTrue(strategy.buildPrefix(clusterId, new Destination() {

        @Override
        public String toString() {
            return "destination";
        }
    }).startsWith("Hello."));
    // make sure that without the system property the setEnvironmentPrefix value works
    System.getProperties().remove(StatsCollectorFactoryCoda.environmentPrefixSystemPropertyName);
    assertTrue(strategy.buildPrefix(clusterId, new Destination() {

        @Override
        public String toString() {
            return "destination";
        }
    }).startsWith("otherEnvPrefix"));
    // make sure that delting the environmentPrefix doesn't create an issue.
    statsCollectorFactory.setEnvironmentPrefix(null);
    strategy.buildPrefix(clusterId, new Destination() {

        @Override
        public String toString() {
            return "destination";
        }
    }).startsWith("otherEnvPrefix");
}
Also used : MetricName(com.yammer.metrics.core.MetricName) Destination(com.nokia.dempsy.messagetransport.Destination) ClusterId(com.nokia.dempsy.config.ClusterId) Test(org.junit.Test)

Example 3 with Destination

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

the class BlockingQueueTest method testBlockingQueueUsingDestination.

@Test
public void testBlockingQueueUsingDestination() throws Exception {
    try {
        setUp2("/blockingqueueTest2AppContext.xml");
        Destination destination = destinationFactory.getDestination();
        Sender lsender = senderFactory.getSender(destination);
        lsender.send("Hello".getBytes());
        String message = new String(pojo.getMessage());
        assertEquals("Hello", message);
        assertEquals(0, overflowHandler.overflowCalled);
    } finally {
        if (ctx != null)
            tearDown();
    }
}
Also used : Sender(com.nokia.dempsy.messagetransport.Sender) Destination(com.nokia.dempsy.messagetransport.Destination) Test(org.junit.Test)

Example 4 with Destination

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

Example 5 with Destination

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

the class TcpTransportTest method testTransportInstantiation.

/**
    * Just send a simple message and make sure it gets through.
    */
@Test
public void testTransportInstantiation() throws Throwable {
    final AtomicBoolean batchedAtLeastOnce = new AtomicBoolean(false);
    runAllCombinations(new Checker() {

        @Override
        public void check(int port, boolean localhost, long batchOutgoingMessagesDelayMillis) throws Throwable {
            final StatsCollector statsCollector = new StatsCollectorFactoryCoda().createStatsCollector(new ClusterId("test", "test-cluster"), new Destination() {
            });
            SenderFactory factory = null;
            TcpReceiver adaptor = null;
            try {
                boolean shouldBatch = batchOutgoingMessagesDelayMillis >= 0;
                if (shouldBatch)
                    batchedAtLeastOnce.set(true);
                TcpTransport transport = new TcpTransport();
                transport.setFailFast(getFailFast());
                // by default batching isn't disabled.
                assertFalse(transport.isBatchingDisabled());
                if (!shouldBatch)
                    transport.setDisableBatching(true);
                if (!shouldBatch)
                    assertTrue(transport.isBatchingDisabled());
                assertEquals(!shouldBatch, transport.isBatchingDisabled());
                //===========================================
                // setup the sender and receiver
                adaptor = (TcpReceiver) transport.createInbound(null);
                adaptor.setStatsCollector(statsCollector);
                StringListener receiver = new StringListener();
                adaptor.setListener(receiver);
                factory = transport.createOutbound(null, statsCollector);
                if (port > 0)
                    adaptor.setPort(port);
                if (localhost)
                    adaptor.setUseLocalhost(localhost);
                //===========================================
                // start the adaptor
                adaptor.start();
                // get the destination
                Destination destination = adaptor.getDestination();
                // send a message
                byte[] messageBytes = "Hello".getBytes();
                Sender sender = factory.getSender(destination);
                assertEquals((shouldBatch ? TcpTransport.defaultBatchingDelayMillis : -1), ((TcpSender) sender).getBatchOutgoingMessagesDelayMillis());
                sender.send(messageBytes);
                sender.send(messageBytes);
                // wait for it to be received.
                for (long endTime = System.currentTimeMillis() + baseTimeoutMillis; endTime > System.currentTimeMillis() && receiver.numMessages.get() < 2; ) Thread.sleep(1);
                assertEquals(2, receiver.numMessages.get());
                // verify everything came over ok.
                assertEquals(1, receiver.receivedStringMessages.size());
                assertEquals("Hello", receiver.receivedStringMessages.iterator().next());
                if (shouldBatch) {
                    // verify the histogram
                    Histogram histogram = ((TcpSender) sender).getBatchingHistogram();
                    assertEquals(calcMean(2), histogram.mean(), 0.0000001);
                    assertEquals(1, histogram.count());
                }
            } finally {
                if (factory != null)
                    factory.stop();
                if (adaptor != null)
                    adaptor.stop();
            }
        }

        @Override
        public String toString() {
            return "testTransportInstantiation";
        }
    });
    assertTrue(batchedAtLeastOnce.get());
}
Also used : Destination(com.nokia.dempsy.messagetransport.Destination) Histogram(com.yammer.metrics.core.Histogram) ClusterId(com.nokia.dempsy.config.ClusterId) StatsCollector(com.nokia.dempsy.monitoring.StatsCollector) BasicStatsCollector(com.nokia.dempsy.monitoring.basic.BasicStatsCollector) Sender(com.nokia.dempsy.messagetransport.Sender) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SenderFactory(com.nokia.dempsy.messagetransport.SenderFactory) StatsCollectorFactoryCoda(com.nokia.dempsy.monitoring.coda.StatsCollectorFactoryCoda) Test(org.junit.Test)

Aggregations

Destination (com.nokia.dempsy.messagetransport.Destination)7 Test (org.junit.Test)5 SenderFactory (com.nokia.dempsy.messagetransport.SenderFactory)4 ClusterId (com.nokia.dempsy.config.ClusterId)3 MessageTransportException (com.nokia.dempsy.messagetransport.MessageTransportException)2 Sender (com.nokia.dempsy.messagetransport.Sender)2 StatsCollector (com.nokia.dempsy.monitoring.StatsCollector)2 BasicStatsCollector (com.nokia.dempsy.monitoring.basic.BasicStatsCollector)2 Dempsy (com.nokia.dempsy.Dempsy)1 TestUtils (com.nokia.dempsy.TestUtils)1 ClusterInfoSession (com.nokia.dempsy.cluster.ClusterInfoSession)1 LocalClusterSessionFactory (com.nokia.dempsy.cluster.invm.LocalClusterSessionFactory)1 ApplicationDefinition (com.nokia.dempsy.config.ApplicationDefinition)1 ClusterDefinition (com.nokia.dempsy.config.ClusterDefinition)1 Listener (com.nokia.dempsy.messagetransport.Listener)1 StatsCollectorFactoryCoda (com.nokia.dempsy.monitoring.coda.StatsCollectorFactoryCoda)1 ClusterRouter (com.nokia.dempsy.router.Router.ClusterRouter)1 Inbound (com.nokia.dempsy.router.RoutingStrategy.Inbound)1 Histogram (com.yammer.metrics.core.Histogram)1 MetricName (com.yammer.metrics.core.MetricName)1