Search in sources :

Example 1 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class RMQSession method createProducer.

/**
 * {@inheritDoc}
 * <p>
 * <b>Note</b>: The destination may be null.
 * </p>
 */
@Override
public MessageProducer createProducer(Destination destination) throws JMSException {
    logger.trace("create producer for destination '{}' on session '{}'", destination, this);
    illegalStateExceptionIfClosed();
    RMQDestination dest = (RMQDestination) destination;
    declareDestinationIfNecessary(dest);
    RMQMessageProducer producer = new RMQMessageProducer(this, dest, this.preferProducerMessageProperty, this.amqpPropertiesCustomiser);
    this.producers.add(producer);
    return producer;
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination)

Example 2 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class SimpleAmqpQueueMessageIT method testSendFromAmqpAndReceiveTextMessage.

@Test
public void testSendFromAmqpAndReceiveTextMessage() throws Exception {
    channel.queueDeclare(QUEUE_NAME_NON_EXCLUSIVE, // durable
    false, // non-exclusive
    false, // autoDelete
    true, // options
    null);
    Map<String, Object> hdrs = new HashMap<String, Object>();
    hdrs.put(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
    // To signal a JMS TextMessage
    hdrs.put("JMSType", "TextMessage");
    hdrs.put("JMSPriority", 21);
    hdrs.put("JMSDeliveryMode", 2);
    hdrs.put("DummyProp", 42);
    hdrs.put("rmq.jms.silly", "silly attempt");
    AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().deliveryMode(1).priority(6).headers(hdrs).build();
    channel.basicPublish("", QUEUE_NAME_NON_EXCLUSIVE, props, MESSAGE.getBytes("UTF-8"));
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    // read-only AMQP-mapped queue
    Queue queue = (Queue) new RMQDestination(QUEUE_NAME_NON_EXCLUSIVE, null, null, QUEUE_NAME_NON_EXCLUSIVE);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    TextMessage message = (TextMessage) queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
    assertNotNull(message, "No message received");
    assertEquals(MESSAGE, message.getText(), "Payload doesn't match");
    // override should work
    assertEquals(21, message.getJMSPriority(), "Priority incorrect");
    // override should fail
    assertEquals(1, message.getJMSDeliveryMode(), "Delivery mode incorrect");
    // override should work
    assertEquals("TextMessage", message.getJMSType(), "JMSType not set correctly");
    Enumeration<?> propNames = message.getPropertyNames();
    Set<String> propNameSet = new HashSet<String>();
    while (propNames.hasMoreElements()) {
        propNameSet.add((String) propNames.nextElement());
    }
    assertEquals(new HashSet<>(Arrays.asList(USER_STRING_PROPERTY_NAME, "DummyProp")), propNameSet, "Headers not set correctly");
    assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
    assertEquals("42", message.getStringProperty("DummyProp"), "Numeric property not transferred");
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) HashMap(java.util.HashMap) AMQP(com.rabbitmq.client.AMQP) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class SimpleAmqpQueueMessageIT method testSendToAmqpAndReceiveTextMessage.

@Test
public void testSendToAmqpAndReceiveTextMessage() throws Exception {
    channel.queueDeclare(QUEUE_NAME, // durable
    false, // exclusive
    true, // autoDelete
    true, // options
    null);
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    // write-only AMQP-mapped queue
    Queue queue = (Queue) new RMQDestination(QUEUE_NAME, "", QUEUE_NAME, null);
    QueueSender queueSender = queueSession.createSender(queue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    queueSender.setPriority(9);
    TextMessage message = queueSession.createTextMessage(MESSAGE);
    message.setStringProperty(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
    queueSender.send(message);
    queueConn.close();
    GetResponse response = channel.basicGet(QUEUE_NAME, false);
    assertNotNull(response, "basicGet failed to retrieve a response");
    byte[] body = response.getBody();
    assertNotNull(body, "body of response is null");
    {
        Map<String, Object> hdrs = response.getProps().getHeaders();
        assertEquals(new HashSet<>(Arrays.asList("JMSMessageID", "JMSDeliveryMode", "JMSPriority", "JMSTimestamp", USER_STRING_PROPERTY_NAME)), hdrs.keySet(), "Some keys missing");
        assertEquals(9, hdrs.get("JMSPriority"), "Priority wrong");
        // toString is a bit wiffy
        assertEquals("NON_PERSISTENT", hdrs.get("JMSDeliveryMode").toString(), "Delivery mode wrong");
        assertEquals(STRING_PROP_VALUE, hdrs.get(USER_STRING_PROPERTY_NAME).toString(), "String property wrong");
    }
    assertEquals(MESSAGE, new String(body, "UTF-8"), "Message received not identical to message sent");
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) QueueSender(javax.jms.QueueSender) Queue(javax.jms.Queue) GetResponse(com.rabbitmq.client.GetResponse) HashMap(java.util.HashMap) Map(java.util.Map) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class SimpleAmqpQueueMessageIT method testSendToAmqpAndReceiveBytesMessage.

@Test
public void testSendToAmqpAndReceiveBytesMessage() throws Exception {
    channel.queueDeclare(QUEUE_NAME, // durable
    false, // exclusive
    true, // autoDelete
    true, // options
    null);
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    // write-only AMQP-mapped queue
    Queue queue = (Queue) new RMQDestination(QUEUE_NAME, "", QUEUE_NAME, null);
    QueueSender queueSender = queueSession.createSender(queue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    queueSender.setPriority(9);
    BytesMessage message = queueSession.createBytesMessage();
    message.setStringProperty(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
    message.writeBytes(BYTE_ARRAY);
    queueSender.send(message);
    queueConn.close();
    GetResponse response = channel.basicGet(QUEUE_NAME, false);
    assertNotNull(response, "basicGet failed to retrieve a response");
    byte[] body = response.getBody();
    assertNotNull(body, "body of response is null");
    {
        Map<String, Object> hdrs = response.getProps().getHeaders();
        assertEquals(new HashSet<String>(Arrays.asList("JMSMessageID", "JMSDeliveryMode", "JMSPriority", "JMSTimestamp", USER_STRING_PROPERTY_NAME)), hdrs.keySet(), "Some keys missing");
        assertEquals(9, hdrs.get("JMSPriority"), "Priority wrong");
        // toString is a bit wiffy
        assertEquals("NON_PERSISTENT", hdrs.get("JMSDeliveryMode").toString(), "Delivery mode wrong");
        assertEquals(STRING_PROP_VALUE, hdrs.get(USER_STRING_PROPERTY_NAME).toString(), "String property wrong");
    }
    assertArrayEquals(BYTE_ARRAY, body, "Message received not identical to message sent");
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) QueueSender(javax.jms.QueueSender) BytesMessage(javax.jms.BytesMessage) Queue(javax.jms.Queue) GetResponse(com.rabbitmq.client.GetResponse) HashMap(java.util.HashMap) Map(java.util.Map) QueueSession(javax.jms.QueueSession) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with RMQDestination

use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.

the class SimpleAmqpQueueMessageIT method testSendFromAmqpAndReceiveBytesMessage.

@Test
public void testSendFromAmqpAndReceiveBytesMessage() throws Exception {
    channel.queueDeclare(QUEUE_NAME_NON_EXCLUSIVE, // durable
    false, // non-exclusive
    false, // autoDelete
    true, // options
    null);
    Map<String, Object> hdrs = new HashMap<String, Object>();
    hdrs.put(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
    hdrs.put("JMSType", USER_JMS_TYPE_SETTING);
    hdrs.put("JMSPriority", 21);
    hdrs.put("JMSDeliveryMode", 2);
    hdrs.put("DummyProp", 42);
    hdrs.put("rmq.jms.silly", "silly attempt");
    AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().deliveryMode(1).priority(6).headers(hdrs).build();
    channel.basicPublish("", QUEUE_NAME_NON_EXCLUSIVE, props, BYTE_ARRAY);
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    // read-only AMQP-mapped queue
    Queue queue = (Queue) new RMQDestination(QUEUE_NAME_NON_EXCLUSIVE, null, null, QUEUE_NAME_NON_EXCLUSIVE);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    BytesMessage message = (BytesMessage) queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
    assertNotNull(message, "No message received");
    byte[] bytes = new byte[BYTE_ARRAY.length + 2];
    int bytesIn = message.readBytes(bytes);
    assertEquals(BYTE_ARRAY.length, bytesIn, "Message payload not correct size");
    byte[] bytesTrunc = new byte[bytesIn];
    System.arraycopy(bytes, 0, bytesTrunc, 0, bytesIn);
    assertArrayEquals(BYTE_ARRAY, bytesTrunc, "Payload doesn't match");
    // override should work
    assertEquals(21, message.getJMSPriority(), "Priority incorrect");
    // override should fail
    assertEquals(1, message.getJMSDeliveryMode(), "Delivery mode incorrect");
    // override should work
    assertEquals(USER_JMS_TYPE_SETTING, message.getJMSType(), "JMSType not set correctly");
    Enumeration<?> propNames = message.getPropertyNames();
    Set<String> propNameSet = new HashSet<String>();
    while (propNames.hasMoreElements()) {
        propNameSet.add((String) propNames.nextElement());
    }
    assertEquals(new HashSet<>(Arrays.asList(USER_STRING_PROPERTY_NAME, "DummyProp")), propNameSet, "Headers not set correctly");
    assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
    assertEquals("42", message.getStringProperty("DummyProp"), "Numeric property not transferred");
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) HashMap(java.util.HashMap) BytesMessage(javax.jms.BytesMessage) AMQP(com.rabbitmq.client.AMQP) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

RMQDestination (com.rabbitmq.jms.admin.RMQDestination)9 Queue (javax.jms.Queue)5 QueueSession (javax.jms.QueueSession)5 Test (org.junit.jupiter.api.Test)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 GetResponse (com.rabbitmq.client.GetResponse)3 QueueSender (javax.jms.QueueSender)3 TextMessage (javax.jms.TextMessage)3 AMQP (com.rabbitmq.client.AMQP)2 Map (java.util.Map)2 BytesMessage (javax.jms.BytesMessage)2 QueueReceiver (javax.jms.QueueReceiver)2 RMQJMSException (com.rabbitmq.jms.util.RMQJMSException)1 JMSException (javax.jms.JMSException)1