/**
   * Test redelivered flag is set on rollbacked transactions
   *
   * @throws Exception
   */
  public void testRedilveredFlagSetOnRollback() throws Exception {
    Destination destination = createDestination(getClass().getName());
    Connection connection = createConnection();
    connection.setClientID(idGen.generateId());
    connection.start();
    Session consumerSession = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
    MessageConsumer consumer = null;
    if (topic) {
      consumer = consumerSession.createDurableSubscriber((Topic) destination, "TESTRED");
    } else {
      consumer = consumerSession.createConsumer(destination);
    }
    Session producerSession = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = producerSession.createProducer(destination);
    producer.setDeliveryMode(deliveryMode);

    TextMessage sentMsg = producerSession.createTextMessage();
    sentMsg.setText("msg1");
    producer.send(sentMsg);
    producerSession.commit();

    Message recMsg = consumer.receive(RECEIVE_TIMEOUT);
    assertFalse(recMsg.getJMSRedelivered());
    recMsg = consumer.receive(RECEIVE_TIMEOUT);
    consumerSession.rollback();
    recMsg = consumer.receive(RECEIVE_TIMEOUT);
    assertTrue(recMsg.getJMSRedelivered());
    consumerSession.commit();
    assertTrue(recMsg.equals(sentMsg));
    assertTrue(recMsg.getJMSRedelivered());
    connection.close();
  }
Esempio n. 2
0
  public void testRedeliveryWithClientAck() throws Exception {

    String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL;
    sendFrame(frame);

    frame = receiveFrame(10000);
    Assert.assertTrue(frame.startsWith("CONNECTED"));

    frame =
        "SUBSCRIBE\n"
            + "destination:"
            + getQueuePrefix()
            + getQueueName()
            + "\n"
            + "ack:client\n\n"
            + Stomp.NULL;

    sendFrame(frame);

    sendMessage(getName());
    frame = receiveFrame(10000);
    Assert.assertTrue(frame.startsWith("MESSAGE"));

    frame = "DISCONNECT\n" + "\n\n" + Stomp.NULL;
    sendFrame(frame);

    // message should be received since message was not acknowledged
    MessageConsumer consumer = session.createConsumer(queue);
    Message message = consumer.receive(1000);
    Assert.assertNotNull(message);
    Assert.assertTrue(message.getJMSRedelivered());
  }
Esempio n. 3
0
  public void onMessage(Message message) {
    try {
      if (logger.isDebugEnabled()) {
        logger.debug("Message received it is of type: " + message.getClass().getName());
        if (message.getJMSDestination() != null) {
          logger.debug(
              "Message received on "
                  + message.getJMSDestination()
                  + " ("
                  + message.getJMSDestination().getClass().getName()
                  + ")");
        } else {
          logger.debug("Message received on unknown destination");
        }
        logger.debug("Message CorrelationId is: " + message.getJMSCorrelationID());
        logger.debug("Jms Message Id is: " + message.getJMSMessageID());
      }

      if (message.getJMSRedelivered()) {
        if (logger.isDebugEnabled()) {
          logger.debug(
              "Message with correlationId: "
                  + message.getJMSCorrelationID()
                  + " is redelivered. handing off to Exception Handler");
        }
        redeliveryHandler.handleRedelivery(message);
      }

      UMOMessageAdapter adapter = connector.getMessageAdapter(message);
      routeMessage(new MuleMessage(adapter));
    } catch (Exception e) {
      handleException(e);
    }
  }
