Example #1
0
  public void testMessageResequencer() throws Exception {
    MuleSession session = getTestSession(getTestService(), muleContext);
    Service testService = getTestService("test", Apple.class);
    assertNotNull(testService);

    SimpleEventResequencer router = new SimpleEventResequencer(3);
    router.setMuleContext(muleContext);
    router.initialise();

    MuleMessage message1 = new DefaultMuleMessage("test event A");
    MuleMessage message2 = new DefaultMuleMessage("test event B");
    MuleMessage message3 = new DefaultMuleMessage("test event C");
    message1.setCorrelationId(message1.getUniqueId());
    message2.setCorrelationId(message1.getUniqueId());
    message3.setCorrelationId(message1.getUniqueId());

    ImmutableEndpoint endpoint = getTestOutboundEndpoint("Test1Provider");
    MuleEvent event1 = new DefaultMuleEvent(message1, endpoint, session, false);
    MuleEvent event2 = new DefaultMuleEvent(message2, endpoint, session, false);
    MuleEvent event3 = new DefaultMuleEvent(message3, endpoint, session, false);
    assertTrue(router.isMatch(event1));
    assertTrue(router.isMatch(event2));
    assertTrue(router.isMatch(event3));

    assertNull(router.process(event2));
    assertNull(router.process(event3));

    MuleEvent[] results = router.process(event1);
    assertNotNull(results);
    assertEquals(3, results.length);

    assertEquals("test event B", results[0].getMessageAsString());
    assertEquals("test event C", results[1].getMessageAsString());
    assertEquals("test event A", results[2].getMessageAsString());

    // set a resequencing comparator. We need to reset the router since it will not process the same
    // event group
    // twice
    router = new SimpleEventResequencer(3);
    router.setMuleContext(muleContext);
    router.setEventComparator(new EventPayloadComparator());
    router.initialise();

    assertNull(router.process(event2));
    assertNull(router.process(event3));

    results = router.process(event1);
    assertNotNull(results);
    assertEquals(3, results.length);

    assertEquals("test event A", results[0].getMessageAsString());
    assertEquals("test event B", results[1].getMessageAsString());
    assertEquals("test event C", results[2].getMessageAsString());
  }
Example #2
0
  public MuleMessage route(MuleMessage message, MuleSession session) throws RoutingException {
    if (messageSize == 0 && numberOfMessages < 2) {
      return super.route(message, session);
    } else if (messageSize > 0) {
      byte[] data = new byte[0];
      try {
        data = message.getPayloadAsBytes();
      } catch (Exception e) {
        throw new RoutingException(
            CoreMessages.failedToReadPayload(), message, getEndpoint(0, message), e);
      }

      int parts = data.length / messageSize;
      if ((parts * messageSize) < data.length) {
        parts++;
      }
      int len = messageSize;
      MuleMessage part = null;
      int count = 0;
      int pos = 0;
      byte[] buffer = null;
      try {
        for (; count < parts; count++) {
          if ((pos + len) > data.length) {
            len = data.length - pos;
          }
          buffer = new byte[len];
          System.arraycopy(data, pos, buffer, 0, buffer.length);
          pos += len;
          part = new DefaultMuleMessage(buffer, message);
          part.setCorrelationId(message.getUniqueId());
          part.setCorrelationGroupSize(parts);
          part.setCorrelationSequence(count);
          // TODO - remove or downgrade once MULE-1718 is fixed,
          // for now these really help see the problem if you set the level for this class to INFO
          if (logger.isInfoEnabled()) {
            logger.info("sending part " + count + " of " + parts);
          }
          super.route(part, session);
          if (logger.isInfoEnabled()) {
            logger.info("sent");
          }
        }

      } catch (RoutingException e) {
        // we'll want to send the whole message to the Exception handler
        e =
            new RoutingException(
                e.getI18nMessage(), e.getMuleMessage(), e.getEndpoint(), e.getCause());
        // e.addInfo("chunking", "true");
        // buffer = new byte[data.length - len];
        // System.arraycopy(data, len, buffer, 0, buffer.length);
        // e.addInfo("remaining data", buffer);
        throw e;
      }
    }
    return message;
  }
Example #3
0
  public void testResponseEventsCleanedUp() throws Exception {
    // relax access to get to the responseEvents
    RelaxedResponseAggregator aggregator = new RelaxedResponseAggregator();

    MuleEvent event = getTestEvent("message1");
    final MuleMessage message = event.getMessage();
    final String id = message.getUniqueId();
    message.setCorrelationId(id);
    message.setCorrelationGroupSize(1);
    aggregator.process(event);

    aggregator.getResponse(message);

    Map responseEvents = aggregator.getResponseEvents();
    assertTrue("Response events should be cleaned up.", responseEvents.isEmpty());
  }
Example #4
0
  public void testOverwriteFlagEnabledByDefault() throws Exception {
    MessagePropertiesTransformer t = new MessagePropertiesTransformer();
    Map add = new HashMap();
    add.put("addedProperty", "overwrittenValue");
    t.setAddProperties(add);
    t.setMuleContext(muleContext);

    MuleMessage msg = new DefaultMuleMessage("message", (Map) null);
    msg.setProperty("addedProperty", "originalValue");
    MuleEventContext ctx = getTestEventContext(msg);
    // context clones message
    msg = ctx.getMessage();
    DefaultMuleMessage transformed = (DefaultMuleMessage) t.transform(msg, null);
    assertSame(msg, transformed);
    assertEquals(msg.getUniqueId(), transformed.getUniqueId());
    assertEquals(msg.getPayload(), transformed.getPayload());
    // property values will be different
    assertEquals(msg.getPropertyNames(), transformed.getPropertyNames());

    assertEquals("overwrittenValue", transformed.getProperty("addedProperty"));
  }
 protected void assertEqualMessages(MuleMessage expect, MuleMessage actual) {
   assertEquals(expect.getPayload(), actual.getPayload());
   assertEquals(expect.getEncoding(), actual.getEncoding());
   assertEquals(expect.getUniqueId(), actual.getUniqueId());
   assertEquals(expect.getExceptionPayload(), actual.getExceptionPayload());
 }