Search in sources :

Example 6 with MapMessage

use of javax.jms.MapMessage in project spring-framework by spring-projects.

the class SimpleMessageConverterTests method testMapConversionWhereMapHasNonStringTypesForKeys.

@Test
public void testMapConversionWhereMapHasNonStringTypesForKeys() throws JMSException {
    MapMessage message = mock(MapMessage.class);
    Session session = mock(Session.class);
    given(session.createMapMessage()).willReturn(message);
    Map<Integer, String> content = new HashMap<>(1);
    content.put(1, "value1");
    SimpleMessageConverter converter = new SimpleMessageConverter();
    try {
        converter.toMessage(content, session);
        fail("expected MessageConversionException");
    } catch (MessageConversionException ex) {
    /* expected */
    }
}
Also used : MessageConversionException(org.springframework.jms.support.converter.MessageConversionException) HashMap(java.util.HashMap) MapMessage(javax.jms.MapMessage) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(javax.jms.Session) Test(org.junit.Test)

Example 7 with MapMessage

use of javax.jms.MapMessage 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 {
        // if we are configured to not map the jms message then return it as body
        if (!mapJmsMessage) {
            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) BytesMessage(javax.jms.BytesMessage) StreamMessage(javax.jms.StreamMessage) JMSException(javax.jms.JMSException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) TextMessage(javax.jms.TextMessage)

Example 8 with MapMessage

use of javax.jms.MapMessage in project ACS by ACS-Community.

the class HelloWorld method onMessage.

/**
   * Method implementing the message handler defined in the
   * SubscriptionListener interface.
   *
   * @param m
   */
public void onMessage(javax.jms.Message m) {
    try {
        MapMessage message = (MapMessage) m;
        String txt = message.getString(TEXT_MAP_FIELD);
        java.util.Date date = new java.util.Date(message.getLong(TIMESTAMP_MAP_FIELD));
        cat.info("got message : [" + date.toString() + "] " + txt);
    } catch (Exception e) {
        cat.info("Exception caught", e);
    }
    close();
}
Also used : MapMessage(javax.jms.MapMessage) MOMException(cern.cmw.mom.pubsub.MOMException) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException)

Example 9 with MapMessage

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

the class SubscriberSampler method extractContent.

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)

Example 10 with MapMessage

use of javax.jms.MapMessage in project wildfly by wildfly.

the class EnvEntryTestCase method testEnvEntriesMDB.

@Test
public void testEnvEntriesMDB() throws Exception {
    ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
    Connection con = factory.createConnection();
    try {
        Destination dest = (Destination) ctx.lookup("java:jboss/queue/testEnvEntry");
        Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(dest);
        Queue replyQueue = session.createTemporaryQueue();
        MessageConsumer consumer = session.createConsumer(replyQueue);
        con.start();
        TextMessage msg = session.createTextMessage();
        msg.setJMSReplyTo(replyQueue);
        msg.setText("This is message one");
        producer.send(msg);
        MapMessage replyMsg = (MapMessage) consumer.receive(5000);
        Assert.assertNotNull(replyMsg);
        Assert.assertEquals(16, replyMsg.getInt("maxExceptions"));
        Assert.assertEquals(12, replyMsg.getInt("numExceptions"));
        Assert.assertEquals(7, replyMsg.getInt("minExceptions"));
    } finally {
        con.close();
    }
}
Also used : Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) MapMessage(javax.jms.MapMessage) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

MapMessage (javax.jms.MapMessage)30 JMSException (javax.jms.JMSException)23 HashMap (java.util.HashMap)12 Map (java.util.Map)9 TextMessage (javax.jms.TextMessage)9 BytesMessage (javax.jms.BytesMessage)8 Message (javax.jms.Message)8 Session (javax.jms.Session)8 Destination (javax.jms.Destination)7 ObjectMessage (javax.jms.ObjectMessage)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 Test (org.junit.Test)6 AMQPMessage (org.apache.qpid.qmf2.common.AMQPMessage)5 Connection (javax.jms.Connection)4 MessageProducer (javax.jms.MessageProducer)4 QmfException (org.apache.qpid.qmf2.common.QmfException)4 MessageFormatException (javax.jms.MessageFormatException)3 StreamMessage (javax.jms.StreamMessage)3 QmfData (org.apache.qpid.qmf2.common.QmfData)3 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)3