Example #1
0
  public void testSendTransactedRollback() throws Exception {
    final int countDownInitialCount = 2;
    final CountDownLatch countDown = new CountDownLatch(countDownInitialCount);

    // This exception strategy will be invoked when a message is redelivered
    // after a rollback

    // setup the component and start Mule
    UMODescriptor descriptor =
        getDescriptor("testComponent", FunctionalTestComponent.class.getName());

    EventCallback callback =
        new EventCallback() {
          public void eventReceived(UMOEventContext context, Object Component) throws Exception {
            callbackCalled = true;
            currentTx = context.getCurrentTransaction();
            assertNotNull(currentTx);
            assertTrue(currentTx.isBegun());
            System.out.println("@@@@ Rolling back transaction @@@@");
            currentTx.setRollbackOnly();
            countDown.countDown();
          }
        };

    initialiseComponent(descriptor, UMOTransactionConfig.ACTION_ALWAYS_BEGIN, callback);
    UMOManager manager = MuleManager.getInstance();
    addResultListener(getOutDest().getAddress(), countDown);

    UMOConnector umoCnn = manager.lookupConnector(CONNECTOR_NAME);
    // Set the test Exception strategy
    umoCnn.setExceptionListener(new RollbackExceptionListener(countDown));

    // Start the server
    manager.start();

    // Send a test message firstso that it is there when the component is
    // started
    send(DEFAULT_MESSAGE, false, Session.AUTO_ACKNOWLEDGE);

    afterInitialise();
    countDown.tryLock(LOCK_WAIT, TimeUnit.MILLISECONDS);
    assertTrue(
        "Only "
            + (countDownInitialCount - countDown.getCount())
            + " of "
            + countDownInitialCount
            + " checkpoints hit",
        countDown.tryLock());

    // Sleep a while to allow transaction to be rolled back
    afterInitialise();

    assertNull(currentMsg);
    assertTrue(callbackCalled);
    assertTrue(currentTx.isRolledBack());

    // Make sure the message isn't on the queue
    assertNull(receive(getInDest().getAddress(), 2000));
  }
Example #2
0
  public void testSendTransactedAlways() throws Exception {
    final int countDownInitialCount = 2;
    final CountDownLatch countDown = new CountDownLatch(countDownInitialCount);

    // setup the component and start Mule
    UMODescriptor descriptor =
        getDescriptor("testComponent", FunctionalTestComponent.class.getName());

    EventCallback callback =
        new EventCallback() {
          public void eventReceived(UMOEventContext context, Object Component) throws Exception {
            callbackCalled = true;
            currentTx = context.getCurrentTransaction();
            assertNotNull(currentTx);
            assertTrue(currentTx.isBegun());
            countDown.countDown();
          }
        };

    initialiseComponent(descriptor, UMOTransactionConfig.ACTION_ALWAYS_BEGIN, callback);

    // Start the server
    MuleManager.getInstance().start();
    addResultListener(getOutDest().getAddress(), countDown);

    // Send a test message first so that it is there when the component is
    // started
    send(DEFAULT_MESSAGE, false, getAcknowledgementMode());

    countDown.tryLock(LOCK_WAIT, TimeUnit.MILLISECONDS);
    assertTrue(
        "Only "
            + (countDownInitialCount - countDown.getCount())
            + " of "
            + countDownInitialCount
            + " checkpoints hit",
        countDown.tryLock());

    assertNotNull(currentMsg);
    assertTrue(currentMsg instanceof TextMessage);
    assertEquals(DEFAULT_MESSAGE + " Received", ((TextMessage) currentMsg).getText());
    assertTrue(callbackCalled);
    assertTrue(currentTx.isBegun());
    // todo for some reason, it takes a while for committed flag on the tx
    // to update
    Thread.sleep(1000);
    assertTrue(currentTx.isCommitted());
  }
Example #3
0
    public void handleMessagingException(UMOMessage message, Throwable t) {
      System.out.println("@@@@ ExceptionHandler Called @@@@");
      if (t instanceof MessageRedeliveredException) {
        countDown.countDown();
        try {
          // MessageRedeliveredException mre =
          // (MessageRedeliveredException)t;
          Message msg = (Message) message.getPayload();

          assertNotNull(msg);
          assertTrue(msg.getJMSRedelivered());
          assertTrue(msg instanceof TextMessage);
          // No need to commit transaction as the Tx template will
          // auto
          // matically commit by default
          super.handleMessagingException(message, t);
        } catch (Exception e) {
          fail(e.getMessage());
        }
      } else {
        t.printStackTrace();
        fail(t.getMessage());
      }
      super.handleMessagingException(message, t);
    }