Esempio n. 4
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);
    }
  public void testNoExceptionOnRedeliveryAckWithSimpleTopicConsumer() throws Exception {
    Destination destination = createDestination(getClass().getName());
    Connection connection = createConnection();
    final AtomicBoolean gotException = new AtomicBoolean();
    connection.setExceptionListener(
        new ExceptionListener() {
          @Override
          public void onException(JMSException exception) {
            LOG.error("unexpected ex:" + exception);
            gotException.set(true);
          }
        });
    connection.setClientID(idGen.generateId());
    connection.start();
    Session consumerSession = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
    MessageConsumer consumer = null;
    if (topic) {
      consumer = consumerSession.createConsumer(destination);
    } else {
      consumer = consumerSession.createConsumer(destination);
    }
    Session producerSession = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = producerSession.createProducer(destination);
    producer.setDeliveryMode(deliveryMode);

    TextMessage sentMsg = producerSession.createTextMessage();
    sentMsg.setText("msg1");
    producer.send(sentMsg);
    producerSession.commit();

    Message recMsg = consumer.receive(RECEIVE_TIMEOUT);
    assertFalse(recMsg.getJMSRedelivered());
    recMsg = consumer.receive(RECEIVE_TIMEOUT);
    consumerSession.rollback();
    recMsg = consumer.receive(RECEIVE_TIMEOUT);
    assertTrue(recMsg.getJMSRedelivered());
    consumerSession.rollback();
    recMsg = consumer.receive(RECEIVE_TIMEOUT);
    assertTrue(recMsg.getJMSRedelivered());
    consumerSession.commit();
    assertTrue(recMsg.equals(sentMsg));
    assertTrue(recMsg.getJMSRedelivered());
    connection.close();

    assertFalse("no exception", gotException.get());
  }
  /**
   * Get redelivered status
   *
   * @return The value
   * @throws JMSException Thrown if an error occurs
   */
  @Override
  public boolean getJMSRedelivered() throws JMSException {
    if (ActiveMQRAMessage.trace) {
      ActiveMQRALogger.LOGGER.trace("getJMSRedelivered()");
    }

    return message.getJMSRedelivered();
  }
  @Test
  public void testIsNewForMessage() throws Exception {
    Message message = EasyMock.createMock(Message.class);
    EasyMock.expect(message.getJMSRedelivered()).andReturn(true);
    EasyMock.replay(message);
    assertEquals(false, newMethodArgumentsIdentifier.isNew(new Object[] {message}));

    EasyMock.verify(message);
  }
 @SuppressWarnings("boxing")
 static void mockHeaders(Message message) throws JMSException {
   when(message.getJMSCorrelationID()).thenReturn("1");
   when(message.getJMSDeliveryMode()).thenReturn(DeliveryMode.PERSISTENT);
   when(message.getJMSExpiration()).thenReturn(0l);
   when(message.getJMSMessageID()).thenReturn("2");
   when(message.getJMSPriority()).thenReturn(3);
   when(message.getJMSRedelivered()).thenReturn(true);
 }
  @Test
  public void testSimpleRollback() throws Exception {
    // send a message
    Connection conn = null;

    try {
      conn = createConnection();
      Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      s.createProducer(queue1).send(s.createTextMessage("one"));

      s.close();

      s = conn.createSession(true, Session.SESSION_TRANSACTED);
      MessageConsumer c = s.createConsumer(queue1);
      conn.start();
      Message m = c.receive(1000);
      ProxyAssertSupport.assertNotNull(m);

      ProxyAssertSupport.assertEquals("one", ((TextMessage) m).getText());
      ProxyAssertSupport.assertFalse(m.getJMSRedelivered());
      ProxyAssertSupport.assertEquals(1, m.getIntProperty("JMSXDeliveryCount"));

      s.rollback();

      // get the message again
      m = c.receive(1000);
      ProxyAssertSupport.assertNotNull(m);

      ProxyAssertSupport.assertTrue(m.getJMSRedelivered());
      ProxyAssertSupport.assertEquals(2, m.getIntProperty("JMSXDeliveryCount"));

      conn.close();

      Long i = getMessageCountForQueue("Queue1");

      ProxyAssertSupport.assertEquals(1, i.intValue());
    } finally {
      if (conn != null) {
        conn.close();
      }
      removeAllMessages(queue1.getQueueName(), true);
    }
  }
