Search in sources :

Example 11 with KeyedMessageWithType

use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.

the class KeyExtractor method extract.

public List<KeyedMessageWithType> extract(final Object toSend) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    final List<Object> messages = new ArrayList<Object>();
    unwindMessages(toSend, messages);
    final ArrayList<KeyedMessageWithType> ret = new ArrayList<>(messages.size());
    for (final Object msg : messages) {
        final Class<?> messageClass = msg.getClass();
        final MessageTypeDetails[] extractors = cache.computeIfAbsent(messageClass, mc -> MessageUtils.getAllMessageTypeDetailsUsingAnnotationValues(mc, true).toArray(MessageTypeDetails[]::new));
        final int numExtractors = extractors.length;
        for (int i = 0; i < numExtractors; i++) {
            final MessageTypeDetails mtd = extractors[i];
            final Function<Object, Object> extractor = mtd.keyExtractorForThisType;
            if (extractor == null)
                throw new IllegalArgumentException("The message object " + msg.getClass().getSimpleName() + " doesn't seem to have a method annotated with " + MessageKey.class.getSimpleName() + " so there's no way to route this message");
            final Object msgKeyValue = extractor.apply(msg);
            ret.add(new KeyedMessageWithType(msgKeyValue, msg, mtd.messageType));
        }
    }
    return ret;
}
Also used : KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) ArrayList(java.util.ArrayList) MessageTypeDetails(net.dempsy.lifecycle.annotation.internal.MessageUtils.MessageTypeDetails)

Example 12 with KeyedMessageWithType

use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.

the class TestContainer method testMpThrowsDempsyException.

@Test
public void testMpThrowsDempsyException() throws Exception {
    assertEquals(0, ((ClusterMetricGetters) container.statCollector).getMessageFailedCount());
    assertEquals(0, ((ClusterMetricGetters) container.statCollector).getDispatchedMessageCount());
    justThrowMe = new DempsyException("JustThrowMe!");
    final KeyedMessageWithType kmwt = ke.extract(new MyMessage("YO")).get(0);
    container.dispatch(kmwt, Operation.handle, true);
    assertEquals(1, ((ClusterMetricGetters) container.statCollector).getMessageFailedCount());
    assertEquals(1, ((ClusterMetricGetters) container.statCollector).getDispatchedMessageCount());
}
Also used : KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) DempsyException(net.dempsy.DempsyException) Test(org.junit.Test)

Example 13 with KeyedMessageWithType

use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.

the class TestContainer method testWrongTypeMessage.

@Test
public void testWrongTypeMessage() throws Exception {
    assertEquals(0, ((ClusterMetricGetters) container.statCollector).getMessageFailedCount());
    final KeyedMessageWithType kmwt = ke.extract(new MyMessage("YO")).get(0);
    container.dispatch(new KeyedMessage(kmwt.key, new Object()), Operation.handle, true);
    assertEquals(1, ((ClusterMetricGetters) container.statCollector).getMessageFailedCount());
}
Also used : KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) KeyedMessage(net.dempsy.messages.KeyedMessage) Test(org.junit.Test)

Example 14 with KeyedMessageWithType

use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.

the class TestContainer method testMpThrowsException.

@Test
public void testMpThrowsException() throws Exception {
    assertEquals(0, ((ClusterMetricGetters) container.statCollector).getMessageFailedCount());
    assertEquals(0, ((ClusterMetricGetters) container.statCollector).getDispatchedMessageCount());
    justThrowMe = new RuntimeException("JustThrowMe!");
    final KeyedMessageWithType kmwt = ke.extract(new MyMessage("YO")).get(0);
    container.dispatch(kmwt, Operation.handle, true);
    assertEquals(1, ((ClusterMetricGetters) container.statCollector).getMessageFailedCount());
    assertEquals(1, ((ClusterMetricGetters) container.statCollector).getDispatchedMessageCount());
}
Also used : KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) Test(org.junit.Test)

Example 15 with KeyedMessageWithType

use of net.dempsy.messages.KeyedMessageWithType in project Dempsy by Dempsy.

the class TestContainer method testMpActivationFails.

@Test
public void testMpActivationFails() throws Exception {
    assertEquals(0, ((ClusterMetricGetters) container.statCollector).getMessageFailedCount());
    assertEquals(0, ((ClusterMetricGetters) container.statCollector).getDispatchedMessageCount());
    throwMeInActivation = new RuntimeException("JustThrowMeDAMMIT!");
    final KeyedMessageWithType kmwt = ke.extract(new MyMessage("YO")).get(0);
    container.dispatch(kmwt, Operation.handle, true);
    assertEquals(1, ((ClusterMetricGetters) container.statCollector).getMessageFailedCount());
    assertEquals(0, ((ClusterMetricGetters) container.statCollector).getDispatchedMessageCount());
}
Also used : KeyedMessageWithType(net.dempsy.messages.KeyedMessageWithType) Test(org.junit.Test)

Aggregations

KeyedMessageWithType (net.dempsy.messages.KeyedMessageWithType)26 Test (org.junit.Test)21 InstanceWrapper (net.dempsy.container.locking.LockingContainer.InstanceWrapper)7 NodeAddress (net.dempsy.transport.NodeAddress)5 DempsyException (net.dempsy.DempsyException)4 Container (net.dempsy.container.Container)4 RoutingStrategy (net.dempsy.router.RoutingStrategy)4 ContainerAddress (net.dempsy.router.RoutingStrategy.ContainerAddress)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 Infrastructure (net.dempsy.Infrastructure)3 Manager (net.dempsy.Manager)3 ClusterInfoException (net.dempsy.cluster.ClusterInfoException)3 ClusterInfoSession (net.dempsy.cluster.ClusterInfoSession)3 ClusterId (net.dempsy.config.ClusterId)3 ContainerException (net.dempsy.container.ContainerException)3 KeyedMessage (net.dempsy.messages.KeyedMessage)3 RoutingStrategyManager (net.dempsy.router.RoutingStrategyManager)3