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

The following examples show how to use org.apache.qpid.proton.Proton#message() . 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: AmqpRawMessageConverter.java    From strimzi-kafka-bridge with Apache License 2.0 6 votes vote down vote up
@Override
public Message toMessage(String address, KafkaConsumerRecord<String, byte[]> record) {

    Message message = Proton.message();
    message.setAddress(address);

    message.decode(record.value(), 0, record.value().length);

    // put message annotations about partition, offset and key (if not null)
    Map<Symbol, Object> map = new HashMap<>();
    map.put(Symbol.valueOf(AmqpBridge.AMQP_PARTITION_ANNOTATION), record.partition());
    map.put(Symbol.valueOf(AmqpBridge.AMQP_OFFSET_ANNOTATION), record.offset());
    map.put(Symbol.valueOf(AmqpBridge.AMQP_KEY_ANNOTATION), record.key());
    map.put(Symbol.valueOf(AmqpBridge.AMQP_TOPIC_ANNOTATION), record.topic());

    MessageAnnotations messageAnnotations = new MessageAnnotations(map);
    message.setMessageAnnotations(messageAnnotations);

    return message;
}
 
Example 2
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetUserIdOnReceievedMessage() throws Exception {
    String userIdString = "testValue";
    byte[] bytes = userIdString.getBytes("UTF-8");

    Message message = Proton.message();

    Properties props = new Properties();
    props.setUserId(new Binary(bytes));
    message.setProperties(props);

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

    assertNotNull("Expected a userid on received message", amqpMessageFacade.getUserId());
    assertEquals("Incorrect messageId value received", userIdString, amqpMessageFacade.getUserId());
}
 
Example 3
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCorrelationIdBytesOnReceievedMessageWithBinaryId() throws Exception {
    Binary testCorrelationId = createBinaryId();
    byte[] bytes = testCorrelationId.getArray();

    Data payloadData = Data.Factory.create();
    PropertiesDescribedType props = new PropertiesDescribedType();
    props.setCorrelationId(new Binary(bytes));
    payloadData.putDescribedType(props);
    Binary b = payloadData.encode();

    System.out.println("Using encoded AMQP message payload: " + b);

    Message message = Proton.message();
    int decoded = message.decode(b.getArray(), b.getArrayOffset(), b.getLength());
    assertEquals(decoded, b.getLength());

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

    assertEquals("Unexpected correlationId value on underlying AMQP message", testCorrelationId, amqpMessageFacade.getProperties().getCorrelationId());
    assertArrayEquals("Expected correlationId bytes not returned", bytes, amqpMessageFacade.getCorrelationIdBytes());
}
 
Example 4
Source File: AmqpCodecTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
/**
 * Test that a message with the {@value AmqpMessageSupport#JMS_MSG_TYPE}
 * annotation set to  {@value AmqpMessageSupport#JMS_MESSAGE} is
 * treated as a generic {@link JmsMessage} with {@link AmqpJmsMessageFacade}
 *
 * @throws Exception if an error occurs during the test.
 */
@Test
public void testCreateGenericMessageFromMessageTypeAnnotation() throws Exception {
    Message message = Proton.message();

    Map<Symbol, Object> map = new HashMap<Symbol, Object>();
    map.put(AmqpMessageSupport.JMS_MSG_TYPE, AmqpMessageSupport.JMS_MESSAGE);

    MessageAnnotations messageAnnotations = new MessageAnnotations(map);
    message.setMessageAnnotations(messageAnnotations);

    JmsMessage jmsMessage = AmqpCodec.decodeMessage(mockConsumer, encodeMessage(message)).asJmsMessage();
    assertNotNull("Message should not be null", jmsMessage);
    assertEquals("Unexpected message class type", JmsMessage.class, jmsMessage.getClass());

    JmsMessageFacade facade = jmsMessage.getFacade();
    assertNotNull("Facade should not be null", facade);
    assertEquals("Unexpected facade class type", AmqpJmsMessageFacade.class, facade.getClass());
}
 
Example 5
Source File: AmqpCodecTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
/**
 * Test that an amqp-sequence body containing a binary value results in an ObjectMessage
 * when not otherwise annotated to indicate the type of JMS message it is.
 *
 * @throws Exception if an error occurs during the test.
 */
