Java Code Examples for org.apache.qpid.proton.amqp.messaging.Header#setDurable()

The following examples show how to use org.apache.qpid.proton.amqp.messaging.Header#setDurable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HeaderType.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
public Header newInstance(Object described)
{
    List l = (List) described;

    Header o = new Header();


    switch(5 - l.size())
    {

        case 0:
            o.setDeliveryCount( (UnsignedInteger) l.get( 4 ) );
        case 1:
            o.setFirstAcquirer( (Boolean) l.get( 3 ) );
        case 2:
            o.setTtl( (UnsignedInteger) l.get( 2 ) );
        case 3:
            o.setPriority( (UnsignedByte) l.get( 1 ) );
        case 4:
            o.setDurable( (Boolean) l.get( 0 ) );
    }


    return o;
}
 
Example 2
Source File: Benchmark.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
private void benchmarkHeader() throws IOException {
    Header header = new Header();
    header.setDurable(true);
    header.setFirstAcquirer(true);

    resultSet.start();
    for (int i = 0; i < ITERATIONS; i++) {
        outputBuf.byteBuffer().clear();
        encoder.writeObject(header);
    }
    resultSet.encodesComplete();

    CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
    decoder.setBuffer(inputBuf);

    resultSet.start();
    for (int i = 0; i < ITERATIONS; i++) {
        decoder.readObject();
        inputBuf.flip();
    }
    resultSet.decodesComplete();

    time("Header", resultSet);
}
 
Example 3
Source File: HeaderTypeCodecTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSkipHeader() throws IOException {
    Header header1 = new Header();
    Header header2 = new Header();

    header1.setDurable(Boolean.FALSE);
    header2.setDurable(Boolean.TRUE);

    encoder.writeObject(header1);
    encoder.writeObject(header2);

    buffer.clear();

    TypeConstructor<?> headerType = decoder.readConstructor();
    assertEquals(Header.class, headerType.getTypeClass());
    headerType.skipValue();

    final Object result = decoder.readObject();

    assertNotNull(result);
    assertTrue(result instanceof Header);

    Header decoded = (Header) result;
    assertTrue(decoded.getDurable().booleanValue());
}
 
Example 4
Source File: AMQPPersisterTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private MessageImpl createProtonMessage(String address, byte[] content) {
   MessageImpl message = (MessageImpl) Proton.message();

   Header header = new Header();
   header.setDurable(true);
   header.setPriority(UnsignedByte.valueOf((byte) 9));

   Properties properties = new Properties();
   properties.setCreationTime(new Date(System.currentTimeMillis()));
   properties.setTo(address);
   properties.setMessageId(UUID.randomUUID());

   MessageAnnotations annotations = new MessageAnnotations(new LinkedHashMap<>());
   ApplicationProperties applicationProperties = new ApplicationProperties(new LinkedHashMap<>());

   AmqpValue body = new AmqpValue(Arrays.copyOf(content, content.length));

   message.setHeader(header);
   message.setMessageAnnotations(annotations);
   message.setProperties(properties);
   message.setApplicationProperties(applicationProperties);
   message.setBody(body);

   return message;
}
 
Example 5
Source File: AmqpHeaderTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateFromHeader() {
    Header protonHeader = new Header();
    protonHeader.setPriority(UnsignedByte.valueOf((byte) 9));
    protonHeader.setTtl(UnsignedInteger.valueOf(10));
    protonHeader.setDeliveryCount(UnsignedInteger.valueOf(11));
    protonHeader.setDurable(true);
    protonHeader.setFirstAcquirer(true);

    AmqpHeader header = new AmqpHeader(protonHeader);

    assertFalse(header.isDefault());
    assertTrue(header.nonDefaultDurable());
    assertTrue(header.nonDefaultPriority());
    assertTrue(header.nonDefaultTimeToLive());
    assertTrue(header.nonDefaultFirstAcquirer());
    assertTrue(header.nonDefaultDeliveryCount());

    assertEquals(true, header.isDurable());
    assertEquals(true, header.isFirstAcquirer());
    assertEquals(9, header.getPriority());
    assertEquals(10, header.getTimeToLive());
    assertEquals(11, header.getDeliveryCount());
}
 
