Search in sources :

Example 81 with BytesMessage

use of javax.jms.BytesMessage in project activemq-artemis by apache.

the class CompressedInteropTest method sendCompressedBytesMessageUsingOpenWire.

private void sendCompressedBytesMessageUsingOpenWire() throws Exception {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);
    final ActiveMQMessageProducer producer = (ActiveMQMessageProducer) session.createProducer(destination);
    BytesMessage bytesMessage = session.createBytesMessage();
    bytesMessage.writeBytes(TEXT.getBytes());
    producer.send(bytesMessage);
}
Also used : ActiveMQMessageProducer(org.apache.activemq.ActiveMQMessageProducer) BytesMessage(javax.jms.BytesMessage) Session(javax.jms.Session) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination)

Example 82 with BytesMessage

use of javax.jms.BytesMessage in project activemq-artemis by apache.

the class CompressedInteropTest method receiveBytesMessage.

private void receiveBytesMessage(boolean useCore) throws Exception {
    BytesMessage bytesMessage = (BytesMessage) receiveMessage(useCore);
    byte[] bytes = new byte[TEXT.getBytes(StandardCharsets.UTF_8).length];
    bytesMessage.readBytes(bytes);
    assertTrue(bytesMessage.readBytes(new byte[255]) == -1);
    String rcvString = new String(bytes, StandardCharsets.UTF_8);
    assertEquals(TEXT, rcvString);
}
Also used : BytesMessage(javax.jms.BytesMessage)

Example 83 with BytesMessage

use of javax.jms.BytesMessage in project meteo by pierre.

the class TopicListener method onMessage.

@Override
public void onMessage(Message message) {
    if (message instanceof TextMessage) {
        TextMessage txtMsg = (TextMessage) message;
        String txt = null;
        try {
            txt = txtMsg.getText();
            log.debug("Received a message, yay!\n" + txt);
            Map event = mapper.readValue(txt, Map.class);
            esperSink.getEPRuntime().sendEvent(event, esperTopicKey);
        } catch (JMSException ex) {
            log.warn("Got an error from the message queue", ex);
        } catch (ClassCastException ex) {
            log.info("Received message that I couldn't parse: " + txt, ex);
        } catch (JsonMappingException ex) {
            log.info("Received message that I couldn't parse: " + txt, ex);
        } catch (JsonParseException ex) {
            log.info("Received message that I couldn't parse: " + txt, ex);
        } catch (IOException ex) {
            log.warn("Got an error from the message queue", ex);
        }
    } else if (message instanceof BytesMessage) {
        final BytesMessage byteMessage = (BytesMessage) message;
        long llen;
        try {
            llen = byteMessage.getBodyLength();
        } catch (JMSException e) {
            log.warn("Unable to get message length", e);
            return;
        }
        if (llen > Integer.MAX_VALUE) {
            // should never occur but...
            log.error("Ridiculously huge message payload, above 32-bit length");
        } else {
            final int len = (int) llen;
            final byte[] data = new byte[len];
            final int readLen;
            try {
                readLen = byteMessage.readBytes(data);
            } catch (JMSException e) {
                log.warn("Unable to get message bytes", e);
                return;
            }
            if (readLen < len) {
                log.error("Failed to read byte message contents; read {}, was trying to read {}", readLen, data.length);
            } else {
                final Map event;
                try {
                    event = mapper.readValue(data, Map.class);
                    esperSink.getEPRuntime().sendEvent(event, esperTopicKey);
                } catch (IOException e) {
                    log.error("Failed to convert message to Esper Event", readLen, data.length);
                }
            }
        }
    } else {
        log.error("Unexpected message type '{}' from AMQ broker: must skip", message.getClass().getName());
    }
}
Also used : JsonMappingException(org.codehaus.jackson.map.JsonMappingException) JMSException(javax.jms.JMSException) BytesMessage(javax.jms.BytesMessage) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) Map(java.util.Map) TextMessage(javax.jms.TextMessage)

Example 84 with BytesMessage

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

the class JMSSampler method extractContent.

@SuppressWarnings("JdkObsolete")
private void extractContent(StringBuilder buffer, StringBuilder propBuffer, Message msg) {
    if (msg != null) {
        try {
            if (msg instanceof TextMessage) {
                buffer.append(((TextMessage) msg).getText());
            } else if (msg instanceof ObjectMessage) {
                ObjectMessage objectMessage = (ObjectMessage) msg;
                if (objectMessage.getObject() != null) {
                    buffer.append(objectMessage.getObject().getClass());
                } else {
                    buffer.append("object is null");
                }
            } else if (msg instanceof BytesMessage) {
                BytesMessage bytesMessage = (BytesMessage) msg;
                buffer.append(bytesMessage.getBodyLength() + " bytes received in BytesMessage");
            } else if (msg instanceof MapMessage) {
                MapMessage mapm = (MapMessage) msg;
                // MapNames are Strings
                @SuppressWarnings("unchecked") Enumeration<String> enumb = mapm.getMapNames();
                while (enumb.hasMoreElements()) {
                    String name = enumb.nextElement();
                    Object obj = mapm.getObject(name);
                    buffer.append(name);
                    buffer.append(",");
                    buffer.append(obj.getClass().getCanonicalName());
                    buffer.append(",");
                    buffer.append(obj);
                    buffer.append("\n");
                }
            }
            Utils.messageProperties(propBuffer, msg);
        } catch (JMSException e) {
            buffer.append("Error extracting content from message:" + e.getMessage());
            LOGGER.error("Error extracting content from message", e);
        }
    }
}
Also used : Enumeration(java.util.Enumeration) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) TextMessage(javax.jms.TextMessage)

Example 85 with BytesMessage

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

the class SubscriberSampler method extractContent.

@SuppressWarnings("JdkObsolete")
private void extractContent(StringBuilder buffer, StringBuilder propBuffer, Message msg, boolean isLast) {
    if (msg != null) {
        try {
            if (msg instanceof TextMessage) {
                buffer.append(((TextMessage) msg).getText());
            } else if (msg instanceof ObjectMessage) {
                ObjectMessage objectMessage = (ObjectMessage) msg;
                if (objectMessage.getObject() != null) {
                    buffer.append(objectMessage.getObject().getClass());
                } else {
                    buffer.append("object is null");
                }
            } else if (msg instanceof BytesMessage) {
                BytesMessage bytesMessage = (BytesMessage) msg;
                buffer.append(bytesMessage.getBodyLength() + " bytes received in BytesMessage");
            } else if (msg instanceof MapMessage) {
                MapMessage mapm = (MapMessage) msg;
                // MapNames are Strings
                @SuppressWarnings("unchecked") Enumeration<String> enumb = mapm.getMapNames();
                while (enumb.hasMoreElements()) {
                    String name = enumb.nextElement();
                    Object obj = mapm.getObject(name);
                    buffer.append(name);
                    buffer.append(",");
                    buffer.append(obj.getClass().getCanonicalName());
                    buffer.append(",");
                    buffer.append(obj);
                    buffer.append("\n");
                }
            }
            Utils.messageProperties(propBuffer, msg);
            if (!isLast && !StringUtils.isEmpty(separator)) {
                propBuffer.append(separator);
                buffer.append(separator);
            }
        } catch (JMSException e) {
            log.error(e.getMessage());
        }
    }
}
Also used : Enumeration(java.util.Enumeration) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) TextMessage(javax.jms.TextMessage)

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