/** Make sure redelivered flag is set on redelivery via rollback */
  @Test
  public void testRedeliveredQueue() throws Exception {
    Connection conn = null;

    try {
      conn = createConnection();

      Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
      MessageProducer producer = sess.createProducer(queue1);

      MessageConsumer consumer = sess.createConsumer(queue1);
      conn.start();

      Message mSent = sess.createTextMessage("igloo");
      producer.send(mSent);

      sess.commit();

      TextMessage mRec = (TextMessage) consumer.receive(2000);
      ProxyAssertSupport.assertEquals("igloo", mRec.getText());
      ProxyAssertSupport.assertFalse(mRec.getJMSRedelivered());

      sess.rollback();
      mRec = (TextMessage) consumer.receive(2000);
      ProxyAssertSupport.assertEquals("igloo", mRec.getText());
      ProxyAssertSupport.assertTrue(mRec.getJMSRedelivered());

      sess.commit();
    } finally {
      if (conn != null) {
        conn.close();
      }
    }
  }
  /**
   * Make sure redelivered flag is set on redelivery via rollback, different setup: we close the
   * rolled back session and we receive the message whose acknowledgment was cancelled on a new
   * session.
   */
  @Test
  public void testRedeliveredQueue2() throws Exception {
    Connection conn = null;

    try {
      conn = createConnection();

      Session sendSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      MessageProducer prod = sendSession.createProducer(queue1);
      prod.send(sendSession.createTextMessage("a message"));

      conn.close();

      conn = createConnection();
      Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);

      MessageConsumer cons = sess.createConsumer(queue1);

      conn.start();

      TextMessage tm = (TextMessage) cons.receive(1000);
      ProxyAssertSupport.assertNotNull(tm);

      ProxyAssertSupport.assertEquals("a message", tm.getText());

      ProxyAssertSupport.assertFalse(tm.getJMSRedelivered());
      ProxyAssertSupport.assertEquals(1, tm.getIntProperty("JMSXDeliveryCount"));

      sess.rollback();

      sess.close();

      Session sess2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      cons = sess2.createConsumer(queue1);

      tm = (TextMessage) cons.receive(1000);

      ProxyAssertSupport.assertEquals("a message", tm.getText());

      ProxyAssertSupport.assertEquals(2, tm.getIntProperty("JMSXDeliveryCount"));

      ProxyAssertSupport.assertTrue(tm.getJMSRedelivered());
    } finally {
      if (conn != null) {
        conn.close();
      }
    }
  }
  public void onMessage(final Message message) {
    try {
      // Step 9. We know the client is sending a text message so we cast
      TextMessage textMessage = (TextMessage) message;

      // Step 10. get the text from the message.
      String text = textMessage.getText();

      System.out.println("message " + text + " received");

      if (!textMessage.getJMSRedelivered()) {
        // Step 11. On first delivery get the transaction, take a look, and throw an exception
        Transaction tx = tm.getTransaction();

        if (tx != null) {
          System.out.println("something is wrong, there should be no global transaction: " + tx);
        } else {
          System.out.println(
              "there is no global transaction, although the message delivery is using a local transaction");
          System.out.println("let's throw an exception and see what happens");
          throw new RuntimeException("DOH!");
        }
      } else {
        // Step 12. Print the message
        System.out.println(
            "The message was redelivered since the message delivery used a local transaction");
      }

    } catch (JMSException e) {
      e.printStackTrace();
    } catch (SystemException e) {
      e.printStackTrace();
    }
  }