Example 6
Source File: AmqpHeaderTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetHeaderWithDurableHeader() {
    AmqpHeader header = new AmqpHeader();

    Header protonHeader = new Header();
    protonHeader.setDurable(true);

    header.setHeader(protonHeader);

    assertFalse(header.isDefault());
    assertTrue(header.nonDefaultDurable());
    assertFalse(header.nonDefaultPriority());
    assertFalse(header.nonDefaultTimeToLive());
    assertFalse(header.nonDefaultFirstAcquirer());
    assertFalse(header.nonDefaultDeliveryCount());

    assertEquals(true, header.isDurable());
    assertEquals(false, header.isFirstAcquirer());
    assertEquals(4, header.getPriority());
    assertEquals(0, header.getTimeToLive());
    assertEquals(0, header.getDeliveryCount());
}
 
Example 7
Source File: AmqpHeaderTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetHeaderWithHeaderWithAllSet() {
    AmqpHeader header = new AmqpHeader();

    Header protonHeader = new Header();
    protonHeader.setPriority(UnsignedByte.valueOf((byte) 9));
    protonHeader.setTtl(UnsignedInteger.valueOf(10));
    protonHeader.setDeliveryCount(UnsignedInteger.valueOf(11));
    protonHeader.setDurable(true);
    protonHeader.setFirstAcquirer(true);

    header.setHeader(protonHeader);

    assertFalse(header.isDefault());
    assertTrue(header.nonDefaultDurable());
    assertTrue(header.nonDefaultPriority());
    assertTrue(header.nonDefaultTimeToLive());
    assertTrue(header.nonDefaultFirstAcquirer());
    assertTrue(header.nonDefaultDeliveryCount());

    assertEquals(true, header.isDurable());
    assertEquals(true, header.isFirstAcquirer());
    assertEquals(9, header.getPriority());
    assertEquals(10, header.getTimeToLive());
    assertEquals(11, header.getDeliveryCount());
}
 
Example 8
Source File: HeaderTypeCodecTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
private void doTestDecodeHeaderSeries(int size) throws IOException {
    Header header = new Header();

    header.setDurable(Boolean.TRUE);
    header.setPriority(UnsignedByte.valueOf((byte) 3));
    header.setDeliveryCount(UnsignedInteger.valueOf(10));
    header.setFirstAcquirer(Boolean.FALSE);
    header.setTtl(UnsignedInteger.valueOf(500));

    for (int i = 0; i < size; ++i) {
        encoder.writeObject(header);
    }

    buffer.clear();

    for (int i = 0; i < size; ++i) {
        final Object result = decoder.readObject();

        assertNotNull(result);
        assertTrue(result instanceof Header);

        Header decoded = (Header) result;

        assertEquals(3, decoded.getPriority().intValue());
        assertTrue(decoded.getDurable().booleanValue());
    }
}
 
Example 9
Source File: HeaderTypeCodecTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeHeaderArray() throws IOException {
    Header header = new Header();

    header.setDurable(Boolean.TRUE);
    header.setPriority(UnsignedByte.valueOf((byte) 3));
    header.setDeliveryCount(UnsignedInteger.valueOf(10));
    header.setFirstAcquirer(Boolean.FALSE);
    header.setTtl(UnsignedInteger.valueOf(500));

    Header[] source = new Header[32];

    for (int i = 0; i < source.length; ++i) {
        source[i] = header;
    }

    encoder.writeObject(source);

    buffer.clear();

    final Object result = decoder.readObject();

    assertNotNull(result);
    assertTrue(result.getClass().isArray());

    final Object[] resultArray = (Object[]) result;

    for (int i = 0; i < source.length; ++i) {

        assertTrue(resultArray[i] instanceof Header);

        Header decoded = (Header) resultArray[i];

        assertEquals(3, decoded.getPriority().intValue());
        assertTrue(decoded.getDurable().booleanValue());
        assertEquals(header.getDeliveryCount(), decoded.getDeliveryCount());
    }
}
 
Example 10
Source File: AMQPMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testPartialDecodeIgnoresDeliveryAnnotationsByDefault() {
   Header header = new Header();
   header.setDurable(true);
   header.setPriority(UnsignedByte.valueOf((byte) 6));

   ByteBuf encodedBytes = Unpooled.buffer(1024);
   NettyWritable writable = new NettyWritable(encodedBytes);

   EncoderImpl encoder = TLSEncode.getEncoder();
   encoder.setByteBuffer(writable);
   encoder.writeObject(header);

   // Signal body of AmqpValue but write corrupt underlying type info
   encodedBytes.writeByte(EncodingCodes.DESCRIBED_TYPE_INDICATOR);
   encodedBytes.writeByte(EncodingCodes.SMALLULONG);
   encodedBytes.writeByte(DELIVERY_ANNOTATIONS_DESCRIPTOR.byteValue());
   encodedBytes.writeByte(EncodingCodes.MAP8);
   encodedBytes.writeByte(2);  // Size
   encodedBytes.writeByte(2);  // Elements
   // Use bad encoding code on underlying type of map key which will fail the decode if run
   encodedBytes.writeByte(255);

   ReadableBuffer readable = new NettyReadable(encodedBytes);

   AMQPStandardMessage message = null;
   try {
      message = new AMQPStandardMessage(0, readable, null, null);
   } catch (Exception decodeError) {
      fail("Should not have encountered an exception on partial decode: " + decodeError.getMessage());
   }

   try {
      // This should perform the lazy decode of the DeliveryAnnotations portion of the message
      message.getDeliveryAnnotations();
      fail("Should have thrown an error when attempting to decode the ApplicationProperties which are malformed.");
   } catch (Exception ex) {
      // Expected decode to fail when building full message.
   }
}
 