Example #4
0
  public void testSendNotTransacted() throws Exception {
    UMODescriptor descriptor =
        getDescriptor("testComponent", FunctionalTestComponent.class.getName());

    final int countDownInitialCount = 2;
    final CountDownLatch countDown = new CountDownLatch(countDownInitialCount);

    EventCallback callback =
        new EventCallback() {
          public void eventReceived(UMOEventContext context, Object Component) {
            callbackCalled = true;
            assertNull(context.getCurrentTransaction());
            countDown.countDown();
          }
        };

    initialiseComponent(descriptor, UMOTransactionConfig.ACTION_NONE, callback);
    addResultListener(getOutDest().getAddress(), countDown);
    MuleManager.getInstance().start();
    afterInitialise();
    send(DEFAULT_MESSAGE, false, Session.AUTO_ACKNOWLEDGE);

    countDown.tryLock(LOCK_WAIT, TimeUnit.MILLISECONDS);
    assertTrue(
        "Only "
            + (countDownInitialCount - countDown.getCount())
            + " of "
            + countDownInitialCount
            + " checkpoints hit",
        countDown.tryLock());

    assertNotNull(currentMsg);
    assertTrue(currentMsg instanceof TextMessage);
    assertEquals(DEFAULT_MESSAGE + " Received", ((TextMessage) currentMsg).getText());
    assertTrue(callbackCalled);
    assertNull(currentTx);
  }
Example #5
0
  public void testTransactedRedeliveryToDLDestination() throws Exception {
    // there are 2 check points for each message delivered, so
    // the message will be delivered twice before this countdown will release
    final int countDownInitialCount = 4;
    final CountDownLatch countDown = new CountDownLatch(countDownInitialCount);

    // setup the component and start Mule
    UMODescriptor descriptor =
        getDescriptor("testComponent", FunctionalTestComponent.class.getName());

    EventCallback callback =
        new EventCallback() {
          public void eventReceived(UMOEventContext context, Object Component) throws Exception {
            callbackCalled = true;
            currentTx = context.getCurrentTransaction();
            assertNotNull(currentTx);
            assertTrue(currentTx.isBegun());
            System.out.println("@@@@ Rolling back transaction @@@@");
            currentTx.setRollbackOnly();
            countDown.countDown();
          }
        };

    initialiseComponent(descriptor, UMOTransactionConfig.ACTION_ALWAYS_BEGIN, callback);
    UMOManager manager = MuleManager.getInstance();
    addResultListener(getDLDest().getAddress(), countDown);

    JmsConnector umoCnn = (JmsConnector) manager.lookupConnector(CONNECTOR_NAME);

    // After redelivery retry the message and then fail
    umoCnn.setMaxRedelivery(1);

    // Set the test Exception strategy
    umoCnn.setExceptionListener(new RollbackExceptionListener(countDown, getDLDest()));

    // Start the server
    manager.start();

    // Send a test message firstso that it is there when the component is
    // started
    send(DEFAULT_MESSAGE, false, Session.AUTO_ACKNOWLEDGE);

    afterInitialise();
    countDown.tryLock(LOCK_WAIT, TimeUnit.MILLISECONDS);
    assertTrue(
        "Only "
            + (countDownInitialCount - countDown.getCount())
            + " of "
            + countDownInitialCount
            + " checkpoints hit",
        countDown.tryLock());

    assertNotNull(currentMsg);
    System.out.println(currentMsg);
    String dest = currentMsg.getStringProperty(MuleProperties.MULE_ENDPOINT_PROPERTY);
    assertNotNull(dest);
    assertEquals(getDLDest().getUri().toString(), dest);
    assertTrue(callbackCalled);

    // Make sure the message isn't on the queue
    assertNull(receive(getInDest().getAddress(), 2000));
  }
Example #6
0
  private void doSendTransactedIfPossible(final boolean transactionAvailable) throws Exception {
    final int countDownInitialCount = 2;
    final CountDownLatch countDown = new CountDownLatch(countDownInitialCount);

    // setup the component and start Mule
    UMODescriptor descriptor =
        getDescriptor("testComponent", FunctionalTestComponent.class.getName());

    EventCallback callback =
        new EventCallback() {
          public void eventReceived(UMOEventContext context, Object Component) throws Exception {
            callbackCalled = true;
            currentTx = context.getCurrentTransaction();
            if (transactionAvailable) {
              assertNotNull(currentTx);
              assertTrue(currentTx.isBegun());
            } else {
              assertNull(currentTx);
            }
            countDown.countDown();
          }
        };

    initialiseComponent(
        descriptor,
        (transactionAvailable
            ? UMOTransactionConfig.ACTION_ALWAYS_BEGIN
            : UMOTransactionConfig.ACTION_NONE),
        callback);

    // Start the server
    managementContext.start();
    addResultListener(getOutDest().getAddress(), countDown);

    // Send a test message firstso that it is there when the component is
    // started
    send(DEFAULT_MESSAGE, false, Session.AUTO_ACKNOWLEDGE);

    countDown.tryLock(LOCK_WAIT, TimeUnit.MILLISECONDS);
    assertTrue(
        "Only "
            + (countDownInitialCount - countDown.getCount())
            + " of "
            + countDownInitialCount
            + " checkpoints hit",
        countDown.tryLock(LOCK_WAIT, TimeUnit.MILLISECONDS));

    assertNotNull(currentMsg);
    assertTrue(currentMsg instanceof TextMessage);
    assertEquals(DEFAULT_MESSAGE + " Received", ((TextMessage) currentMsg).getText());
    assertTrue(callbackCalled);

    if (transactionAvailable) {
      assertNotNull(currentTx);
      assertTrue(currentTx.isBegun());
      // TODO for some reason, it takes a while for committed flag on the
      // tx to update
      Thread.sleep(300);
      assertTrue(currentTx.isCommitted());
    } else {
      assertNull(currentTx);
    }
  }