@Test
public void testCreateObjectMessageMessageFromAmqpSequence() throws Exception {
    Message message = Proton.message();
    List<String> list = new ArrayList<String>();
    message.setBody(new AmqpSequence(list));

    JmsMessage jmsMessage = AmqpCodec.decodeMessage(mockConsumer, encodeMessage(message)).asJmsMessage();
    assertNotNull("Message should not be null", jmsMessage);
    assertEquals("Unexpected message class type", JmsObjectMessage.class, jmsMessage.getClass());

    JmsMessageFacade facade = jmsMessage.getFacade();
    assertNotNull("Facade should not be null", facade);
    assertEquals("Unexpected facade class type", AmqpJmsObjectMessageFacade.class, facade.getClass());

    AmqpObjectTypeDelegate delegate = ((AmqpJmsObjectMessageFacade) facade).getDelegate();
    assertTrue("Unexpected delegate type: " + delegate, delegate instanceof AmqpTypedObjectDelegate);
}
 
Example 6
Source File: AmqpCodecTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
/**
 * Test that a message with the {@value AmqpMessageSupport#JMS_MSG_TYPE}
 * annotation set to  {@value AmqpMessageSupport#JMS_BYTES_MESSAGE} is
 * treated as a {@link JmsTextMessage} with {@link AmqpJmsTextMessageFacade}
 *
 * @throws Exception if an error occurs during the test.
 */
@Test
public void testCreateTextMessageFromMessageTypeAnnotation() throws Exception {
    Message message = Proton.message();

    Map<Symbol, Object> map = new HashMap<Symbol, Object>();
    map.put(AmqpMessageSupport.JMS_MSG_TYPE, AmqpMessageSupport.JMS_TEXT_MESSAGE);

    MessageAnnotations messageAnnotations = new MessageAnnotations(map);
    message.setMessageAnnotations(messageAnnotations);

    JmsMessage jmsMessage = AmqpCodec.decodeMessage(mockConsumer, encodeMessage(message)).asJmsMessage();
    assertNotNull("Message should not be null", jmsMessage);
    assertEquals("Unexpected message class type", JmsTextMessage.class, jmsMessage.getClass());

    JmsMessageFacade facade = jmsMessage.getFacade();
    assertNotNull("Facade should not be null", facade);
    assertEquals("Unexpected facade class type", AmqpJmsTextMessageFacade.class, facade.getClass());
}
 
Example 7
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 8
Source File: AmqpCodecTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
/**
 * Test that an amqp-value body containing a list results in an ObjectMessage
 * when not otherwise annotated to indicate the type of JMS message it is.
 *
 * @throws Exception if an error occurs during the test.
 */
@Test
public void testCreateAmqpObjectMessageFromAmqpValueWithList() throws Exception {
    Message message = Proton.message();
    List<String> list = new ArrayList<String>();
    message.setBody(new AmqpValue(list));

    JmsMessage jmsMessage = AmqpCodec.decodeMessage(mockConsumer, encodeMessage(message)).asJmsMessage();
    assertNotNull("Message should not be null", jmsMessage);
    assertEquals("Unexpected message class type", JmsObjectMessage.class, jmsMessage.getClass());

    JmsMessageFacade facade = jmsMessage.getFacade();
    assertNotNull("Facade should not be null", facade);
    assertEquals("Unexpected facade class type", AmqpJmsObjectMessageFacade.class, facade.getClass());

    AmqpObjectTypeDelegate delegate = ((AmqpJmsObjectMessageFacade) facade).getDelegate();
    assertTrue("Unexpected delegate type: " + delegate, delegate instanceof AmqpTypedObjectDelegate);
}
 
Example 9
Source File: PubSubBroker.java    From enmasse with Apache License 2.0 5 votes vote down vote up
public synchronized void sendMessages(String address, List<String> messages) {
    Queue<Message> queue = queues.computeIfAbsent(address, a -> new ArrayDeque<>());
    for (String data : messages) {
        Message message = Proton.message();
        message.setBody(new AmqpValue(data));
        queue.add(message);
    }
}
 
Example 10
Source File: MessageSubscriberWhiteboxVerificationTckTest.java    From vertx-proton with Apache License 2.0 5 votes vote down vote up
@Override
public Message createElement(int element) {
  Message m = Proton.message();
  m.setBody(new AmqpValue("hello" + element));

  return m;
}
 
Example 11
Source File: Recv.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onDelivery(Event event) {
    Receiver recv = (Receiver)event.getLink();
    Delivery delivery = recv.current();
    if (delivery.isReadable() && !delivery.isPartial()) {
        int size = delivery.pending();
        byte[] buffer = new byte[size];
        int read = recv.recv(buffer, 0, buffer.length);
        recv.advance();

        Message msg = Proton.message();
        msg.decode(buffer, 0, read);
        System.out.println(((AmqpValue)msg.getBody()).getValue());
    }
}
 
Example 12
Source File: AmqpCodecTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
/**
 * Test that a message with no body section, and no content-type results in a BytesMessage
 * when not otherwise annotated to indicate the type of JMS message it is.
 *
 * @throws Exception if an error occurs during the test.
 */
