Search in sources :

Example 1 with RMQTextMessage

use of com.rabbitmq.jms.client.message.RMQTextMessage in project rabbitmq-jms-client by rabbitmq.

the class RMQMessageProducer method sendAMQPMessage.

private void sendAMQPMessage(RMQDestination destination, RMQMessage msg, int deliveryMode, int priority, long timeToLive) throws JMSException {
    if (!destination.amqpWritable()) {
        this.logger.error("Cannot write to AMQP destination {}", destination);
        throw new RMQJMSException("Cannot write to AMQP destination", new UnsupportedOperationException("MessageProducer.send to undefined AMQP resource"));
    }
    if (msg instanceof RMQBytesMessage || msg instanceof RMQTextMessage) {
        try {
            AMQP.BasicProperties.Builder bob = new AMQP.BasicProperties.Builder();
            bob.contentType("application/octet-stream");
            bob.deliveryMode(RMQMessage.rmqDeliveryMode(deliveryMode));
            bob.priority(priority);
            bob.expiration(rmqExpiration(timeToLive));
            bob.headers(msg.toAmqpHeaders());
            bob = amqpPropertiesCustomiser.apply(bob, msg);
            byte[] data = msg.toAmqpByteArray();
            this.session.getChannel().basicPublish(destination.getAmqpExchangeName(), destination.getAmqpRoutingKey(), bob.build(), data);
        } catch (IOException x) {
            throw new RMQJMSException(x);
        }
    } else {
        this.logger.error("Unsupported message type {} for AMQP destination {}", msg.getClass().getName(), destination);
        throw new RMQJMSException("Unsupported message type for AMQP destination", new UnsupportedOperationException("MessageProducer.send to AMQP resource: Message not Text or Bytes"));
    }
}
Also used : RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) AMQP(com.rabbitmq.client.AMQP) RMQTextMessage(com.rabbitmq.jms.client.message.RMQTextMessage) RMQBytesMessage(com.rabbitmq.jms.client.message.RMQBytesMessage) IOException(java.io.IOException)

Example 2 with RMQTextMessage

use of com.rabbitmq.jms.client.message.RMQTextMessage in project rabbitmq-jms-client by rabbitmq.

the class SelectedTopicMessageIT method testSendAndReceiveTextMessages.

@Test
public void testSendAndReceiveTextMessages() throws Exception {
    topicConn.start();
    TopicSession topicSession = topicConn.createTopicSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Topic topic = topicSession.createTopic(TOPIC_NAME);
    TopicPublisher sender = topicSession.createPublisher(topic);
    TopicSubscriber receiver1 = topicSession.createSubscriber(topic, "boolProp", false);
    TopicSubscriber receiver2 = topicSession.createSubscriber(topic, "not boolProp", false);
    sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    TextMessage message1 = topicSession.createTextMessage(MESSAGE1);
    TextMessage message2 = topicSession.createTextMessage(MESSAGE2);
    message1.setBooleanProperty("boolProp", true);
    message2.setBooleanProperty("boolProp", false);
    sender.send(message1);
    sender.send(message2);
    RMQTextMessage tmsg1 = (RMQTextMessage) receiver1.receive();
    RMQTextMessage tmsg2 = (RMQTextMessage) receiver2.receive();
    String t1 = tmsg1.getText();
    String t2 = tmsg2.getText();
    assertEquals(MESSAGE1, t1);
    assertEquals(MESSAGE2, t2);
}
Also used : TopicSubscriber(javax.jms.TopicSubscriber) TopicSession(javax.jms.TopicSession) TopicPublisher(javax.jms.TopicPublisher) RMQTextMessage(com.rabbitmq.jms.client.message.RMQTextMessage) Topic(javax.jms.Topic) TextMessage(javax.jms.TextMessage) RMQTextMessage(com.rabbitmq.jms.client.message.RMQTextMessage) Test(org.junit.jupiter.api.Test)

Example 3 with RMQTextMessage

use of com.rabbitmq.jms.client.message.RMQTextMessage in project rabbitmq-jms-client by rabbitmq.

the class RMQMessageProducerTest method preferMessagePropertyMessagePropertiesSpecified.

@Test
public void preferMessagePropertyMessagePropertiesSpecified() throws Exception {
    StubRMQMessageProducer producer = new StubRMQMessageProducer(session, destination, false);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    producer.setPriority(9);
    producer.setTimeToLive(1000L);
    RMQTextMessage message = new RMQTextMessage();
    message.setJMSPriority(1);
    message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
    long expiration = System.currentTimeMillis() + 10 * 1000;
    message.setJMSExpiration(expiration);
    producer.send(message);
    assertEquals(DeliveryMode.PERSISTENT, message.getJMSDeliveryMode());
    assertEquals(1, message.getJMSPriority());
    assertEquals(expiration, message.getJMSExpiration());
}
Also used : RMQTextMessage(com.rabbitmq.jms.client.message.RMQTextMessage) Test(org.junit.jupiter.api.Test)

Example 4 with RMQTextMessage

use of com.rabbitmq.jms.client.message.RMQTextMessage in project rabbitmq-jms-client by rabbitmq.

the class RMQMessageProducerTest method preferProducerPropertyNoMessagePropertySpecified.

@Test
public void preferProducerPropertyNoMessagePropertySpecified() throws Exception {
    StubRMQMessageProducer producer = new StubRMQMessageProducer(session, destination, true);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    producer.setPriority(9);
    producer.setTimeToLive(1000L);
    RMQTextMessage message = new RMQTextMessage();
    producer.send(message);
    assertEquals(DeliveryMode.NON_PERSISTENT, message.getJMSDeliveryMode());
    assertEquals(9, message.getJMSPriority());
    assertTrue(message.getJMSExpiration() > System.currentTimeMillis());
}
Also used : RMQTextMessage(com.rabbitmq.jms.client.message.RMQTextMessage) Test(org.junit.jupiter.api.Test)

Example 5 with RMQTextMessage

use of com.rabbitmq.jms.client.message.RMQTextMessage in project rabbitmq-jms-client by rabbitmq.

the class RMQMessageProducerTest method preferProducerPropertyMessagePropertiesSpecified.

@Test
public void preferProducerPropertyMessagePropertiesSpecified() throws Exception {
    StubRMQMessageProducer producer = new StubRMQMessageProducer(session, destination, true);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    producer.setPriority(9);
    producer.setTimeToLive(1000L);
    RMQTextMessage message = new RMQTextMessage();
    message.setJMSPriority(1);
    message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
    message.setJMSExpiration(System.currentTimeMillis() + 10 * 1000);
    producer.send(message);
    assertEquals(DeliveryMode.NON_PERSISTENT, message.getJMSDeliveryMode());
    assertEquals(9, message.getJMSPriority());
    assertTrue(message.getJMSExpiration() > System.currentTimeMillis());
}
Also used : RMQTextMessage(com.rabbitmq.jms.client.message.RMQTextMessage) Test(org.junit.jupiter.api.Test)

Aggregations

RMQTextMessage (com.rabbitmq.jms.client.message.RMQTextMessage)7 Test (org.junit.jupiter.api.Test)6 TextMessage (javax.jms.TextMessage)2 Topic (javax.jms.Topic)2 TopicPublisher (javax.jms.TopicPublisher)2 TopicSession (javax.jms.TopicSession)2 TopicSubscriber (javax.jms.TopicSubscriber)2 AMQP (com.rabbitmq.client.AMQP)1 RMQBytesMessage (com.rabbitmq.jms.client.message.RMQBytesMessage)1 RMQJMSException (com.rabbitmq.jms.util.RMQJMSException)1 IOException (java.io.IOException)1