Search in sources :

Example 91 with BytesMessage

use of javax.jms.BytesMessage in project camel by apache.

the class JmsBinding method extractBodyFromJms.

/**
     * Extracts the body from the JMS message
     *
     * @param exchange the exchange
     * @param message  the message to extract its body
     * @return the body, can be <tt>null</tt>
     */
public Object extractBodyFromJms(Exchange exchange, Message message) {
    try {
        // based on message type
        if (endpoint != null && endpoint.getMessageConverter() != null) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Extracting body using a custom MessageConverter: {} from JMS message: {}", endpoint.getMessageConverter(), message);
            }
            return endpoint.getMessageConverter().fromMessage(message);
        }
        // if we are configured to not map the jms message then return it as body
        if (endpoint != null && !endpoint.getConfiguration().isMapJmsMessage()) {
            LOG.trace("Option map JMS message is false so using JMS message as body: {}", message);
            return message;
        }
        if (message instanceof ObjectMessage) {
            LOG.trace("Extracting body as a ObjectMessage from JMS message: {}", message);
            ObjectMessage objectMessage = (ObjectMessage) message;
            Object payload = objectMessage.getObject();
            if (payload instanceof DefaultExchangeHolder) {
                DefaultExchangeHolder holder = (DefaultExchangeHolder) payload;
                DefaultExchangeHolder.unmarshal(exchange, holder);
                return exchange.getIn().getBody();
            } else {
                return objectMessage.getObject();
            }
        } else if (message instanceof TextMessage) {
            LOG.trace("Extracting body as a TextMessage from JMS message: {}", message);
            TextMessage textMessage = (TextMessage) message;
            return textMessage.getText();
        } else if (message instanceof MapMessage) {
            LOG.trace("Extracting body as a MapMessage from JMS message: {}", message);
            return createMapFromMapMessage((MapMessage) message);
        } else if (message instanceof BytesMessage) {
            LOG.trace("Extracting body as a BytesMessage from JMS message: {}", message);
            return createByteArrayFromBytesMessage((BytesMessage) message);
        } else if (message instanceof StreamMessage) {
            LOG.trace("Extracting body as a StreamMessage from JMS message: {}", message);
            return message;
        } else {
            return null;
        }
    } catch (JMSException e) {
        throw new RuntimeCamelException("Failed to extract body due to: " + e + ". Message: " + message, e);
    }
}
Also used : DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) Object(org.apache.camel.component.jms.JmsMessageType.Object) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) JMSException(javax.jms.JMSException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) TextMessage(javax.jms.TextMessage)

Example 92 with BytesMessage

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

the class MessageTest method getJmsBytesMessage.

@Test
public void getJmsBytesMessage() throws Exception {
    final byte[] content = new byte[512];
    IntStream.range(0, content.length).forEachOrdered(i -> content[i] = (byte) (i % 256));
    Connection connection = getConnection();
    try {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(session.createQueue(QUEUE_NAME));
        BytesMessage message = session.createBytesMessage();
        message.writeBytes(content);
        producer.send(message);
    } finally {
        connection.close();
    }
    List<Map<String, Object>> messages = getHelper().postJson("queue/myqueue/getMessageInfo", Collections.singletonMap("includeHeaders", Boolean.TRUE), LIST_MAP_TYPE_REF, SC_OK);
    assertThat(messages.size(), is(equalTo(1)));
    Map<String, Object> message = messages.get(0);
    int messageId = (int) message.get("id");
    byte[] receivedContent = getHelper().getBytes(String.format("queue/myqueue/getMessageContent?messageId=%d", messageId));
    assumeThat("AMQP1.0 messages return the AMQP type", getProtocol(), is(not(equalTo(Protocol.AMQP_1_0))));
    assertThat(receivedContent, is(equalTo(content)));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) HashMap(java.util.HashMap) Map(java.util.Map) Session(javax.jms.Session) Test(org.junit.Test)

Example 93 with BytesMessage

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

the class BDBAMQP10V0UpgradeTest method testRecoverAmqpV0Message.

public void testRecoverAmqpV0Message() throws Exception {
    Connection connection = getConnection();
    connection.start();
    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    Queue queue = createTestQueue(session, "queue");
    MessageConsumer consumer = session.createConsumer(queue);
    Message message = consumer.receive(getReceiveTimeout());
    assertNotNull("Recovered message not received", message);
    assertTrue(message instanceof BytesMessage);
    BytesMessage bytesMessage = ((BytesMessage) message);
    long length = bytesMessage.getBodyLength();
    String expectedContentHash = message.getStringProperty("sha256hash");
    byte[] content = new byte[(int) length];
    bytesMessage.readBytes(content);
    assertEquals("Unexpected content length", EXPECTED_MESSAGE_LENGTH, length);
    assertNotNull("Message should carry expectedShaHash property", expectedContentHash);
    String contentHash = computeContentHash(content);
    assertEquals("Unexpected content hash", expectedContentHash, contentHash);
    session.commit();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) Queue(javax.jms.Queue) Session(javax.jms.Session)

Example 94 with BytesMessage

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

the class BytesMessageTest method sendAndReceiveEmpty.

@Test
public void sendAndReceiveEmpty() throws Exception {
    Queue queue = createQueue(getTestName());
    Connection connection = getConnection();
    try {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(queue);
        BytesMessage message = session.createBytesMessage();
        producer.send(message);
        MessageConsumer consumer = session.createConsumer(queue);
        connection.start();
        Message receivedMessage = consumer.receive(getReceiveTimeout());
        assertTrue("BytesMessage should be received", receivedMessage instanceof BytesMessage);
        assertEquals("Unexpected body length", 0, ((BytesMessage) receivedMessage).getBodyLength());
    } finally {
        connection.close();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) Session(javax.jms.Session) Test(org.junit.Test)

Example 95 with BytesMessage

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

the class ProducerFlowControlTest method nextMessage.

private Message nextMessage(int msg, Session producerSession) throws JMSException {
    BytesMessage send = producerSession.createBytesMessage();
    send.writeBytes(BYTE_300);
    send.setIntProperty("msg", msg);
    return send;
}
Also used : BytesMessage(javax.jms.BytesMessage)

Aggregations

BytesMessage (javax.jms.BytesMessage)180 Session (javax.jms.Session)79 MessageProducer (javax.jms.MessageProducer)77 MessageConsumer (javax.jms.MessageConsumer)70 Test (org.junit.Test)59 Message (javax.jms.Message)54 TextMessage (javax.jms.TextMessage)47 Connection (javax.jms.Connection)41 Queue (javax.jms.Queue)37 JMSException (javax.jms.JMSException)34 MapMessage (javax.jms.MapMessage)29 ObjectMessage (javax.jms.ObjectMessage)26 StreamMessage (javax.jms.StreamMessage)20 HashMap (java.util.HashMap)18 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)17 Topic (javax.jms.Topic)14 Map (java.util.Map)12 ActiveMQMessage (org.apache.activemq.command.ActiveMQMessage)12 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)10 Destination (javax.jms.Destination)9