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

The following examples show how to use org.apache.qpid.proton.message.Message#setHeader() . 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 testGetTtlSynthesizedExpirationOnReceivedMessageWithTtlButNoAbsoluteExpiration() throws JMSException {
    Long ttl = 123L;

    Message message = Proton.message();
    Header header = new Header();
    header.setTtl(UnsignedInteger.valueOf(ttl));
    message.setHeader(header);

    long start = System.currentTimeMillis();
    AmqpJmsMessageFacade amqpMessageFacade = createReceivedMessageFacade(createMockAmqpConsumer(), message);
    long end = System.currentTimeMillis();

    long expiration = amqpMessageFacade.getExpiration();

    assertTrue("Should have sythesized expiration based on current time + ttl", start + ttl <= expiration);
    assertTrue("Should have sythesized expiration based on current time + ttl", expiration <= end + ttl);

    long expiration2 = amqpMessageFacade.getExpiration();
    assertEquals("Second retrieval should return same result", expiration, expiration2);

    amqpMessageFacade = amqpMessageFacade.copy();
    long expiration3 = amqpMessageFacade.getExpiration();
    assertEquals("Thrid retrieval from copy should return same result", expiration, expiration3);
}
 
Example 2
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDeliveryCountForReceivedMessageWithHeaderWithDeliveryCount() {
    Message message = Proton.message();
    Header header = new Header();
    header.setDeliveryCount(new UnsignedInteger(1));
    message.setHeader(header);

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

    // JMS delivery count starts at one.
    assertEquals("expected delivery count value not found", 2, amqpMessageFacade.getDeliveryCount());

    // Redelivered state inferred from delivery count
    assertTrue(amqpMessageFacade.isRedelivered());
    assertEquals(1, amqpMessageFacade.getRedeliveryCount());;
}
 
Example 3
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetRedeliveredWhenAlreadyRedeliveredDoesNotChangeDeliveryCount() {
    Message message = Proton.message();
    Header header = new Header();
    header.setDeliveryCount(new UnsignedInteger(1));
    message.setHeader(header);

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

    // Redelivered state inferred from delivery count
    assertTrue(amqpMessageFacade.isRedelivered());
    assertEquals(1, amqpMessageFacade.getRedeliveryCount());;

    amqpMessageFacade.setRedelivered(true);
    assertTrue(amqpMessageFacade.isRedelivered());
    assertEquals(1, amqpMessageFacade.getRedeliveryCount());;
}
 
Example 4
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetRedeliveredFalseClearsDeliveryCount() {
    Message message = Proton.message();
    Header header = new Header();
    header.setDeliveryCount(new UnsignedInteger(1));
    message.setHeader(header);

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

    // Redelivered state inferred from delivery count
    assertTrue(amqpMessageFacade.isRedelivered());
    assertEquals(1, amqpMessageFacade.getRedeliveryCount());;

    amqpMessageFacade.setRedelivered(false);
    assertFalse(amqpMessageFacade.isRedelivered());
    assertEquals(0, amqpMessageFacade.getRedeliveryCount());;
}
 
Example 5
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
/**
 * Receive message which has a header section with a priority value. Ensure the headers
 * underlying field value is cleared when the priority is set to the default priority of 4.
 */
@Test
public void testSetPriorityToDefaultOnReceivedMessageWithPriorityClearsPriorityField() {
    byte priority = 11;

    Message message = Proton.message();
    Header header = new Header();
    message.setHeader(header);
    header.setPriority(UnsignedByte.valueOf(priority));

    AmqpJmsMessageFacade amqpMessageFacade = createReceivedMessageFacade(createMockAmqpConsumer(), message);
    amqpMessageFacade.setPriority(Message.DEFAULT_PRIORITY);

    //check the expected value is still returned
    assertEquals("expected priority value not returned", Message.DEFAULT_PRIORITY, amqpMessageFacade.getPriority());

    //check the underlying header field was actually cleared rather than set to Message.DEFAULT_PRIORITY
    assertNull("underlying header priority field was not cleared", amqpMessageFacade.getHeader());
}
 
Example 6
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetDeliveryCountForReceivedMessageWithHeaderButNoDeliveryCount() {
    Message message = Proton.message();
    AmqpJmsMessageFacade amqpMessageFacade = createReceivedMessageFacade(createMockAmqpConsumer(), message);

    Header header = new Header();
    message.setHeader(header);

    // JMS delivery count starts at one.
    assertEquals("expected delivery count value not found", 1, amqpMessageFacade.getDeliveryCount());

    // Redelivered state inferred from delivery count
    assertFalse(amqpMessageFacade.isRedelivered());
    assertEquals(0, amqpMessageFacade.getRedeliveryCount());;
}
 
Example 7
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
/**
 * When messages have a header section, but lack the priority field,
 * the AMQP spec says the priority has default value of 4.
 */
@Test
public void testGetPriorityIs4ForReceivedMessageWithHeaderButWithoutPriority() {
    Message message = Proton.message();

    Header header = new Header();
    message.setHeader(header);

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

    assertEquals("expected priority value not found", 4, amqpMessageFacade.getPriority());
}
 
Example 8
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
/**
 * When messages have a header section, which have a priority value, ensure it is returned.
 */
@Test
public void testGetPriorityForReceivedMessageWithHeaderWithPriority() {
    // value over 10 deliberately
    byte priority = 7;

    Message message = Proton.message();
    Header header = new Header();
    message.setHeader(header);
    header.setPriority(UnsignedByte.valueOf(priority));

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

    assertEquals("expected priority value not found", priority, amqpMessageFacade.getPriority());
}
 
Example 9
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
/**
 * When messages have a header section, which has a priority value just above the
 * JMS range of 0-9, ensure it is constrained to 9.
 */
@Test
public void testGetPriorityForReceivedMessageWithPriorityJustAboveJmsRange() {
    // value just over 9 deliberately
    byte priority = 11;

    Message message = Proton.message();
    Header header = new Header();
    message.setHeader(header);
    header.setPriority(UnsignedByte.valueOf(priority));

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

    assertEquals("expected priority value not found", 9, amqpMessageFacade.getPriority());
}
 
Example 10
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
/**
 * When messages have a header section, which has a priority value above the
 * JMS range of 0-9 and also outside the signed byte range (given AMQP
 * allowing ubyte priority), ensure it is constrained to 9.
 */
@Test
public void testGetPriorityForReceivedMessageWithPriorityAboveSignedByteRange() {
    String priorityString = String.valueOf(255);

    Message message = Proton.message();
    Header header = new Header();
    message.setHeader(header);
    header.setPriority(UnsignedByte.valueOf(priorityString));

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

    assertEquals("expected priority value not found", 9, amqpMessageFacade.getPriority());
}
 
Example 11
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();
}