Esempio n. 10
0
  public Map<String, Object> extractHeadersFromJms(Message jmsMessage, Exchange exchange) {
    Map<String, Object> map = new HashMap<String, Object>();
    if (jmsMessage != null) {
      // lets populate the standard JMS message headers
      try {
        map.put("JMSCorrelationID", jmsMessage.getJMSCorrelationID());
        map.put("JMSCorrelationIDAsBytes", JmsMessageHelper.getJMSCorrelationIDAsBytes(jmsMessage));
        map.put("JMSDeliveryMode", jmsMessage.getJMSDeliveryMode());
        map.put("JMSDestination", jmsMessage.getJMSDestination());
        map.put("JMSExpiration", jmsMessage.getJMSExpiration());
        map.put("JMSMessageID", jmsMessage.getJMSMessageID());
        map.put("JMSPriority", jmsMessage.getJMSPriority());
        map.put("JMSRedelivered", jmsMessage.getJMSRedelivered());
        map.put("JMSTimestamp", jmsMessage.getJMSTimestamp());

        map.put("JMSReplyTo", JmsMessageHelper.getJMSReplyTo(jmsMessage));
        map.put("JMSType", JmsMessageHelper.getJMSType(jmsMessage));

        // this works around a bug in the ActiveMQ property handling
        map.put(JMS_X_GROUP_ID, JmsMessageHelper.getStringProperty(jmsMessage, JMS_X_GROUP_ID));
        map.put("JMSXUserID", JmsMessageHelper.getStringProperty(jmsMessage, "JMSXUserID"));
      } catch (JMSException e) {
        throw new RuntimeCamelException(e);
      }

      Enumeration<?> names;
      try {
        names = jmsMessage.getPropertyNames();
      } catch (JMSException e) {
        throw new RuntimeCamelException(e);
      }
      while (names.hasMoreElements()) {
        String name = names.nextElement().toString();
        try {
          Object value = JmsMessageHelper.getProperty(jmsMessage, name);
          if (headerFilterStrategy != null
              && headerFilterStrategy.applyFilterToExternalHeaders(name, value, exchange)) {
            continue;
          }

          // must decode back from safe JMS header name to original header name
          // when storing on this Camel JmsMessage object.
          String key = jmsKeyFormatStrategy.decodeKey(name);
          map.put(key, value);
        } catch (JMSException e) {
          throw new RuntimeCamelException(name, e);
        }
      }
    }

    return map;
  }
Esempio n. 11
0
 public static void constructConnectorMessageHeaders(Message jmsMsg, MessageHeaders msgHeaders)
     throws JMSException {
   msgHeaders.setJMSCorrelationID(jmsMsg.getJMSCorrelationID());
   msgHeaders.setJMSDeliveryMode(jmsMsg.getJMSDeliveryMode());
   msgHeaders.setJMSDestination(jmsMsg.getJMSDestination().toString());
   msgHeaders.setJMSExpiration(jmsMsg.getJMSExpiration());
   msgHeaders.setJMSMessageID(jmsMsg.getJMSMessageID());
   msgHeaders.setJMSPriority(jmsMsg.getJMSPriority());
   msgHeaders.setJMSRedelivered(jmsMsg.getJMSRedelivered());
   msgHeaders.setJMSReplyTo(jmsMsg.getJMSReplyTo().toString());
   msgHeaders.setJMSTimestamp(jmsMsg.getJMSTimestamp());
   msgHeaders.setJMSType(jmsMsg.getJMSType());
 }