Beispiel #4
0
  public void testSubscribeWithClientAck() 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:/queue/"
            + 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);
    TextMessage message = (TextMessage) consumer.receive(1000);
    Assert.assertNotNull(message);
    Assert.assertTrue(message.getJMSRedelivered());
  }
  /**
   * test messages are acknowledged and recovered properly
   *
   * @throws Exception
   */
  public void testClientAcknowledge() throws Exception {
    Destination destination = createDestination(getClass().getName());
    Connection connection = createConnection();
    connection.setClientID(idGen.generateId());
    connection.start();
    Session consumerSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    MessageConsumer consumer = consumerSession.createConsumer(destination);
    Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = producerSession.createProducer(destination);
    producer.setDeliveryMode(deliveryMode);

    // send some messages

    TextMessage sent1 = producerSession.createTextMessage();
    sent1.setText("msg1");
    sent1.setStringProperty("str", "1");
    producer.send(sent1);

    TextMessage sent2 = producerSession.createTextMessage();
    sent2.setText("msg2");
    sent2.setStringProperty("str", "2");
    producer.send(sent2);

    TextMessage sent3 = producerSession.createTextMessage();
    sent2.setText("msg3");
    sent2.setStringProperty("str", "3");
    producer.send(sent3);

    TextMessage msgTest = (TextMessage) consumer.receive(RECEIVE_TIMEOUT);
    System.out.println("msgTest::" + msgTest + " // " + msgTest.getText());
    TextMessage rec2 = (TextMessage) consumer.receive(RECEIVE_TIMEOUT);
    System.out.println("msgTest::" + rec2 + " // " + rec2.getText());
    assertNull(consumer.receiveNoWait());

    // ack rec2
    rec2.acknowledge();

    TextMessage sent4 = producerSession.createTextMessage();
    sent4.setText("msg4");
    producer.send(sent4);

    TextMessage rec4 = (TextMessage) consumer.receive(RECEIVE_TIMEOUT);
    assertTrue(rec4.equals(sent4));
    consumerSession.recover();
    rec4 = (TextMessage) consumer.receive(RECEIVE_TIMEOUT);
    assertTrue(rec4.equals(sent4));
    assertTrue(rec4.getJMSRedelivered());
    rec4.acknowledge();
    connection.close();
  }
  @Test
  public void testRedeliveredFlagTopic() throws Exception {
    Connection conn = null;

    try {
      conn = createConnection();

      Session sessSend = conn.createSession(true, Session.SESSION_TRANSACTED);
      Session sess1 = conn.createSession(true, Session.SESSION_TRANSACTED);
      MessageConsumer consumer1 = sess1.createConsumer(ActiveMQServerTestCase.topic1);

      MessageProducer producer = sessSend.createProducer(ActiveMQServerTestCase.topic1);
      Message mSent = sessSend.createTextMessage("igloo");
      producer.send(mSent);
      sessSend.commit();

      conn.start();

      TextMessage mRec1 = (TextMessage) consumer1.receive(2000);
      ProxyAssertSupport.assertNotNull(mRec1);

      ProxyAssertSupport.assertEquals("igloo", mRec1.getText());
      ProxyAssertSupport.assertFalse(mRec1.getJMSRedelivered());

      sess1.rollback(); // causes redelivery for session

      mRec1 = (TextMessage) consumer1.receive(2000);
      ProxyAssertSupport.assertEquals("igloo", mRec1.getText());
      ProxyAssertSupport.assertTrue(mRec1.getJMSRedelivered());

      sess1.commit();
    } finally {
      if (conn != null) {
        conn.close();
      }
    }
  }
  @Override
  public boolean runExample() throws Exception {
    final int numMessages = 30;

    Connection connection = null;

    InitialContext initialContext = null;

    try {
      // Step 1. Get an initial context for looking up JNDI from the server #1
      initialContext = new InitialContext();

      // Step 2. Look up the JMS resources from JNDI
      Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
      ConnectionFactory connectionFactory =
          (ConnectionFactory) initialContext.lookup("ConnectionFactory");

      // Step 3. Create a JMS Connection
      connection = connectionFactory.createConnection();

      // Step 4. Create a *non-transacted* JMS Session with client acknowledgement
      Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);

      // Step 5. Start the connection to ensure delivery occurs
      connection.start();

      // Step 6. Create a JMS MessageProducer and a MessageConsumer
      MessageProducer producer = session.createProducer(queue);
      MessageConsumer consumer = session.createConsumer(queue);

      // Step 7. Send some messages to server #1, the live server
      for (int i = 0; i < numMessages; i++) {
        TextMessage message = session.createTextMessage("This is text message " + i);
        producer.send(message);
        System.out.println("Sent message: " + message.getText());
      }

      // Step 8. Receive and acknowledge a third of the sent messages
      TextMessage message0 = null;
      for (int i = 0; i < numMessages / 3; i++) {
        message0 = (TextMessage) consumer.receive(5000);
        System.out.println("Got message: " + message0.getText());
      }
      message0.acknowledge();

      // Step 9. Receive the rest third of the sent messages but *do not* acknowledge them yet
      for (int i = numMessages / 3; i < numMessages; i++) {
        message0 = (TextMessage) consumer.receive(5000);
        System.out.println("Got message: " + message0.getText());
      }

      // Step 10. Crash server #1, the live server, and wait a little while to make sure
      // it has really crashed
      Thread.sleep(5000);
      killServer(0);

      // Step 11. Acknowledging the 2nd half of the sent messages will fail as failover to the
      // backup server has occurred
      try {
        message0.acknowledge();
      } catch (JMSException e) {
        System.out.println(
            "Got (the expected) exception while acknowledging message: " + e.getMessage());
      }

      // Step 12. Consume again the 2nd third of the messages again. Note that they are not
      // considered as redelivered.
      for (int i = numMessages / 3; i < (numMessages / 3) * 2; i++) {
        message0 = (TextMessage) consumer.receive(5000);
        System.out.printf(
            "Got message: %s (redelivered?: %s)\n",
            message0.getText(), message0.getJMSRedelivered());
      }
      message0.acknowledge();

      reStartServer(0, 10000);

      Thread.sleep(10000);

      // Step 11. Acknowledging the 2nd half of the sent messages will fail as failover to the
      // backup server has occurred
      try {
        message0.acknowledge();
      } catch (JMSException e) {
        System.err.println("Got exception while acknowledging message: " + e.getMessage());
      }

      // Step 12. Consume again the 2nd third of the messages again. Note that they are not
      // considered as redelivered.
      for (int i = (numMessages / 3) * 2; i < numMessages; i++) {
        message0 = (TextMessage) consumer.receive(5000);
        System.out.printf(
            "Got message: %s (redelivered?: %s)\n",
            message0.getText(), message0.getJMSRedelivered());
      }
      message0.acknowledge();

      return true;
    } finally {
      // Step 13. Be sure to close our resources!

      if (connection != null) {
        connection.close();
      }

      if (initialContext != null) {
        initialContext.close();
      }
    }
  }