Exemple #1
0
  public HornetQBuffer toHornetQBuffer() throws Exception {
    if (buffer == null) {
      if (bytesBody != null) {
        buffer = HornetQBuffers.dynamicBuffer(bytesBody.length + 512);
      } else {
        buffer = HornetQBuffers.dynamicBuffer(512);
      }

      if (isPing()) {
        buffer.writeByte((byte) 10);
        return buffer;
      }

      StringBuffer head = new StringBuffer();
      head.append(command);
      head.append(Stomp.NEWLINE);
      // Output the headers.
      for (Map.Entry<String, String> header : headers.entrySet()) {
        head.append(header.getKey());
        head.append(Stomp.Headers.SEPARATOR);
        head.append(header.getValue());
        head.append(Stomp.NEWLINE);
      }
      // Add a newline to separate the headers from the content.
      head.append(Stomp.NEWLINE);

      buffer.writeBytes(head.toString().getBytes("UTF-8"));
      if (bytesBody != null) {
        buffer.writeBytes(bytesBody);
      }
      buffer.writeBytes(END_OF_FRAME);

      size = buffer.writerIndex();
    }
    return buffer;
  }
Exemple #2
0
 public void encodeHeadersAndProperties(final HornetQBuffer buffer) {
   buffer.writeLong(messageID);
   buffer.writeNullableSimpleString(address);
   if (userID == null) {
     buffer.writeByte(DataConstants.NULL);
   } else {
     buffer.writeByte(DataConstants.NOT_NULL);
     buffer.writeBytes(userID.asBytes());
   }
   buffer.writeByte(type);
   buffer.writeBoolean(durable);
   buffer.writeLong(expiration);
   buffer.writeLong(timestamp);
   buffer.writeByte(priority);
   properties.encode(buffer);
 }
Exemple #3
0
  // Decode from journal or paging
  public void decode(final HornetQBuffer buff) {
    int start = buff.readerIndex();

    endOfBodyPosition = buff.readInt();

    endOfMessagePosition = buff.getInt(endOfBodyPosition - BUFFER_HEADER_SPACE + start);

    int length = endOfMessagePosition - BUFFER_HEADER_SPACE;

    buffer.setIndex(0, BUFFER_HEADER_SPACE);

    buffer.writeBytes(buff, start, length);

    decode();

    buff.readerIndex(start + length);
  }
 public void encode(final HornetQBuffer buffer) {
   buffer.writeBytes(new byte[5]);
 }
Exemple #5
0
 public int encode(final HornetQBuffer bufferOut, final int size) {
   bufferOut.writeBytes(getWholeBuffer(), lastPos, size);
   lastPos += size;
   return size;
 }
Exemple #6
0
  // Encode to journal or paging
  public void encode(final HornetQBuffer buff) {
    encodeToBuffer();

    buff.writeBytes(buffer, BUFFER_HEADER_SPACE, endOfMessagePosition - BUFFER_HEADER_SPACE);
  }