Example 11
Source File: AMQPMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testPartialDecodeIgnoresBodyByDefault() {
   Header header = new Header();
   header.setDurable(true);
   header.setPriority(UnsignedByte.valueOf((byte) 6));

   ByteBuf encodedBytes = Unpooled.buffer(1024);
   NettyWritable writable = new NettyWritable(encodedBytes);

   EncoderImpl encoder = TLSEncode.getEncoder();
   encoder.setByteBuffer(writable);
   encoder.writeObject(header);

   // Signal body of AmqpValue but write corrupt underlying type info
   encodedBytes.writeByte(EncodingCodes.DESCRIBED_TYPE_INDICATOR);
   encodedBytes.writeByte(EncodingCodes.SMALLULONG);
   encodedBytes.writeByte(AMQPVALUE_DESCRIPTOR.byteValue());
   // Use bad encoding code on underlying type
   encodedBytes.writeByte(255);

   ReadableBuffer readable = new NettyReadable(encodedBytes);

   AMQPStandardMessage message = null;
   try {
      message = new AMQPStandardMessage(0, readable, null, null);
   } catch (Exception decodeError) {
      fail("Should not have encountered an exception on partial decode: " + decodeError.getMessage());
   }

   assertTrue(message.isDurable());

   try {
      // This will decode the body section if present in order to present it as a Proton Message object
      message.getBody();
      fail("Should have thrown an error when attempting to decode the body which is malformed.");
   } catch (Exception ex) {
      // Expected decode to fail when building full message.
   }
}
 
Example 12
Source File: AMQPMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private MessageImpl createProtonMessage() {
   MessageImpl message = (MessageImpl) Proton.message();

   Header header = new Header();
   header.setDurable(true);
   header.setPriority(UnsignedByte.valueOf((byte) 9));

   Properties properties = new Properties();
   properties.setCreationTime(new Date(System.currentTimeMillis()));
   properties.setTo(TEST_TO_ADDRESS);
   properties.setMessageId(UUID.randomUUID());

   MessageAnnotations annotations = new MessageAnnotations(new LinkedHashMap<>());
   annotations.getValue().put(Symbol.valueOf(TEST_MESSAGE_ANNOTATION_KEY), TEST_MESSAGE_ANNOTATION_VALUE);

   ApplicationProperties applicationProperties = new ApplicationProperties(new LinkedHashMap<>());
   applicationProperties.getValue().put(TEST_APPLICATION_PROPERTY_KEY, TEST_APPLICATION_PROPERTY_VALUE);

   AmqpValue body = new AmqpValue(TEST_STRING_BODY);

   message.setHeader(header);
   message.setMessageAnnotations(annotations);
   message.setProperties(properties);
   message.setApplicationProperties(applicationProperties);
   message.setBody(body);

   return message;
}
 
Example 13
Source File: FastPathHeaderType.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Override
public Header readValue() {
    DecoderImpl decoder = getDecoder();
    ReadableBuffer buffer = decoder.getBuffer();
    byte typeCode = decoder.getBuffer().get();

    @SuppressWarnings("unused")
    int size = 0;
    int count = 0;

    switch (typeCode) {
        case EncodingCodes.LIST0:
            break;
        case EncodingCodes.LIST8:
            size = buffer.get() & 0xff;
            count = buffer.get() & 0xff;
            break;
        case EncodingCodes.LIST32:
            size = buffer.getInt();
            count = buffer.getInt();
            break;
        default:
            throw new DecodeException("Incorrect type found in Header encoding: " + typeCode);
    }

    Header header = new Header();

    for (int index = 0; index < count; ++index) {
        switch (index) {
            case 0:
                header.setDurable(decoder.readBoolean(null));
                break;
            case 1:
                header.setPriority(decoder.readUnsignedByte(null));
                break;
            case 2:
                header.setTtl(decoder.readUnsignedInteger(null));
                break;
            case 3:
                header.setFirstAcquirer(decoder.readBoolean(null));
                break;
            case 4:
                header.setDeliveryCount(decoder.readUnsignedInteger(null));
                break;
            default:
                throw new IllegalStateException("To many entries in Header encoding");
        }
    }

    return header;
}
 
