Search in sources :

Example 16 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class StreamMessageSerializationIT method testReceiveStreamMessageWithValue.

protected void testReceiveStreamMessageWithValue(Object value) throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        drainQueue(queueSession, queue);
        QueueSender queueSender = queueSession.createSender(queue);
        StreamMessage message = (StreamMessage) MessageTestType.STREAM.gen(queueSession, null);
        // we simulate an attack from the sender by calling writeObject with a non-primitive value
        // (StreamMessage supports only primitive types)
        // the value is then sent to the destination and the consumer will have to
        // deserialize it and can potentially execute malicious code
        Method writeObjectMethod = RMQStreamMessage.class.getDeclaredMethod("writeObject", Object.class, boolean.class);
        writeObjectMethod.setAccessible(true);
        writeObjectMethod.invoke(message, value, true);
        queueSender.send(message);
    } finally {
        reconnect(TRUSTED_PACKAGES);
    }
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    RMQStreamMessage m = (RMQStreamMessage) queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
    MessageTestType.STREAM.check(m, null);
    assertEquals(m.readObject(), value);
}
Also used : RMQStreamMessage(com.rabbitmq.jms.client.message.RMQStreamMessage) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) RMQStreamMessage(com.rabbitmq.jms.client.message.RMQStreamMessage) StreamMessage(javax.jms.StreamMessage) Method(java.lang.reflect.Method) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession)

Example 17 with QueueReceiver

use of javax.jms.QueueReceiver 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 = 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");
    assertThat(message.getJMSTimestamp()).isZero();
}
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 18 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class SimpleQueueMessageIT method messageTestBase.

private void messageTestBase(MessageTestType mtt) throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        drainQueue(queueSession, queue);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        queueSender.send(mtt.gen(queueSession, (Serializable) queue));
    } finally {
        reconnect();
    }
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    mtt.check(queueReceiver.receive(TEST_RECEIVE_TIMEOUT), (Serializable) queue);
}
Also used : Serializable(java.io.Serializable) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession)

Example 19 with QueueReceiver

use of javax.jms.QueueReceiver in project qpid-broker-j by apache.

the class QueueReceiverTest method createReceiver.

@Test
public void createReceiver() throws Exception {
    Queue queue = createQueue(getTestName());
    QueueConnection queueConnection = getQueueConnection();
    try {
        queueConnection.start();
        Utils.sendMessages(queueConnection, queue, 3);
        QueueSession session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueReceiver receiver = session.createReceiver(queue, String.format("%s=2", INDEX));
        assertEquals("Queue names should match from QueueReceiver", queue.getQueueName(), receiver.getQueue().getQueueName());
        Message received = receiver.receive(getReceiveTimeout());
        assertNotNull("Message is not received", received);
        assertEquals("Unexpected message is received", 2, received.getIntProperty(INDEX));
    } finally {
        queueConnection.close();
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) Message(javax.jms.Message) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

Example 20 with QueueReceiver

use of javax.jms.QueueReceiver in project iaf by ibissource.

the class PullingIfsaProviderListener method openThread.

@Override
public Map openThread() throws ListenerException {
    Map threadContext = new HashMap();
    try {
        if (!isSessionsArePooled()) {
            QueueSession session = createSession();
            threadContext.put(THREAD_CONTEXT_SESSION_KEY, session);
            QueueReceiver receiver;
            receiver = getServiceReceiver(session);
            threadContext.put(THREAD_CONTEXT_RECEIVER_KEY, receiver);
        }
        return threadContext;
    } catch (IfsaException e) {
        throw new ListenerException(getLogPrefix() + "exception in openThread()", e);
    }
}
Also used : IfsaException(nl.nn.adapterframework.extensions.ifsa.IfsaException) ListenerException(nl.nn.adapterframework.core.ListenerException) HashMap(java.util.HashMap) QueueReceiver(javax.jms.QueueReceiver) HashMap(java.util.HashMap) Map(java.util.Map) QueueSession(javax.jms.QueueSession)

Aggregations

QueueReceiver (javax.jms.QueueReceiver)56 QueueSession (javax.jms.QueueSession)47 Queue (javax.jms.Queue)40 TextMessage (javax.jms.TextMessage)28 Message (javax.jms.Message)25 QueueSender (javax.jms.QueueSender)25 Test (org.junit.jupiter.api.Test)17 QueueConnection (javax.jms.QueueConnection)13 Serializable (java.io.Serializable)11 JMSException (javax.jms.JMSException)10 Test (org.junit.Test)10 QueueConnectionFactory (javax.jms.QueueConnectionFactory)6 BytesMessage (javax.jms.BytesMessage)5 InitialContext (javax.naming.InitialContext)5 Destination (javax.jms.Destination)4 MessageProducer (javax.jms.MessageProducer)4 NamingException (javax.naming.NamingException)4 RMQDestination (com.rabbitmq.jms.admin.RMQDestination)3 Enumeration (java.util.Enumeration)3 HashMap (java.util.HashMap)3