@Override
 public void close() {
   try {
     session.rollback();
     session.close();
   } catch (Exception ignored) {
   }
 }
示例#2
0
  @Test
  public void testDelayedRedeliveryDefaultOnRollback() throws Exception {
    ClientSessionFactory sessionFactory = createSessionFactory(locator);
    ClientSession session = sessionFactory.createSession(false, false, false);

    session.createQueue(qName, qName, null, true);
    session.close();

    ClientSession session1 = sessionFactory.createSession(false, true, true);
    ClientProducer producer = session1.createProducer(qName);

    final int NUM_MESSAGES = 5;

    for (int i = 0; i < NUM_MESSAGES; i++) {
      ClientMessage tm = createDurableMessage(session1, "message" + i);
      producer.send(tm);
    }
    session1.close();

    ClientSession session2 = sessionFactory.createSession(false, false, false);
    ClientConsumer consumer2 = session2.createConsumer(qName);

    session2.start();

    for (int i = 0; i < NUM_MESSAGES; i++) {
      ClientMessage tm = consumer2.receive(500);
      Assert.assertNotNull(tm);
      Assert.assertEquals("message" + i, tm.getBodyBuffer().readString());
    }

    // Now rollback
    long now = System.currentTimeMillis();

    session2.rollback();

    // This should redeliver with a delayed redelivery

    for (int i = 0; i < NUM_MESSAGES; i++) {
      ClientMessage tm = consumer2.receive(DelayedMessageTest.DELAY + 1000);
      Assert.assertNotNull(tm);

      long time = System.currentTimeMillis();

      Assert.assertTrue(time - now >= DelayedMessageTest.DELAY);

      // Hudson can introduce a large degree of indeterminism
      Assert.assertTrue(
          time - now + ">" + (DelayedMessageTest.DELAY + 1000),
          time - now < DelayedMessageTest.DELAY + 1000);
    }

    session2.commit();
    session2.close();
  }
示例#3
0
  // https://jira.jboss.org/jira/browse/HORNETQ-111
  // Test that, on rollback credits are released for messages cleared in the buffer
  public void testConsumerCreditsOnRollbackLargeMessages() throws Exception {

    locator.setConsumerWindowSize(10000);
    locator.setMinLargeMessageSize(1000);

    ClientSessionFactory sf = createSessionFactory(locator);

    ClientSession session = sf.createTransactedSession();

    session.createQueue(QUEUE, QUEUE, null, false);

    ClientProducer producer = session.createProducer(QUEUE);

    final int numMessages = 100;

    final byte[] bytes = new byte[10000];

    for (int i = 0; i < numMessages; i++) {
      ClientMessage message = session.createMessage(false);

      message.getBodyBuffer().writeBytes(bytes);

      message.putIntProperty("count", i);

      producer.send(message);
    }

    session.commit();

    ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();

    for (int i = 0; i < 110; i++) {
      ClientMessage message = consumer.receive();

      int count = message.getIntProperty("count");

      boolean redelivered = message.getDeliveryCount() > 1;

      if (count % 2 == 0 && !redelivered) {
        session.rollback();
      } else {
        session.commit();
      }
    }

    session.close();
  }
    @Override
    public void close() {
      try {

        if (!openConsumerOnEveryLoop) {
          consumer.close();
        }
        session.rollback();
        session.close();

        if (!shareConnectionFactory) {
          sf.close();
          locator.close();
        }
      } catch (Exception ignored) {
      }
    }
示例#5
0
  @Test
  public void testManyMessagesReceivedWithRollback() throws Exception {
    Xid xid = new XidImpl("bq1".getBytes(), 4, "gtid1".getBytes());
    ClientProducer producer = clientSession.createProducer(address);
    ClientConsumer consumer = clientSessionXa.createConsumer(qName1);

    SimpleString rh = new SimpleString("SMID1");
    ClientMessage m1 = createTextMessage(clientSession, "m1");
    m1.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
    m1.setDurable(true);
    ClientMessage m2 = createTextMessage(clientSession, "m2");
    m2.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
    m2.setDurable(true);
    ClientMessage m3 = createTextMessage(clientSession, "m3");
    m3.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
    m3.setDurable(true);
    ClientMessage m4 = createTextMessage(clientSession, "m4");
    m4.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
    m4.setDurable(true);
    ClientMessage m5 = createTextMessage(clientSession, "m5");
    m5.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
    m5.setDurable(true);
    ClientMessage m6 = createTextMessage(clientSession, "m6");
    m6.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
    m6.setDurable(true);
    clientSessionXa.start(xid, XAResource.TMNOFLAGS);
    clientSessionXa.start();
    producer.send(m1);
    ClientMessage m = consumer.receive(1000);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m1");
    producer.send(m2);
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m2");
    producer.send(m3);
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m3");
    producer.send(m4);
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m4");
    producer.send(m5);
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m5");
    producer.send(m6);
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m6");
    clientSessionXa.end(xid, XAResource.TMSUCCESS);
    clientSessionXa.prepare(xid);

    clientSession.close();
    clientSessionXa.close();
    restartServer();

    clientSessionXa.rollback(xid);
    consumer = clientSession.createConsumer(qName1);
    clientSession.start();
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    m.acknowledge();
    Assert.assertEquals(m.getBodyBuffer().readString(), "m6");
    m = consumer.receiveImmediate();
    Assert.assertNull(m);
  }
