Java Code Examples for org.apache.qpid.proton.message.Message#setTtl()

The following examples show how to use org.apache.qpid.proton.message.Message#setTtl() . 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: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnSendClearsTtlOnMessageReceivedWithTtl() throws Exception {
    Message message = Proton.message();
    int origTtl = 5;
    message.setTtl(origTtl);

    AmqpJmsMessageFacade amqpMessageFacade = createReceivedMessageFacade(createMockAmqpConsumer(), message);

    assertEquals("TTL has been unset already", origTtl, message.getTtl());

    amqpMessageFacade.onSend(0);

    // check value on underlying TTL field is NOT set
    assertEquals("TTL has not been cleared", 0, amqpMessageFacade.getAmqpHeader().getTimeToLive());
    assertNull("Underlying Header should be null, no values set to non-defaults", amqpMessageFacade.getHeader());
}
 
Example 2
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnSendOverridesTtlOnMessageReceivedWithTtl() throws Exception {
    Message message = Proton.message();
    int origTtl = 5;
    int newTtl = 10;
    message.setTtl(origTtl);

    AmqpJmsMessageFacade amqpMessageFacade = createReceivedMessageFacade(createMockAmqpConsumer(), message);

    assertEquals("TTL has been unset already", origTtl, message.getTtl());

    amqpMessageFacade.onSend(newTtl);

    // check value on underlying TTL field is NOT set
    assertEquals("TTL has not been overriden", newTtl, amqpMessageFacade.getAmqpHeader().getTimeToLive());
    assertEquals("TTL field on underlying message should be set", UnsignedInteger.valueOf(newTtl), amqpMessageFacade.getHeader().getTtl());
}
 
Example 3
Source File: MessageHelperTest.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Verifies that a TTL set on a message is preserved if it does not exceed the
 * <em>max-ttl</em> specified for a tenant.
 */
@Test
public void testAddPropertiesDoesNotOverrideValidMessageTtl() {

    final Message message = ProtonHelper.message();
    message.setTtl(10000L);
    final ResourceIdentifier target = ResourceIdentifier.from(EventConstants.EVENT_ENDPOINT, Constants.DEFAULT_TENANT, "4711");
    final JsonObject defaults = new JsonObject()
            .put(MessageHelper.SYS_HEADER_PROPERTY_TTL, 30);

    MessageHelper.addDefaults(message, target, defaults, 45L);

    assertThat(message.getTtl()).isEqualTo(10000L);
}
 
Example 4
Source File: AmqpMessageTest.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
@Test
public void testMessageAttributes() {
    Map<String, Object> props = new LinkedHashMap<>();
    props.put("hello", "world");
    props.put("some", "content");
    Message message = message();
    message.setTtl(1);
    message.setDurable(true);
    message.setReplyTo("reply");
    ApplicationProperties apps = new ApplicationProperties(props);
    message.setApplicationProperties(apps);
    message.setContentType("text/plain");
    message.setCorrelationId("1234");
    message.setDeliveryCount(2);
    message.setExpiryTime(10000);
    message.setFooter(new Footer(props));
    message.setGroupId("some-group");
    message.setAddress("address");
    message.setCreationTime(System.currentTimeMillis());
    message.setSubject("subject");
    message.setUserId("username".getBytes());
    message.setPriority((short) 2);
    message.setBody(new AmqpValue("hello"));
    message.setMessageId("4321");

    AmqpMessage<?> msg = new AmqpMessage<>(new AmqpMessageImpl(message), null, null);
    assertThat(msg.getAddress()).isEqualTo("address");
    assertThat(msg.getApplicationProperties()).contains(entry("hello", "world"), entry("some", "content"));
    assertThat(msg.getContentType()).isEqualTo("text/plain");
    assertThat(msg.getCreationTime()).isNotZero();
    assertThat(msg.getDeliveryCount()).isEqualTo(2);
    assertThat(msg.getExpiryTime()).isEqualTo(10000);
    assertThat(msg.getGroupId()).isEqualTo("some-group");
    assertThat(msg.getTtl()).isEqualTo(1);
    assertThat(msg.getSubject()).isEqualTo("subject");
    assertThat(msg.getPriority()).isEqualTo((short) 2);
    assertThat(((AmqpValue) msg.getBody()).getValue()).isEqualTo("hello");
    assertThat(msg.getCorrelationId()).isEqualTo("1234");
    assertThat(msg.getMessageId()).isEqualTo("4321");
    assertThat(msg.getHeader()).isNotNull();
    assertThat(msg.isDurable()).isTrue();
    assertThat(msg.getError().name()).isEqualTo("OK");
    assertThat(msg.getGroupSequence()).isZero();

}
 
Example 5
Source File: MessageHelper.java    From hono with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Sets the <em>time-to-live</em> for the given AMQP 1.0 message.
 *
 * @param message The message whose <em>time-to-live</em> is to be set.
 * @param timeToLive The <em>time-to-live</em> duration to be set on the message.
 * @throws NullPointerException if any of the parameters is {@code null}.
 */
public static void setTimeToLive(final Message message, final Duration timeToLive) {
    Objects.requireNonNull(message);
    Objects.requireNonNull(timeToLive);

    message.setTtl(timeToLive.toMillis());
}