@Test
public void testCreateBytesMessageFromNoBodySectionAndNoContentType() throws Exception {
    Message message = Proton.message();

    assertNull(message.getContentType());

    JmsMessage jmsMessage = AmqpCodec.decodeMessage(mockConsumer, encodeMessage(message)).asJmsMessage();
    assertNotNull("Message should not be null", jmsMessage);
    assertEquals("Unexpected message class type", JmsBytesMessage.class, jmsMessage.getClass());

    JmsMessageFacade facade = jmsMessage.getFacade();
    assertNotNull("Facade should not be null", facade);
    assertEquals("Unexpected facade class type", AmqpJmsBytesMessageFacade.class, facade.getClass());
}
 
Example 13
Source File: DeliveryTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
private Delivery sendMessageToClient(String deliveryTag, String messageContent, Integer messageFormat)
{
    byte[] tag = deliveryTag.getBytes(StandardCharsets.UTF_8);

    Message m = Proton.message();
    m.setBody(new AmqpValue(messageContent));

    final int BUFFER_SIZE = messageContent.length() * 4;
    byte[] encoded = new byte[BUFFER_SIZE];
    int len = m.encode(encoded, 0, BUFFER_SIZE);

    assertTrue("given array was too small", len <= BUFFER_SIZE);

    Delivery serverDelivery = getServer().sender.delivery(tag);

    // Verify the default format of 0 is in place
    assertEquals("Unexpected message format", 0, serverDelivery.getMessageFormat());

    // Set the message format explicitly if given, or leave it at the default
    if(messageFormat != null) {
        serverDelivery.setMessageFormat(messageFormat);
    }

    int sent = getServer().sender.send(encoded, 0, len);

    assertEquals("sender unable to send all data at once as assumed for simplicity", len, sent);

    boolean senderAdvanced = getServer().sender.advance();
    assertTrue("sender has not advanced", senderAdvanced);

    return serverDelivery;
}
 
Example 14
Source File: AmqpJmsMessageFacadeTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test
public void testClearAllMessageAnnotationsUsingNewMessage() {
    Message message = Proton.message();
    AmqpJmsMessageFacade amqpMessageFacade = createReceivedMessageFacade(createMockAmqpConsumer(), message);

    amqpMessageFacade.clearMessageAnnotations();

    assertNull(amqpMessageFacade.getMessageAnnotations());
}
 
Example 15
Source File: AMQPMessageSupportTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetMessageAnnotationWhenMessageHasNoAnnotationsMap() {
   Message message = Proton.message();
   assertNull(AMQPMessageSupport.getMessageAnnotation("x-opt-test", message));
}
 
Example 16
Source File: TrackerSubscriberBlackboxVerificationTckTest.java    From vertx-proton with Apache License 2.0 4 votes vote down vote up
@Override
public Tracker createElement(int element) {
  Message m = Proton.message();
  m.setBody(new AmqpValue("hello" + element));
  return Tracker.create(m, null);
}
 
Example 17
Source File: AMQPMessageSupportTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testIsContentTypeWithNullStringValueAndNonNullMessageContentType() {
   Message message = Proton.message();
   message.setContentType("test");
   assertFalse(AMQPMessageSupport.isContentType(null, message));
}
 
Example 18
Source File: AMQPMessageSupportTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testIsContentTypeWithNonNullStringValueAndNullMessageContentType() {
   Message message = Proton.message();
   assertFalse(AMQPMessageSupport.isContentType("test", message));
}
 
Example 19
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 3 votes vote down vote up
private Delivery verifyDelivery(Receiver receiver, String deliveryTag, String messageContent)
{
    Delivery delivery = receiver.current();

    assertTrue(Arrays.equals(deliveryTag.getBytes(StandardCharsets.UTF_8), delivery.getTag()));

    assertNull(delivery.getLocalState());
    assertNull(delivery.getRemoteState());

    assertFalse(delivery.isPartial());
    assertTrue(delivery.isReadable());

    byte[] received = new byte[BUFFER_SIZE];
    int len = receiver.recv(received, 0, BUFFER_SIZE);

    assertTrue("given array was too small", len < BUFFER_SIZE);

    Message m = Proton.message();
    m.decode(received, 0, len);

    Object messageBody = ((AmqpValue)m.getBody()).getValue();
    assertEquals("Unexpected message content", messageContent, messageBody);

    boolean receiverAdvanced = receiver.advance();
    assertTrue("receiver has not advanced", receiverAdvanced);

    return delivery;
}
 
Example 20
Source File: ProtonHelper.java    From vertx-proton with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a bare Message object.
 *
 * @return the message
 */
public static Message message() {
  return Proton.message();
}