Search in sources :

Example 1 with RMQBytesMessage

use of com.rabbitmq.jms.client.message.RMQBytesMessage 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 RMQBytesMessage

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

the class ForeignBytesMessageTest method testEmptyBytesMessage.

@Test
public void testEmptyBytesMessage() throws Exception {
    BytesMessage testMsg = new RMQBytesMessage();
    assertEquals(0, testMsg.getBodyLength());
    BytesMessage rmqMsg = (BytesMessage) RMQBytesMessage.recreate(testMsg);
    assertEquals(0, rmqMsg.getBodyLength());
}
Also used : RMQBytesMessage(com.rabbitmq.jms.client.message.RMQBytesMessage) BytesMessage(javax.jms.BytesMessage) RMQBytesMessage(com.rabbitmq.jms.client.message.RMQBytesMessage) Test(org.junit.jupiter.api.Test)

Example 3 with RMQBytesMessage

use of com.rabbitmq.jms.client.message.RMQBytesMessage in project rocketmq-externals by apache.

the class RabbitmqSourceTaskTest method getMessageConnentTest.

@Test
public void getMessageConnentTest() throws JMSException {
    String value = "hello rocketmq";
    RabbitmqSourceTask task = new RabbitmqSourceTask();
    RMQTextMessage textMessage = new RMQTextMessage();
    textMessage.setText(value);
    ByteBuffer buffer = task.getMessageContent(textMessage);
    Assert.assertEquals(new String(buffer.array()), textMessage.getText());
    ObjectMessage objectMessage = new RMQObjectMessage();
    objectMessage.setObject(value);
    buffer = task.getMessageContent(objectMessage);
    Assert.assertEquals(new String(buffer.array()), "\"" + objectMessage.getObject().toString() + "\"");
    BytesMessage bytes = new RMQBytesMessage();
    bytes.writeBytes(value.getBytes());
    bytes.reset();
    buffer = task.getMessageContent(bytes);
    Assert.assertEquals(new String(buffer.array()), value);
    MapMessage mapMessage = new RMQMapMessage();
    mapMessage.setString("hello", "rocketmq");
    buffer = task.getMessageContent(mapMessage);
    Map<String, String> map = JSON.parseObject(buffer.array(), Map.class);
    Assert.assertEquals(map.get("hello"), "rocketmq");
    Assert.assertEquals(map.size(), 1);
    StreamMessage streamMessage = new RMQStreamMessage();
    String valueTwo = null;
    for (int i = 0; i < 200; i++) {
        valueTwo = valueTwo + value;
    }
    streamMessage.writeBytes(valueTwo.getBytes());
    streamMessage.reset();
// buffer = task.getMessageContent(streamMessage);
// Assert.assertEquals(new String(buffer.array()), valueTwo);
}
Also used : MapMessage(javax.jms.MapMessage) RMQMapMessage(com.rabbitmq.jms.client.message.RMQMapMessage) RMQTextMessage(com.rabbitmq.jms.client.message.RMQTextMessage) RMQBytesMessage(com.rabbitmq.jms.client.message.RMQBytesMessage) BytesMessage(javax.jms.BytesMessage) ByteBuffer(java.nio.ByteBuffer) RMQMapMessage(com.rabbitmq.jms.client.message.RMQMapMessage) RMQStreamMessage(com.rabbitmq.jms.client.message.RMQStreamMessage) RMQObjectMessage(com.rabbitmq.jms.client.message.RMQObjectMessage) ObjectMessage(javax.jms.ObjectMessage) RMQObjectMessage(com.rabbitmq.jms.client.message.RMQObjectMessage) RMQStreamMessage(com.rabbitmq.jms.client.message.RMQStreamMessage) StreamMessage(javax.jms.StreamMessage) RMQBytesMessage(com.rabbitmq.jms.client.message.RMQBytesMessage) Test(org.junit.Test)

Aggregations

RMQBytesMessage (com.rabbitmq.jms.client.message.RMQBytesMessage)3 RMQTextMessage (com.rabbitmq.jms.client.message.RMQTextMessage)2 BytesMessage (javax.jms.BytesMessage)2 AMQP (com.rabbitmq.client.AMQP)1 RMQMapMessage (com.rabbitmq.jms.client.message.RMQMapMessage)1 RMQObjectMessage (com.rabbitmq.jms.client.message.RMQObjectMessage)1 RMQStreamMessage (com.rabbitmq.jms.client.message.RMQStreamMessage)1 RMQJMSException (com.rabbitmq.jms.util.RMQJMSException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 MapMessage (javax.jms.MapMessage)1 ObjectMessage (javax.jms.ObjectMessage)1 StreamMessage (javax.jms.StreamMessage)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1