Search in sources :

Example 1 with MessageTransportException

use of net.dempsy.transport.MessageTransportException in project Dempsy by Dempsy.

the class BlockingQueueTest method testBlockingQueueOverflow.

/**
 * Test overflow for a blocking Transport around a queue with depth one. While the transport will not call the, and does not even have a , overflow handler,
 * every message will call the overflow handler on
 * the receiver since the queue is always full.
 *
 * @throws Throwable
 */
@Test
public void testBlockingQueueOverflow() throws Throwable {
    final AtomicReference<String> message = new AtomicReference<String>(null);
    final ArrayBlockingQueue<Object> input = new ArrayBlockingQueue<>(1);
    try (@SuppressWarnings("resource") SystemPropertyManager // test only works when the blocking queue blocks
    props = new SystemPropertyManager().set(BlockingQueueSenderFactory.class.getPackageName() + "." + BlockingQueueSenderFactory.BLOCKING_KEY, "true");
        final TestInfrastructure infra = new TestInfrastructure(new DefaultThreadingModel("BQTest-testBlockingQueueOverflow-"));
        final Receiver r = new BlockingQueueReceiver(input);
        final TransportManager tranMan = chain(new TransportManager(), c -> c.start(infra));
        final SenderFactory sf = tranMan.getAssociatedInstance(transportTypeId)) {
        final Sender sender = sf.getSender(r.getAddress(infra));
        final AtomicBoolean finallySent = new AtomicBoolean(false);
        final AtomicLong receiveCount = new AtomicLong();
        // fill up queue
        sender.send("Hello");
        final Thread t = new Thread(() -> {
            try {
                sender.send("Hello again");
            } catch (final MessageTransportException | InterruptedException e) {
                throw new RuntimeException(e);
            }
            finallySent.set(true);
        });
        t.start();
        Thread.sleep(100);
        // the thread should be hung blocked on the send
        assertFalse(finallySent.get());
        // Start the receiver to read
        r.start((final String msg) -> {
            message.set(new String(msg));
            receiveCount.incrementAndGet();
            return true;
        }, infra);
        // 2 messages should have been read and the 2nd should be "Hello again"
        assertTrue(poll(o -> "Hello again".equals(message.get())));
        // The thread should shut down eventually
        assertTrue(poll(o -> !t.isAlive()));
    }
}
Also used : Sender(net.dempsy.transport.Sender) TransportManager(net.dempsy.transport.TransportManager) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) SenderFactory(net.dempsy.transport.SenderFactory) SystemPropertyManager(net.dempsy.util.SystemPropertyManager) Assert.assertTrue(org.junit.Assert.assertTrue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConditionPoll.poll(net.dempsy.utils.test.ConditionPoll.poll) Assert.assertFalse(org.junit.Assert.assertFalse) Functional.chain(net.dempsy.util.Functional.chain) TestInfrastructure(net.dempsy.util.TestInfrastructure) MessageTransportException(net.dempsy.transport.MessageTransportException) Receiver(net.dempsy.transport.Receiver) TestInfrastructure(net.dempsy.util.TestInfrastructure) Receiver(net.dempsy.transport.Receiver) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) Sender(net.dempsy.transport.Sender) SystemPropertyManager(net.dempsy.util.SystemPropertyManager) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) MessageTransportException(net.dempsy.transport.MessageTransportException) SenderFactory(net.dempsy.transport.SenderFactory) TransportManager(net.dempsy.transport.TransportManager) Test(org.junit.Test)

Example 2 with MessageTransportException

use of net.dempsy.transport.MessageTransportException in project Dempsy by Dempsy.

the class PassthroughSenderFactory method getSender.

@Override
public synchronized PassthroughSender getSender(final NodeAddress destination) throws MessageTransportException {
    PassthroughSender ret = senders.get(destination);
    if (ret == null) {
        final PassthroughReceiver r = PassthroughReceiver.receivers.get(Long.valueOf(((PassthroughAddress) destination).destinationId));
        if (r == null)
            throw new MessageTransportException("Recveiver for " + destination + " is shut down");
        ret = new PassthroughSender(r, statsCollector, this);
        senders.put(destination, ret);
    }
    return ret;
}
Also used : MessageTransportException(net.dempsy.transport.MessageTransportException)

