Java源码示例:org.apache.qpid.proton.amqp.messaging.Header
示例1
public IncomingAmqpMetadata(String address, JsonObject properties, String contentType,
String contentEncoding, String correlationId, long creationTime, int deliveryCount, long expirationTime,
String groupId, long groupSequence, String id, boolean durable, boolean firstAcquirer, int priority,
String subject, long ttl, Header header) {
this.address = address;
this.properties = properties;
this.contentType = contentType;
this.contentEncoding = contentEncoding;
this.correlationId = correlationId;
this.creationTime = creationTime;
this.deliveryCount = deliveryCount;
this.expirationTime = expirationTime;
this.groupId = groupId;
this.groupSequence = groupSequence;
this.id = id;
this.durable = durable;
this.firstAcquirer = firstAcquirer;
this.priority = priority;
this.subject = subject;
this.ttl = ttl;
this.header = header;
}
示例2
@Test
public void testEncodeHeaderWithAllSetAndDecode() {
AmqpHeader header = new AmqpHeader();
header.setDurable(true);
header.setDeliveryCount(43);
header.setFirstAcquirer(true);
header.setPriority(9);
header.setTimeToLive(32768);
ByteBuf encoded = AmqpCodec.encode(header.getHeader());
AmqpHeader decoded = new AmqpHeader((Header) AmqpCodec.decode(encoded));
assertTrue(decoded.isDurable());
assertTrue(decoded.isFirstAcquirer());
assertEquals(43, decoded.getDeliveryCount());
assertEquals(9, decoded.getPriority());
assertEquals(32768, decoded.getTimeToLive());
}
示例3
@Test
public void testSetHeaderWithFirstAcquirerHeader() {
AmqpHeader header = new AmqpHeader();
Header protonHeader = new Header();
protonHeader.setFirstAcquirer(true);
header.setHeader(protonHeader);
assertFalse(header.isDefault());
assertFalse(header.nonDefaultDurable());
assertFalse(header.nonDefaultPriority());
assertFalse(header.nonDefaultTimeToLive());
assertTrue(header.nonDefaultFirstAcquirer());
assertFalse(header.nonDefaultDeliveryCount());
assertEquals(false, header.isDurable());
assertEquals(true, header.isFirstAcquirer());
assertEquals(4, header.getPriority());
assertEquals(0, header.getTimeToLive());
assertEquals(0, header.getDeliveryCount());
}
示例4
@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());
}
示例5
@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());
}
示例6
private void writeElement(Header header, int index) {
switch (index) {
case 0:
getEncoder().writeBoolean(header.getDurable());
break;
case 1:
getEncoder().writeUnsignedByte(header.getPriority());
break;
case 2:
getEncoder().writeUnsignedInteger(header.getTtl());
break;
case 3:
getEncoder().writeBoolean(header.getFirstAcquirer());
break;
case 4:
getEncoder().writeUnsignedInteger(header.getDeliveryCount());
break;
default:
throw new IllegalArgumentException("Unknown Header value index: " + index);
}
}
示例7
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;
}
示例8
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);
}
示例9
@Test
public void testSetHeaderWithNull() {
AmqpHeader header = new AmqpHeader();
header.setHeader((Header) null);
assertTrue(header.isDefault());
assertFalse(header.nonDefaultDurable());
assertFalse(header.nonDefaultPriority());
assertFalse(header.nonDefaultTimeToLive());
assertFalse(header.nonDefaultFirstAcquirer());
assertFalse(header.nonDefaultDeliveryCount());
assertEquals(false, header.isDurable());
assertEquals(false, header.isFirstAcquirer());
assertEquals(4, header.getPriority());
assertEquals(0, header.getTimeToLive());
assertEquals(0, header.getDeliveryCount());
}
示例10
private boolean checkMessageProperties(AMQPMessage message, Map<String, Object> expectedProperties) {
assertNotNull(message);
assertNotNull(server.getNodeID());
assertNotNull(message.getConnectionID());
assertEquals(message.getAddress(), expectedProperties.get(ADDRESS));
assertEquals(message.isDurable(), expectedProperties.get(DURABLE));
Properties props = message.getProperties();
assertEquals(props.getCorrelationId(), expectedProperties.get(CORRELATION_ID));
assertEquals(props.getReplyTo(), expectedProperties.get(REPLY_TO));
assertEquals(props.getMessageId(), expectedProperties.get(MESSAGE_ID));
Header header = message.getHeader();
assertEquals(header.getDurable(), expectedProperties.get(DURABLE));
assertEquals(header.getTtl().toString(), expectedProperties.get(TIME_TO_LIVE).toString());
assertEquals(header.getPriority().toString(), expectedProperties.get(PRIORITY).toString());
return true;
}
示例11
@Test
public void testNonDurableMessageReencodedToDurable() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertFalse(decoded.isDurable());
// Underlying message data not updated yet
assertNull(decoded.getHeader().getDurable());
decoded.setDurable(true);
decoded.reencode();
assertTrue(decoded.isDurable());
// Underlying message data now updated
assertTrue(decoded.getHeader().getDurable());
}
示例12
@Test
public void testSetHeaderWithTimeToLiveHeader() {
AmqpHeader header = new AmqpHeader();
Header protonHeader = new Header();
protonHeader.setTtl(UnsignedInteger.valueOf(9));
header.setHeader(protonHeader);
assertFalse(header.isDefault());
assertFalse(header.nonDefaultDurable());
assertFalse(header.nonDefaultPriority());
assertTrue(header.nonDefaultTimeToLive());
assertFalse(header.nonDefaultFirstAcquirer());
assertFalse(header.nonDefaultDeliveryCount());
assertEquals(false, header.isDurable());
assertEquals(false, header.isFirstAcquirer());
assertEquals(4, header.getPriority());
assertEquals(9, header.getTimeToLive());
assertEquals(0, header.getDeliveryCount());
}
示例13
@Test
public void testSetHeaderWithPriorityHeader() {
AmqpHeader header = new AmqpHeader();
Header protonHeader = new Header();
protonHeader.setPriority(UnsignedByte.valueOf((byte) 9));
header.setHeader(protonHeader);
assertFalse(header.isDefault());
assertFalse(header.nonDefaultDurable());
assertTrue(header.nonDefaultPriority());
assertFalse(header.nonDefaultTimeToLive());
assertFalse(header.nonDefaultFirstAcquirer());
assertFalse(header.nonDefaultDeliveryCount());
assertEquals(false, header.isDurable());
assertEquals(false, header.isFirstAcquirer());
assertEquals(9, header.getPriority());
assertEquals(0, header.getTimeToLive());
assertEquals(0, header.getDeliveryCount());
}
示例14
@Test
public void testSetExpirationToClearUpdateHeader() {
final long ttl = 100000;
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
protonMessage.setTtl(ttl);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertTrue(decoded.getExpiration() > System.currentTimeMillis());
decoded.setExpiration(-1);
decoded.reencode();
assertEquals(0, decoded.getExpiration());
assertNull(decoded.getHeader().getTtl());
}
示例15
@Test
public void testGetHeader() {
MessageImpl protonMessage = createProtonMessage();
AMQPStandardMessage message = new AMQPStandardMessage(0, encodeMessage(protonMessage), null, null);
Header decoded = message.getHeader();
assertNotSame(decoded, protonMessage.getHeader());
assertHeaderEquals(protonMessage.getHeader(), decoded);
// Update the values
decoded.setDeliveryCount(UnsignedInteger.ZERO);
decoded.setTtl(UnsignedInteger.valueOf(255));
decoded.setFirstAcquirer(true);
// Check that the message is unaffected.
assertHeaderNotEquals(protonMessage.getHeader(), decoded);
}
示例16
@Test
public void testSetHeaderWithDeliveryCountHeader() {
AmqpHeader header = new AmqpHeader();
Header protonHeader = new Header();
protonHeader.setDeliveryCount(UnsignedInteger.valueOf(9));
header.setHeader(protonHeader);
assertFalse(header.isDefault());
assertFalse(header.nonDefaultDurable());
assertFalse(header.nonDefaultPriority());
assertFalse(header.nonDefaultTimeToLive());
assertFalse(header.nonDefaultFirstAcquirer());
assertTrue(header.nonDefaultDeliveryCount());
assertEquals(false, header.isDurable());
assertEquals(false, header.isFirstAcquirer());
assertEquals(4, header.getPriority());
assertEquals(0, header.getTimeToLive());
assertEquals(9, header.getDeliveryCount());
}
示例17
private boolean isEquals(Header left, Header right) {
if (left == null && right == null) {
return true;
}
if (!isNullnessEquals(left, right)) {
return false;
}
try {
assertEquals(left.getDurable(), right.getDurable());
assertEquals(left.getDeliveryCount(), right.getDeliveryCount());
assertEquals(left.getFirstAcquirer(), right.getFirstAcquirer());
assertEquals(left.getPriority(), right.getPriority());
assertEquals(left.getTtl(), right.getTtl());
} catch (Throwable e) {
return false;
}
return true;
}
示例18
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;
}
示例19
@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);
}
示例20
@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());;
}
示例21
@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());;
}
示例22
@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());;
}
示例23
/**
* 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());
}
示例24
@Test
public void testEncodeEmptyHeaderAndDecode() {
Header empty = new Header();
ByteBuf encoded = AmqpCodec.encode(empty);
Header decoded = (Header) AmqpCodec.decode(encoded);
assertNotNull(decoded);
AmqpHeader header = new AmqpHeader(decoded);
assertFalse(header.isDurable());
assertEquals(4, header.getPriority());
assertEquals(0, header.getTimeToLive());
assertFalse(header.isFirstAcquirer());
assertEquals(0, header.getDeliveryCount());
}
示例25
public static Message message(Header header,
DeliveryAnnotations deliveryAnnotations, MessageAnnotations messageAnnotations,
Properties properties, ApplicationProperties applicationProperties,
Section body, Footer footer)
{
return Message.Factory.create(header, deliveryAnnotations,
messageAnnotations, properties,
applicationProperties, body, footer);
}
示例26
@Test
public void testEncodeHeaderWithDeliveryCountAndDecode() {
AmqpHeader header = new AmqpHeader();
header.setDeliveryCount(1);
ByteBuf encoded = AmqpCodec.encode(header.getHeader());
AmqpHeader decoded = new AmqpHeader((Header) AmqpCodec.decode(encoded));
assertFalse(decoded.isDurable());
assertEquals(4, decoded.getPriority());
assertEquals(0, decoded.getTimeToLive());
assertFalse(decoded.isFirstAcquirer());
assertEquals(1, decoded.getDeliveryCount());
}
示例27
private int getElementCount(Header header) {
if (header.getDeliveryCount() != null) {
return 5;
} else if (header.getFirstAcquirer() != null) {
return 4;
} else if (header.getTtl() != null) {
return 3;
} else if (header.getPriority() != null) {
return 2;
} else if (header.getDurable() != null) {
return 1;
} else {
return 0;
}
}
示例28
private byte deduceEncodingCode(Header value, int elementCount) {
if (elementCount == 0) {
return EncodingCodes.LIST0;
} else {
return EncodingCodes.LIST8;
}
}
示例29
@Test
public void testGetHeaderWhenDurableIsNotDefault() {
AmqpHeader amqpHeader = new AmqpHeader();
amqpHeader.setDurable(true);
Header header = amqpHeader.getHeader();
assertNotNull("Header should have been created as values are not all defaulted", header);
assertTrue(header.getDurable());
}
示例30
@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());
}
}