Exemple #7
0
  public int sendMessage(ServerMessage serverMessage, long consumerID, int deliveryCount) {
    LargeServerMessageImpl largeMessage = null;
    ServerMessage newServerMessage = serverMessage;
    try {
      StompSubscription subscription = subscriptions.get(consumerID);
      StompFrame frame = null;
      if (serverMessage.isLargeMessage()) {
        newServerMessage = serverMessage.copy();

        largeMessage = (LargeServerMessageImpl) serverMessage;
        BodyEncoder encoder = largeMessage.getBodyEncoder();
        encoder.open();
        int bodySize = (int) encoder.getLargeBodySize();

        // large message doesn't have a body.
        ((ServerMessageImpl) newServerMessage).createBody(bodySize);
        encoder.encode(newServerMessage.getBodyBuffer(), bodySize);
        encoder.close();
      }

      if (serverMessage.getBooleanProperty(Message.HDR_LARGE_COMPRESSED)) {
        // decompress
        HornetQBuffer qbuff = newServerMessage.getBodyBuffer();
        int bytesToRead = qbuff.writerIndex() - MessageImpl.BODY_OFFSET;
        Inflater inflater = new Inflater();
        inflater.setInput(qbuff.readBytes(bytesToRead).toByteBuffer().array());

        // get the real size of large message
        long sizeBody = newServerMessage.getLongProperty(Message.HDR_LARGE_BODY_SIZE);

        byte[] data = new byte[(int) sizeBody];
        inflater.inflate(data);
        inflater.end();
        qbuff.resetReaderIndex();
        qbuff.resetWriterIndex();
        qbuff.writeBytes(data);
      }

      frame = connection.createStompMessage(newServerMessage, subscription, deliveryCount);

      int length = frame.getEncodedSize();

      if (subscription.getAck().equals(Stomp.Headers.Subscribe.AckModeValues.AUTO)) {
        if (manager.send(connection, frame)) {
          // we ack and commit only if the send is successful
          session.acknowledge(consumerID, newServerMessage.getMessageID());
          session.commit();
        }
      } else {
        messagesToAck.put(
            newServerMessage.getMessageID(), new Pair<Long, Integer>(consumerID, length));
        // Must send AFTER adding to messagesToAck - or could get acked from client BEFORE it's been
        // added!
        manager.send(connection, frame);
      }

      return length;
    } catch (Exception e) {
      return 0;
    } finally {
      if (largeMessage != null) {
        largeMessage.releaseResources();
        largeMessage = null;
      }
    }
  }
  @Test
  public void testOrder1() 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 = 500;
    ServerLocator locator = createInVMNonHALocator();

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

    locator.setBlockOnNonDurableSend(true);
    locator.setBlockOnDurableSend(true);
    locator.setBlockOnAcknowledge(true);
    locator.setConsumerWindowSize(1024 * 1024);

    ClientSessionFactory sf = createSessionFactory(locator);

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

    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(true, true, 0);

    session.start();

    ClientConsumer consumer = session.createConsumer(ADDRESS);

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

      if (i < 100) {
        // Do not consume the last one so we could restart
        message.acknowledge();
      }
    }

    session.close();

    session = null;

    sf.close();
    sf = createSessionFactory(locator);

    locator = createInVMNonHALocator();

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

    session.start();

    consumer = session.createConsumer(ADDRESS);

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

    session.close();
  }
  @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();
  }
  @Test
  public void testPageCounter2() 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 = 500;

    ServerLocator locator = createInVMNonHALocator();

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

    locator.setBlockOnNonDurableSend(true);
    locator.setBlockOnDurableSend(true);
    locator.setBlockOnAcknowledge(true);
    locator.setConsumerWindowSize(1024 * 1024);

    ClientSessionFactory sf = createSessionFactory(locator);

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

    Queue q1 = server.createQueue(ADDRESS, ADDRESS, null, true, false);

    Queue q2 = server.createQueue(ADDRESS, new SimpleString("inactive"), 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));
    }

    final AtomicInteger errors = new AtomicInteger(0);

    Thread t1 =
        new Thread() {
          @Override
          public void run() {
            try {
              ServerLocator sl = createInVMNonHALocator();
              ClientSessionFactory sf = sl.createSessionFactory();
              ClientSession sess = sf.createSession(true, true, 0);
              sess.start();
              ClientConsumer cons = sess.createConsumer(ADDRESS);
              for (int i = 0; i < 100; i++) {
                ClientMessage msg = cons.receive(5000);
                assertNotNull(msg);
                assertEquals(i, msg.getIntProperty("id").intValue());
                msg.acknowledge();
              }
              sess.close();
              sl.close();
            } catch (Throwable e) {
              e.printStackTrace();
              errors.incrementAndGet();
            }
          }
        };

    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 % 20 == 0) {
        session.commit();
      }
    }

    session.commit();

    t1.start();
    t1.join();

    assertEquals(0, errors.get());
    long timeout = System.currentTimeMillis() + 10000;
    while (numberOfMessages - 100 != q1.getMessageCount() && System.currentTimeMillis() < timeout) {
      Thread.sleep(500);
    }

    assertEquals(numberOfMessages, q2.getMessageCount());
    assertEquals(numberOfMessages, q2.getMessagesAdded());
    assertEquals(numberOfMessages - 100, q1.getMessageCount());
    assertEquals(numberOfMessages, q2.getMessagesAdded());
  }
  @Test
  public void testPageCounter() 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 = 500;

    ServerLocator locator = createInVMNonHALocator();

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

    locator.setBlockOnNonDurableSend(true);
    locator.setBlockOnDurableSend(true);
    locator.setBlockOnAcknowledge(true);
    locator.setConsumerWindowSize(1024 * 1024);

    ClientSessionFactory sf = createSessionFactory(locator);

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

    Queue q1 = server.createQueue(ADDRESS, ADDRESS, null, true, false);

    Queue q2 = server.createQueue(ADDRESS, new SimpleString("inactive"), 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));
    }

    final AtomicInteger errors = new AtomicInteger(0);

    Thread t1 =
        new Thread() {
          @Override
          public void run() {
            try {
              ServerLocator sl = createInVMNonHALocator();
              ClientSessionFactory sf = sl.createSessionFactory();
              ClientSession sess = sf.createSession(true, true, 0);
              sess.start();
              ClientConsumer cons = sess.createConsumer(ADDRESS);
              for (int i = 0; i < numberOfMessages; i++) {
                ClientMessage msg = cons.receive(5000);
                assertNotNull(msg);
                assertEquals(i, msg.getIntProperty("id").intValue());
                msg.acknowledge();
              }

              assertNull(cons.receiveImmediate());
              sess.close();
              sl.close();
            } catch (Throwable e) {
              e.printStackTrace();
              errors.incrementAndGet();
            }
          }
        };

    t1.start();

    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 % 20 == 0) {
        session.commit();
      }
    }

    session.commit();

    t1.join();

    assertEquals(0, errors.get());

    assertEquals(numberOfMessages, q2.getMessageCount());
    assertEquals(numberOfMessages, q2.getMessagesAdded());
    assertEquals(0, q1.getMessageCount());
    assertEquals(numberOfMessages, q1.getMessagesAdded());

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

    server.stop();

    server.start();

    Bindings bindings = server.getPostOffice().getBindingsForAddress(ADDRESS);

    q1 = null;
    q2 = null;

    for (Binding bind : bindings.getBindings()) {
      if (bind instanceof LocalQueueBinding) {
        LocalQueueBinding qb = (LocalQueueBinding) bind;
        if (qb.getQueue().getName().equals(ADDRESS)) {
          q1 = qb.getQueue();
        }

        if (qb.getQueue().getName().equals(new SimpleString("inactive"))) {
          q2 = qb.getQueue();
        }
      }
    }

    assertNotNull(q1);

    assertNotNull(q2);

    assertEquals("q2 msg count", numberOfMessages, q2.getMessageCount());
    assertEquals("q2 msgs added", numberOfMessages, q2.getMessagesAdded());
    assertEquals("q1 msg count", 0, q1.getMessageCount());
    // 0, since nothing was sent to the queue after the server was restarted
    assertEquals("q1 msgs added", 0, q1.getMessagesAdded());
  }