Example 14
Source File: HeaderBenchmark.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private void initHeader()
{
    header = new Header();
    header.setDurable(true);
    header.setFirstAcquirer(true);
}
 
Example 15
Source File: AmqpSendReceiveTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private void doTestBrokerRestartAndDurability(boolean durable, boolean enforceHeader, boolean explicitSetNonDurable) throws Exception {
   AmqpClient client = createAmqpClient();
   AmqpConnection connection = addConnection(client.connect());
   AmqpSession session = connection.createSession();

   AmqpSender sender = session.createSender(getQueueName());

   final Queue queueView1 = getProxyToQueue(getQueueName());

   Message protonMessage = Message.Factory.create();
   protonMessage.setMessageId("ID:Message:1");
   protonMessage.setBody(new AmqpValue("Test-Message -> " + (durable ? "durable" : "non-durable")));
   if (durable || enforceHeader) {
      Header header = new Header();
      if (durable) {
         header.setDurable(true);
      } else {
         if (explicitSetNonDurable) {
            header.setDurable(false);
         } else {
            // Set priority so the durable field gets defaulted
            header.setPriority(UnsignedByte.valueOf((byte) 5));
            assertNull(header.getDurable());
         }
      }

      protonMessage.setHeader(header);
   } else {
      assertNull("Should not have a header", protonMessage.getHeader());
   }

   AmqpMessage message = new AmqpMessage(protonMessage);

   sender.send(message);
   connection.close();

   Wait.assertEquals(1, queueView1::getMessageCount);

   // Restart the server and the Queue should be empty
   // if the message was non-durable
   server.stop();
   server.start();

   // Reconnect now
   connection = addConnection(client.connect());
   session = connection.createSession();
   AmqpReceiver receiver = session.createReceiver(getQueueName());

   final Queue queueView2 = getProxyToQueue(getQueueName());
   if (durable) {
      Wait.assertTrue("Message should not have returned", () -> queueView2.getMessageCount() == 1);
   } else {
      Wait.assertTrue("Message should have been restored", () -> queueView2.getMessageCount() == 0);
   }

   receiver.flow(1);
   message = receiver.receive(1, TimeUnit.SECONDS);

   if (durable) {
      assertNotNull("Should have read a message", message);
   } else {
      assertNull("Should not have read a message", message);
   }

   connection.close();
}
 
Example 16
Source File: AMQPMessageTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testPartialDecodeIgnoresApplicationPropertiesByDefault() {
   Header header = new Header();
   header.setDurable(true);
   header.setPriority(UnsignedByte.valueOf((byte) 6));

   ByteBuf encodedBytes = Unpooled.buffer(1024);
   NettyWritable writable = new NettyWritable(encodedBytes);

   EncoderImpl encoder = TLSEncode.getEncoder();
   encoder.setByteBuffer(writable);
   encoder.writeObject(header);

   // Signal body of AmqpValue but write corrupt underlying type info
   encodedBytes.writeByte(EncodingCodes.DESCRIBED_TYPE_INDICATOR);
   encodedBytes.writeByte(EncodingCodes.SMALLULONG);
   encodedBytes.writeByte(APPLICATION_PROPERTIES_DESCRIPTOR.byteValue());
   encodedBytes.writeByte(EncodingCodes.MAP8);
   encodedBytes.writeByte(2);  // Size
   encodedBytes.writeByte(2);  // Elements
   // Use bad encoding code on underlying type of map key which will fail the decode if run
   encodedBytes.writeByte(255);

   ReadableBuffer readable = new NettyReadable(encodedBytes);

   AMQPStandardMessage message = null;
   try {
      message = new AMQPStandardMessage(0, readable, null, null);
   } catch (Exception decodeError) {
      fail("Should not have encountered an exception on partial decode: " + decodeError.getMessage());
   }

   assertTrue(message.isDurable());

   try {
      // This should perform the lazy decode of the ApplicationProperties portion of the message
      message.getStringProperty("test");
      fail("Should have thrown an error when attempting to decode the ApplicationProperties which are malformed.");
   } catch (Exception ex) {
      // Expected decode to fail when building full message.
   }
}