示例#6
0
  @Test
  public void testHangOnDelivery() throws Exception {
    queue = server.createQueue(QUEUE, QUEUE, null, true, false);
    try {

      ClientSessionFactory factory = locator.createSessionFactory();
      ClientSession sessionProducer = factory.createSession(false, false, false);

      ServerLocator consumerLocator = createInVMNonHALocator();
      ClientSessionFactory factoryConsumer = consumerLocator.createSessionFactory();
      ClientSession sessionConsumer = factoryConsumer.createSession();

      ClientProducer producer = sessionProducer.createProducer(QUEUE);

      ClientConsumer consumer = sessionConsumer.createConsumer(QUEUE);

      producer.send(sessionProducer.createMessage(true));

      blockConsumers();

      sessionProducer.commit();

      sessionConsumer.start();

      awaitBlocking();

      // this shouldn't lock
      producer.send(sessionProducer.createMessage(true));
      sessionProducer.commit();

      // These two operations should finish without the test hanging
      queue.getMessagesAdded(1);
      queue.getMessageCount(1);

      releaseConsumers();

      // a rollback to make sure everything will be reset on the deliveries
      // and that both consumers will receive each a message
      // this is to guarantee the server will have both consumers regsitered
      sessionConsumer.rollback();

      // a flush to guarantee any pending task is finished on flushing out delivery and pending msgs
      queue.flushExecutor();
      Assert.assertEquals(2, queue.getMessageCount());
      Assert.assertEquals(2, queue.getMessagesAdded());

      ClientMessage msg = consumer.receive(5000);
      Assert.assertNotNull(msg);
      msg.acknowledge();

      msg = consumer.receive(5000);
      Assert.assertNotNull(msg);
      msg.acknowledge();

      sessionProducer.commit();
      sessionConsumer.commit();

      sessionProducer.close();
      sessionConsumer.close();
    } finally {
      releaseConsumers();
    }
  }
示例#7
0
  @Test
  public void testOrderOverRollback2() throws Throwable {
    boolean persistentMessages = true;

    Configuration config = createDefaultConfig();

    config.setJournalSyncNonTransactional(false);

    HornetQServer server =
        createServer(true, config, PAGE_SIZE, PAGE_MAX, new HashMap<String, AddressSettings>());

    server.start();

    final int messageSize = 1024;

    final int numberOfMessages = 200;

    ServerLocator locator = createInVMNonHALocator();

    locator.setClientFailureCheckPeriod(1000);
    locator.setConnectionTTL(2000);
    locator.setReconnectAttempts(0);

    locator.setBlockOnNonDurableSend(true);
    locator.setBlockOnDurableSend(true);
    locator.setBlockOnAcknowledge(true);
    locator.setConsumerWindowSize(0);

    ClientSessionFactory sf = createSessionFactory(locator);

    ClientSession session = sf.createSession(false, false, false);

    QueueImpl queue = (QueueImpl) server.createQueue(ADDRESS, ADDRESS, null, true, false);

    ClientProducer producer = session.createProducer(PagingTest.ADDRESS);

    byte[] body = new byte[messageSize];

    ByteBuffer bb = ByteBuffer.wrap(body);

    for (int j = 1; j <= messageSize; j++) {
      bb.put(getSamplebyte(j));
    }

    for (int i = 0; i < numberOfMessages; i++) {
      ClientMessage message = session.createMessage(persistentMessages);

      HornetQBuffer bodyLocal = message.getBodyBuffer();

      bodyLocal.writeBytes(body);

      message.putIntProperty(new SimpleString("id"), i);

      producer.send(message);
      if (i % 1000 == 0) {
        session.commit();
      }
    }

    session.commit();

    session.close();

    session = sf.createSession(false, false, 0);

    session.start();

    ClientConsumer consumer = session.createConsumer(ADDRESS);

    // number of references without paging
    int numberOfRefs = queue.getNumberOfReferences();

    // consume all non-paged references
    for (int ref = 0; ref < numberOfRefs; ref++) {
      ClientMessage msg = consumer.receive(5000);
      assertNotNull(msg);
      msg.acknowledge();
    }

    session.commit();

    session.close();

    session = sf.createSession(false, false, 0);

    session.start();

    consumer = session.createConsumer(ADDRESS);

    ClientMessage msg = consumer.receive(5000);
    assertNotNull(msg);
    int msgIDRolledBack = msg.getIntProperty("id").intValue();
    msg.acknowledge();

    session.rollback();

    msg = consumer.receive(5000);

    assertNotNull(msg);

    assertEquals(msgIDRolledBack, msg.getIntProperty("id").intValue());

    session.rollback();

    session.close();

    sf.close();
    locator.close();

    server.stop();

    server.start();

    locator = createInVMNonHALocator();

    locator.setClientFailureCheckPeriod(1000);
    locator.setConnectionTTL(2000);
    locator.setReconnectAttempts(0);

    locator.setBlockOnNonDurableSend(true);
    locator.setBlockOnDurableSend(true);
    locator.setBlockOnAcknowledge(true);
    locator.setConsumerWindowSize(0);

    sf = createSessionFactory(locator);

    session = sf.createSession(false, false, 0);

    session.start();

    consumer = session.createConsumer(ADDRESS);

    for (int i = msgIDRolledBack; i < numberOfMessages; i++) {
      ClientMessage message = consumer.receive(5000);
      assertNotNull(message);
      assertEquals(i, message.getIntProperty("id").intValue());
      message.acknowledge();
    }

    session.commit();

    session.close();
  }