Example 3 with MessageTransportException

use of net.dempsy.transport.MessageTransportException in project Dempsy by Dempsy.

the class NioReceiver method start.

@SuppressWarnings("unchecked")
@Override
public void start(final Listener<?> listener, final Infrastructure infra) throws MessageTransportException {
    if (!isRunning.get())
        throw new IllegalStateException("Cannot restart an " + NioReceiver.class.getSimpleName());
    thePlug = infra.getThePlug();
    if (binding == null)
        // sets binding via side affect.
        getAddress(infra);
    // before starting the acceptor, make sure we have Readers created.
    try {
        for (int i = 0; i < readers.length; i++) readers[i] = new Reader<T>(isRunning, address, (Listener<T>) listener, serializer, maxMessageSize, thePlug);
    } catch (final IOException ioe) {
        LOGGER.error(address.toString() + " failed to start up readers", ioe);
        throw new MessageTransportException(address.toString() + " failed to start up readers", ioe);
    }
    final ThreadingModel threadingModel = infra.getThreadingModel();
    // now start the readers.
    for (int i = 0; i < readers.length; i++) threadingModel.runDaemon(readers[i], "nio-reader-" + i + "-" + address);
    // start the acceptor
    threadingModel.runDaemon(acceptor = new Acceptor(binding, isRunning, readers, address, thePlug), "nio-acceptor-" + address);
}
Also used : ThreadingModel(net.dempsy.threading.ThreadingModel) MessageTransportException(net.dempsy.transport.MessageTransportException) IOException(java.io.IOException)

Example 4 with MessageTransportException

use of net.dempsy.transport.MessageTransportException in project Dempsy by Dempsy.

the class NioSenderFactory method getSender.

@Override
public NioSender getSender(final NodeAddress destination) throws MessageTransportException {
    final TcpAddress tcpaddr = (TcpAddress) destination;
    final NioSender ret;
    if (isRunning.get()) {
        ret = senders.computeIfAbsent(tcpaddr, a -> new NioSender(a, this));
    } else
        throw new MessageTransportException(nodeId + " sender had getSender called while stopped.");
    try {
        ret.connect(false);
    } catch (final IOException e) {
        throw new MessageTransportException(nodeId + " sender failed to connect to " + destination, e);
    }
    return ret;
}
Also used : TcpAddress(net.dempsy.transport.tcp.TcpAddress) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) NioUtils.dontInterrupt(net.dempsy.transport.tcp.nio.internal.NioUtils.dontInterrupt) SelectionKey(java.nio.channels.SelectionKey) NodeAddress(net.dempsy.transport.NodeAddress) Selector(java.nio.channels.Selector) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerFactory(org.slf4j.LoggerFactory) SenderFactory(net.dempsy.transport.SenderFactory) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ArrayList(java.util.ArrayList) Serializer(net.dempsy.serialization.Serializer) NodeStatsCollector(net.dempsy.monitoring.NodeStatsCollector) List(java.util.List) Infrastructure(net.dempsy.Infrastructure) Functional.chain(net.dempsy.util.Functional.chain) Map(java.util.Map) Manager(net.dempsy.Manager) NioUtils(net.dempsy.transport.tcp.nio.internal.NioUtils) MessageTransportException(net.dempsy.transport.MessageTransportException) TcpAddress(net.dempsy.transport.tcp.TcpAddress) MessageTransportException(net.dempsy.transport.MessageTransportException) IOException(java.io.IOException)

Aggregations

MessageTransportException (net.dempsy.transport.MessageTransportException)4 IOException (java.io.IOException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 SenderFactory (net.dempsy.transport.SenderFactory)2 Functional.chain (net.dempsy.util.Functional.chain)2 SelectionKey (java.nio.channels.SelectionKey)1 Selector (java.nio.channels.Selector)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Infrastructure (net.dempsy.Infrastructure)1 Manager (net.dempsy.Manager)1 NodeStatsCollector (net.dempsy.monitoring.NodeStatsCollector)1