Search in sources :

Example 1 with MessageProcessor

use of net.dempsy.lifecycle.simple.MessageProcessor in project Dempsy by Dempsy.

the class TestSimple method testSimple.

@Test
public void testSimple() throws Exception {
    final AtomicLong count = new AtomicLong(0L);
    try (final NodeManager nm = new NodeManager();
        final DefaultThreadingModel tm = new DefaultThreadingModel("TB", -1, 1)) {
        final Node n = new Node.Builder("test-app").defaultRoutingStrategyId("net.dempsy.router.simple").receiver(new Dummy()).cluster("start").adaptor(new Adaptor() {

            private Dispatcher disp;

            boolean done = false;

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

            @Override
            public void start() {
                try {
                    while (!done) {
                        disp.dispatch(new KeyedMessageWithType(Integer.valueOf(1), "Hello", "string"));
                        // This is here for when the Container has a max pending and it gets starved for CPU cycles
                        // in this particular test.
                        Thread.yield();
                    }
                } catch (final InterruptedException ie) {
                    if (!done)
                        LOGGER.error("Interrupted but not stopping.");
                }
            }

            @Override
            public void setDispatcher(final Dispatcher dispatcher) {
                this.disp = dispatcher;
            }
        }).cluster("mp").mp(new MessageProcessor(MpFactory.make(() -> new Mp() {

            @Override
            public KeyedMessageWithType[] handle(final KeyedMessage message) {
                count.incrementAndGet();
                return null;
            }
        }, "string"))).build();
        nm.node(n).collaborator(new LocalClusterSessionFactory().createSession()).threadingModel(tm.start("nodeid"));
        nm.start();
        assertTrue(ConditionPoll.poll(o -> count.get() > 100000));
    }
}
Also used : Adaptor(net.dempsy.messages.Adaptor) Logger(org.slf4j.Logger) Node(net.dempsy.config.Node) MessageProcessor(net.dempsy.lifecycle.simple.MessageProcessor) Mp(net.dempsy.lifecycle.simple.Mp) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) Dispatcher(net.dempsy.messages.Dispatcher) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) NodeAddress(net.dempsy.transport.NodeAddress) Listener(net.dempsy.transport.Listener) LoggerFactory(org.slf4j.LoggerFactory) KeyedMessage(net.dempsy.messages.KeyedMessage) Assert.assertTrue(org.junit.Assert.assertTrue) ConditionPoll(net.dempsy.utils.test.ConditionPoll) Test(org.junit.Test) MpFactory(net.dempsy.lifecycle.simple.MpFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) Adaptor(net.dempsy.messages.Adaptor) MessageTransportException(net.dempsy.transport.MessageTransportException) Receiver(net.dempsy.transport.Receiver) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) Mp(net.dempsy.lifecycle.simple.Mp) Node(net.dempsy.config.Node) MessageProcessor(net.dempsy.lifecycle.simple.MessageProcessor) LocalClusterSessionFactory(net.dempsy.cluster.local.LocalClusterSessionFactory) Dispatcher(net.dempsy.messages.Dispatcher) DefaultThreadingModel(net.dempsy.threading.DefaultThreadingModel) AtomicLong(java.util.concurrent.atomic.AtomicLong) KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) KeyedMessage(net.dempsy.messages.KeyedMessage) Test(org.junit.Test)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)1 LocalClusterSessionFactory (net.dempsy.cluster.local.LocalClusterSessionFactory)1 Node (net.dempsy.config.Node)1 MessageProcessor (net.dempsy.lifecycle.simple.MessageProcessor)1 Mp (net.dempsy.lifecycle.simple.Mp)1 MpFactory (net.dempsy.lifecycle.simple.MpFactory)1 Adaptor (net.dempsy.messages.Adaptor)1 Dispatcher (net.dempsy.messages.Dispatcher)1 KeyedMessage (net.dempsy.messages.KeyedMessage)1 KeyedMessageWithType (net.dempsy.messages.KeyedMessageWithType)1 DefaultThreadingModel (net.dempsy.threading.DefaultThreadingModel)1 Listener (net.dempsy.transport.Listener)1 MessageTransportException (net.dempsy.transport.MessageTransportException)1 NodeAddress (net.dempsy.transport.NodeAddress)1 Receiver (net.dempsy.transport.Receiver)1 ConditionPoll (net.dempsy.utils.test.ConditionPoll)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Test (org.junit.Test)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1