Esempio n. 12
0
 private static Map<String, Object> headers(final javax.jms.Message message) throws JMSException {
   final Map<String, Object> result = new HashMap<>();
   putHeadedIfPresent(result, CorrelationID, message.getJMSCorrelationID());
   putHeadedIfPresent(result, CorrelationIDAsBytes, message.getJMSCorrelationIDAsBytes());
   putHeadedIfPresent(result, DeliveryMode, message.getJMSDeliveryMode());
   putHeadedIfPresent(result, Destination, message.getJMSDestination());
   putHeadedIfPresent(result, Expiration, message.getJMSExpiration());
   putHeadedIfPresent(result, MessageID, message.getJMSMessageID());
   putHeadedIfPresent(result, Priority, message.getJMSPriority());
   putHeadedIfPresent(result, Redelivered, message.getJMSRedelivered());
   putHeadedIfPresent(result, ReplyTo, message.getJMSReplyTo());
   putHeadedIfPresent(result, Timestamp, message.getJMSTimestamp());
   putHeadedIfPresent(result, Type, message.getJMSType());
   return result;
 }
  static void assertHeaders(Message message, Operation op) throws JMSException {
    Object map = op.get(MESSAGE_HEADERS);

    assertNotNull(map);
    assertTrue(map instanceof OperationMap);

    OperationMap opMap = (OperationMap) map;

    assertEquals(CORRELATION_ID, message.getJMSCorrelationID(), opMap.get(CORRELATION_ID));
    assertEquals(
        DELIVERY_MODE,
        getDeliveryMode(message.getJMSDeliveryMode()).getLabel(),
        opMap.get(DELIVERY_MODE));
    assertEquals(EXPIRATION, Long.valueOf(message.getJMSExpiration()), opMap.get(EXPIRATION));
    assertEquals(MESSAGE_ID, message.getJMSMessageID(), opMap.get(MESSAGE_ID));
    assertEquals(PRIORITY, Integer.valueOf(message.getJMSPriority()), opMap.get(PRIORITY));
    assertEquals(REDELIVERED, Boolean.valueOf(message.getJMSRedelivered()), opMap.get(REDELIVERED));
  }
Esempio n. 14
0
  /** {@inheritDoc} */
  @Override
  public void mapFrom(JMSBindingData source, Context context) throws Exception {
    super.mapFrom(source, context);

    Message message = source.getMessage();

    // process JMS headers
    if (matches(HEADER_JMS_DESTINATION)) {
      context
          .setProperty(HEADER_JMS_DESTINATION, message.getJMSDestination())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_DELIVERY_MODE)) {
      context
          .setProperty(HEADER_JMS_DELIVERY_MODE, message.getJMSDeliveryMode())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_EXPIRATION)) {
      context
          .setProperty(HEADER_JMS_EXPIRATION, message.getJMSExpiration())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_PRIORITY)) {
      context
          .setProperty(HEADER_JMS_PRIORITY, message.getJMSPriority())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_MESSAGE_ID)) {
      context
          .setProperty(HEADER_JMS_MESSAGE_ID, message.getJMSMessageID())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_TIMESTAMP)) {
      context
          .setProperty(HEADER_JMS_TIMESTAMP, message.getJMSTimestamp())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_CORRELATION_ID)) {
      context
          .setProperty(HEADER_JMS_CORRELATION_ID, message.getJMSCorrelationID())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_REPLY_TO)) {
      context
          .setProperty(HEADER_JMS_REPLY_TO, message.getJMSReplyTo())
          .addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_TYPE)) {
      context.setProperty(HEADER_JMS_TYPE, message.getJMSType()).addLabels(JMS_HEADER_LABELS);
    }
    if (matches(HEADER_JMS_REDELIVERED)) {
      context
          .setProperty(HEADER_JMS_REDELIVERED, message.getJMSRedelivered())
          .addLabels(JMS_HEADER_LABELS);
    }

    // process JMS properties
    Enumeration<?> e = message.getPropertyNames();
    while (e.hasMoreElements()) {
      String key = e.nextElement().toString();
      if (matches(key)) {
        Object value = null;
        try {
          value = message.getObjectProperty(key);
        } catch (JMSException pce) {
          // ignore and keep going (here just to keep checkstyle happy)
          pce.getMessage();
        }
        if (value != null) {
          context.setProperty(key, value).addLabels(JMS_PROPERTY_LABELS);
        }
      } else if (matches(key, getIncludeRegexes(), new ArrayList<Pattern>())) {
        Object value = null;
        try {
          value = message.getObjectProperty(key);
        } catch (JMSException pce) {
          // ignore and keep going (here just to keep checkstyle happy)
          pce.getMessage();
        }
        if (value != null) {
          context.setProperty(key, value).addLabels(JMS_PROPERTY_LABELS);
        }
      }
    }
  }