Example usage for javax.jms Message getJMSDeliveryMode

List of usage examples for javax.jms Message getJMSDeliveryMode

Introduction

In this page you can find the example usage for javax.jms Message getJMSDeliveryMode.

Prototype


int getJMSDeliveryMode() throws JMSException;

Source Link

Document

Gets the DeliveryMode value specified for this message.

Usage

From source file:org.apache.camel.component.jms.JmsBinding.java

public Map<String, Object> extractHeadersFromJms(Message jmsMessage, Exchange exchange) {
    Map<String, Object> map = new HashMap<String, Object>();
    if (jmsMessage != null) {
        // lets populate the standard JMS message headers
        try {//  www .j  a v  a  2s.c o  m
            map.put("JMSCorrelationID", jmsMessage.getJMSCorrelationID());
            map.put("JMSDeliveryMode", jmsMessage.getJMSDeliveryMode());
            map.put("JMSDestination", jmsMessage.getJMSDestination());
            map.put("JMSExpiration", jmsMessage.getJMSExpiration());
            map.put("JMSMessageID", jmsMessage.getJMSMessageID());
            map.put("JMSPriority", jmsMessage.getJMSPriority());
            map.put("JMSRedelivered", jmsMessage.getJMSRedelivered());
            map.put("JMSTimestamp", jmsMessage.getJMSTimestamp());

            // to work around OracleAQ not supporting the JMSReplyTo header (CAMEL-2909)
            try {
                map.put("JMSReplyTo", jmsMessage.getJMSReplyTo());
            } catch (JMSException e) {
                LOG.trace("Cannot read JMSReplyTo header. Will ignore this exception.", e);
            }
            // to work around OracleAQ not supporting the JMSType header (CAMEL-2909)
            try {
                map.put("JMSType", jmsMessage.getJMSType());
            } catch (JMSException e) {
                LOG.trace("Cannot read JMSType header. Will ignore this exception.", e);
            }

            // this works around a bug in the ActiveMQ property handling
            map.put("JMSXGroupID", jmsMessage.getStringProperty("JMSXGroupID"));
        } catch (JMSException e) {
            throw new RuntimeCamelException(e);
        }

        Enumeration names;
        try {
            names = jmsMessage.getPropertyNames();
        } catch (JMSException e) {
            throw new RuntimeCamelException(e);
        }
        while (names.hasMoreElements()) {
            String name = names.nextElement().toString();
            try {
                Object value = jmsMessage.getObjectProperty(name);
                if (headerFilterStrategy != null
                        && headerFilterStrategy.applyFilterToExternalHeaders(name, value, exchange)) {
                    continue;
                }

                // must decode back from safe JMS header name to original header name
                // when storing on this Camel JmsMessage object.
                String key = jmsKeyFormatStrategy.decodeKey(name);
                map.put(key, value);
            } catch (JMSException e) {
                throw new RuntimeCamelException(name, e);
            }
        }
    }

    return map;
}

From source file:org.apache.james.queue.jms.JMSMailQueue.java

@Override
public long flush() throws MailQueueException {
    boolean first = true;
    long count = 0;
    try (Session session = connection.createSession(true, Session.SESSION_TRANSACTED)) {
        Queue queue = session.createQueue(queueName);
        try (MessageConsumer consumer = session.createConsumer(queue)) {
            try (MessageProducer producer = session.createProducer(queue)) {

                Message message = null;
                while (first || message != null) {
                    if (first) {
                        // give the consumer 2000 ms to receive messages
                        message = consumer.receive(2000);
                    } else {
                        message = consumer.receiveNoWait();
                    }//from  w w  w  . ja v a 2 s  .c  o  m
                    first = false;

                    if (message != null) {
                        Message m = copy(session, message);
                        m.setBooleanProperty(FORCE_DELIVERY, true);
                        producer.send(m, message.getJMSDeliveryMode(), message.getJMSPriority(),
                                message.getJMSExpiration());
                        count++;
                    }
                }
                session.commit();
                return count;
            }
        }
    } catch (Exception e) {
        LOGGER.error("Unable to flush mail", e);
        throw new MailQueueException("Unable to get size of queue " + queueName, e);
    }
}

From source file:org.apache.qpid.disttest.jms.ClientJmsDelegate.java

public Message sendNextMessage(final CreateProducerCommand command) {
    Message sentMessage = null;
    MessageProvider messageProvider = _testMessageProviders.get(command.getMessageProviderName());
    if (messageProvider == null) {
        messageProvider = _defaultMessageProvider;
    }//  ww  w  .  j a va2s .c o m

    final Session session = _testSessions.get(command.getSessionName());
    final MessageProducer producer = _testProducers.get(command.getParticipantName());
    try {
        sentMessage = messageProvider.nextMessage(session, command);
        int deliveryMode = producer.getDeliveryMode();
        int priority = producer.getPriority();
        long ttl = producer.getTimeToLive();
        if (messageProvider.isPropertySet(MessageProvider.PRIORITY)) {
            priority = sentMessage.getJMSPriority();
        }
        if (messageProvider.isPropertySet(MessageProvider.DELIVERY_MODE)) {
            deliveryMode = sentMessage.getJMSDeliveryMode();
        }
        if (messageProvider.isPropertySet(MessageProvider.TTL)) {
            ttl = sentMessage.getLongProperty(MessageProvider.TTL);
        }
        producer.send(sentMessage, deliveryMode, priority, ttl);
    } catch (final JMSException jmse) {
        throw new DistributedTestException("Unable to create and send message with producer: "
                + command.getParticipantName() + " on session: " + command.getSessionName(), jmse);
    }
    return sentMessage;
}

From source file:org.apache.synapse.transport.jms.JMSUtils.java

/**
 * Extract transport level headers for JMS from the given message into a Map
 *
 * @param message the JMS message//  w w  w  .java 2  s.c o m
 * @return a Map of the transport headers
 */
public static Map getTransportHeaders(Message message) {
    // create a Map to hold transport headers
    Map map = new HashMap();

    // correlation ID
    try {
        if (message.getJMSCorrelationID() != null) {
            map.put(JMSConstants.JMS_COORELATION_ID, message.getJMSCorrelationID());
        }
    } catch (JMSException ignore) {
    }

    // set the delivery mode as persistent or not
    try {
        map.put(JMSConstants.JMS_DELIVERY_MODE, Integer.toString(message.getJMSDeliveryMode()));
    } catch (JMSException ignore) {
    }

    // destination name
    try {
        if (message.getJMSDestination() != null) {
            Destination dest = message.getJMSDestination();
            map.put(JMSConstants.JMS_DESTINATION,
                    dest instanceof Queue ? ((Queue) dest).getQueueName() : ((Topic) dest).getTopicName());
        }
    } catch (JMSException ignore) {
    }

    // expiration
    try {
        map.put(JMSConstants.JMS_EXPIRATION, Long.toString(message.getJMSExpiration()));
    } catch (JMSException ignore) {
    }

    // if a JMS message ID is found
    try {
        if (message.getJMSMessageID() != null) {
            map.put(JMSConstants.JMS_MESSAGE_ID, message.getJMSMessageID());
        }
    } catch (JMSException ignore) {
    }

    // priority
    try {
        map.put(JMSConstants.JMS_PRIORITY, Long.toString(message.getJMSPriority()));
    } catch (JMSException ignore) {
    }

    // redelivered
    try {
        map.put(JMSConstants.JMS_REDELIVERED, Boolean.toString(message.getJMSRedelivered()));
    } catch (JMSException ignore) {
    }

    // replyto destination name
    try {
        if (message.getJMSReplyTo() != null) {
            Destination dest = message.getJMSReplyTo();
            map.put(JMSConstants.JMS_REPLY_TO,
                    dest instanceof Queue ? ((Queue) dest).getQueueName() : ((Topic) dest).getTopicName());
        }
    } catch (JMSException ignore) {
    }

    // priority
    try {
        map.put(JMSConstants.JMS_TIMESTAMP, Long.toString(message.getJMSTimestamp()));
    } catch (JMSException ignore) {
    }

    // message type
    try {
        if (message.getJMSType() != null) {
            map.put(JMSConstants.JMS_TYPE, message.getJMSType());
        }
    } catch (JMSException ignore) {
    }

    // any other transport properties / headers
    Enumeration e = null;
    try {
        e = message.getPropertyNames();
    } catch (JMSException ignore) {
    }

    if (e != null) {
        while (e.hasMoreElements()) {
            String headerName = (String) e.nextElement();
            try {
                map.put(headerName, message.getStringProperty(headerName));
                continue;
            } catch (JMSException ignore) {
            }
            try {
                map.put(headerName, Boolean.valueOf(message.getBooleanProperty(headerName)));
                continue;
            } catch (JMSException ignore) {
            }
            try {
                map.put(headerName, new Integer(message.getIntProperty(headerName)));
                continue;
            } catch (JMSException ignore) {
            }
            try {
                map.put(headerName, new Long(message.getLongProperty(headerName)));
                continue;
            } catch (JMSException ignore) {
            }
            try {
                map.put(headerName, new Double(message.getDoubleProperty(headerName)));
                continue;
            } catch (JMSException ignore) {
            }
            try {
                map.put(headerName, new Float(message.getFloatProperty(headerName)));
                continue;
            } catch (JMSException ignore) {
            }
        }
    }

    return map;
}

From source file:org.mule.transport.jms.JmsMessageUtils.java

public static Message copyJMSProperties(Message from, Message to, JmsConnector connector) throws JMSException {
    if (connector.supportsProperty(JmsConstants.JMS_CORRELATION_ID)) {
        to.setJMSCorrelationID(from.getJMSCorrelationID());
    }//from  w w w. j  a v  a  2  s.c o m
    if (connector.supportsProperty(JmsConstants.JMS_DELIVERY_MODE)) {
        to.setJMSDeliveryMode(from.getJMSDeliveryMode());
    }
    if (connector.supportsProperty(JmsConstants.JMS_DESTINATION)) {
        to.setJMSDestination(from.getJMSDestination());
    }
    if (connector.supportsProperty(JmsConstants.JMS_EXPIRATION)) {
        to.setJMSExpiration(from.getJMSExpiration());
    }
    if (connector.supportsProperty(JmsConstants.JMS_MESSAGE_ID)) {
        to.setJMSMessageID(from.getJMSMessageID());
    }
    if (connector.supportsProperty(JmsConstants.JMS_PRIORITY)) {
        to.setJMSPriority(from.getJMSPriority());
    }
    if (connector.supportsProperty(JmsConstants.JMS_REDELIVERED)) {
        to.setJMSRedelivered(from.getJMSRedelivered());
    }
    if (connector.supportsProperty(JmsConstants.JMS_REPLY_TO)) {
        to.setJMSReplyTo(from.getJMSReplyTo());
    }
    if (connector.supportsProperty(JmsConstants.JMS_TIMESTAMP)) {
        to.setJMSTimestamp(from.getJMSTimestamp());
    }
    if (connector.supportsProperty(JmsConstants.JMS_TYPE)) {
        to.setJMSType(from.getJMSType());
    }
    return to;
}

From source file:org.perfcake.message.sender.JmsSenderTest.java

@Test
public void testBasicSend() throws Exception {
    final Properties props = new Properties();
    props.setProperty("messageType", "STRING");
    props.setProperty("target", "queue/test");

    final JmsSender sender = (JmsSender) ObjectFactory.summonInstance(JmsSender.class.getName(), props);

    Assert.assertEquals(sender.getMessageType(), JmsSender.MessageType.STRING);
    Assert.assertEquals(sender.getTarget(), "queue/test");
    Assert.assertEquals(sender.isPersistent(), true);
    Assert.assertEquals(sender.isTransacted(), false);

    try {/*w ww .j a v a 2  s  .co m*/
        sender.init();

        // make sure the destination is empty
        Assert.assertNull(JmsHelper.readMessage(factory, 500, queue));

        // STRING Type
        final org.perfcake.message.Message message = new org.perfcake.message.Message();
        final String payload1 = "Hello World!";
        message.setPayload(payload1);
        sender.preSend(message, null, null);
        sender.send(message, null);
        sender.postSend(message);

        Message response = JmsHelper.readMessage(factory, 500, queue);
        Assert.assertEquals(response.getJMSDeliveryMode(), DeliveryMode.PERSISTENT);
        Assert.assertTrue(response instanceof TextMessage);
        Assert.assertEquals(((TextMessage) response).getText(), payload1);

        // OBJECT Type
        sender.setMessageType(JmsSender.MessageType.OBJECT);
        final Long payload2 = 42L;
        message.setPayload(payload2);
        sender.preSend(message, null, null);
        sender.send(message, null);
        sender.postSend(message);

        response = JmsHelper.readMessage(factory, 500, queue);
        Assert.assertTrue(response instanceof ObjectMessage);
        Assert.assertTrue(((ObjectMessage) response).getObject() instanceof Long);
        Assert.assertEquals((Long) ((ObjectMessage) response).getObject(), payload2);

        // BYTEARRAY Type
        sender.setMessageType(JmsSender.MessageType.BYTEARRAY);
        message.setPayload(payload1);
        sender.preSend(message, null, null);
        sender.send(message, null);
        sender.postSend(message);

        response = JmsHelper.readMessage(factory, 500, queue);
        Assert.assertTrue(response instanceof BytesMessage);
        Assert.assertEquals(((BytesMessage) response).readUTF(), payload1);

        // make sure the destination is empty
        Assert.assertNull(JmsHelper.readMessage(factory, 500, queue));

    } finally {
        sender.close();
    }
}

From source file:org.perfcake.message.sender.JmsSenderTest.java

@Test
public void testTransactedSecuredSend() throws Exception {
    final String payload = "Hello my secret World!";

    final Properties props = new Properties();
    props.setProperty("messagetType", "STRING");
    props.setProperty("target", "queue/secured_test");
    props.setProperty("username", "frank");
    props.setProperty("password", "frank");

    final JmsSender sender = (JmsSender) ObjectFactory.summonInstance(JmsSender.class.getName(), props);

    Assert.assertEquals(sender.getUsername(), "frank");
    Assert.assertEquals(sender.getPassword(), "frank");

    sender.setTransacted(true);// w w w.  j a va  2s.com

    try {
        sender.init();

        // make sure the destination is empty
        Assert.assertNull(JmsHelper.readMessage(factory, 500, securedQueue));

        // STRING Type
        final org.perfcake.message.Message message = new org.perfcake.message.Message();
        message.setPayload(payload);
        sender.preSend(message, null, null);
        sender.send(message, null);
        sender.postSend(message);

        // make sure the destination is empty because the message is not yet commited (done in close())
        Assert.assertNull(JmsHelper.readMessage(factory, 500, securedQueue));
    } finally {
        sender.close();
    }

    final Message response = JmsHelper.readMessage(factory, 500, securedQueue);
    Assert.assertEquals(response.getJMSDeliveryMode(), DeliveryMode.PERSISTENT);
    Assert.assertTrue(response instanceof TextMessage);
    Assert.assertEquals(((TextMessage) response).getText(), payload);

    // make sure the destination is empty
    Assert.assertNull(JmsHelper.readMessage(factory, 500, securedQueue));
}

From source file:org.perfcake.message.sender.JmsSenderTest.java

@Test
public void testNonPersistentDelivery() throws Exception {
    final Properties props = new Properties();
    props.setProperty("messagetType", "STRING");
    props.setProperty("target", "queue/test");
    props.setProperty("persistent", "false");

    final JmsSender sender = (JmsSender) ObjectFactory.summonInstance(JmsSender.class.getName(), props);

    Assert.assertEquals(sender.isPersistent(), false);

    try {/* w  w w.  j  av  a  2 s. c o  m*/
        sender.init();

        // make sure the destination is empty
        Assert.assertNull(JmsHelper.readMessage(factory, 500, queue));

        // NON-PERSISTENT delivery
        final org.perfcake.message.Message message = new org.perfcake.message.Message();
        final String payload1 = "Hello World!";
        message.setPayload(payload1);
        sender.preSend(message, null, null);
        sender.send(message, null);
        sender.postSend(message);

        final Message response = JmsHelper.readMessage(factory, 500, queue);
        Assert.assertEquals(response.getJMSDeliveryMode(), DeliveryMode.NON_PERSISTENT);
        Assert.assertTrue(response instanceof TextMessage);
        Assert.assertEquals(((TextMessage) response).getText(), payload1);

        // make sure the destination is empty
        Assert.assertNull(JmsHelper.readMessage(factory, 500, queue));
    } finally {
        sender.close();
    }
}

From source file:org.springframework.jms.support.converter.SimpleJmsHeaderMapper.java

@Override
public Map<String, Object> toHeaders(javax.jms.Message jmsMessage) {
    Map<String, Object> headers = new HashMap<String, Object>();
    try {//from   ww  w.jav a 2s .  c  o  m
        try {
            String correlationId = jmsMessage.getJMSCorrelationID();
            if (correlationId != null) {
                headers.put(JmsHeaders.CORRELATION_ID, correlationId);
            }
        } catch (Exception e) {
            logger.info("failed to read JMSCorrelationID property, skipping", e);
        }
        try {
            Destination destination = jmsMessage.getJMSDestination();
            if (destination != null) {
                headers.put(JmsHeaders.DESTINATION, destination);
            }
        } catch (Exception e) {
            logger.info("failed to read JMSDestination property, skipping", e);
        }
        try {
            int deliveryMode = jmsMessage.getJMSDeliveryMode();
            headers.put(JmsHeaders.DELIVERY_MODE, deliveryMode);
        } catch (Exception e) {
            logger.info("failed to read JMSDeliveryMode property, skipping", e);
        }
        try {
            long expiration = jmsMessage.getJMSExpiration();
            headers.put(JmsHeaders.EXPIRATION, expiration);
        } catch (Exception e) {
            logger.info("failed to read JMSExpiration property, skipping", e);
        }
        try {
            String messageId = jmsMessage.getJMSMessageID();
            if (messageId != null) {
                headers.put(JmsHeaders.MESSAGE_ID, messageId);
            }
        } catch (Exception e) {
            logger.info("failed to read JMSMessageID property, skipping", e);
        }
        try {
            headers.put(JmsHeaders.PRIORITY, jmsMessage.getJMSPriority());
        } catch (Exception e) {
            logger.info("failed to read JMSPriority property, skipping", e);
        }
        try {
            Destination replyTo = jmsMessage.getJMSReplyTo();
            if (replyTo != null) {
                headers.put(JmsHeaders.REPLY_TO, replyTo);
            }
        } catch (Exception e) {
            logger.info("failed to read JMSReplyTo property, skipping", e);
        }
        try {
            headers.put(JmsHeaders.REDELIVERED, jmsMessage.getJMSRedelivered());
        } catch (Exception e) {
            logger.info("failed to read JMSRedelivered property, skipping", e);
        }
        try {
            String type = jmsMessage.getJMSType();
            if (type != null) {
                headers.put(JmsHeaders.TYPE, type);
            }
        } catch (Exception e) {
            logger.info("failed to read JMSType property, skipping", e);
        }
        try {
            headers.put(JmsHeaders.TIMESTAMP, jmsMessage.getJMSTimestamp());
        } catch (Exception e) {
            logger.info("failed to read JMSTimestamp property, skipping", e);
        }

        Enumeration<?> jmsPropertyNames = jmsMessage.getPropertyNames();
        if (jmsPropertyNames != null) {
            while (jmsPropertyNames.hasMoreElements()) {
                String propertyName = jmsPropertyNames.nextElement().toString();
                try {
                    String headerName = this.toHeaderName(propertyName);
                    headers.put(headerName, jmsMessage.getObjectProperty(propertyName));
                } catch (Exception e) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("error occurred while mapping JMS property '" + propertyName
                                + "' to Message header", e);
                    }
                }
            }
        }
    } catch (JMSException e) {
        if (logger.isWarnEnabled()) {
            logger.warn("error occurred while mapping from JMS properties to MessageHeaders", e);
        }
    }
    return headers;
}

From source file:org.springframework.jms.support.SimpleJmsHeaderMapper.java

@Override
public MessageHeaders toHeaders(javax.jms.Message jmsMessage) {
    Map<String, Object> headers = new HashMap<String, Object>();
    try {//from  w w  w  .ja  v  a2  s.  c o  m
        try {
            String correlationId = jmsMessage.getJMSCorrelationID();
            if (correlationId != null) {
                headers.put(JmsHeaders.CORRELATION_ID, correlationId);
            }
        } catch (Exception ex) {
            logger.info("failed to read JMSCorrelationID property, skipping", ex);
        }
        try {
            Destination destination = jmsMessage.getJMSDestination();
            if (destination != null) {
                headers.put(JmsHeaders.DESTINATION, destination);
            }
        } catch (Exception ex) {
            logger.info("failed to read JMSDestination property, skipping", ex);
        }
        try {
            int deliveryMode = jmsMessage.getJMSDeliveryMode();
            headers.put(JmsHeaders.DELIVERY_MODE, deliveryMode);
        } catch (Exception ex) {
            logger.info("failed to read JMSDeliveryMode property, skipping", ex);
        }
        try {
            long expiration = jmsMessage.getJMSExpiration();
            headers.put(JmsHeaders.EXPIRATION, expiration);
        } catch (Exception ex) {
            logger.info("failed to read JMSExpiration property, skipping", ex);
        }
        try {
            String messageId = jmsMessage.getJMSMessageID();
            if (messageId != null) {
                headers.put(JmsHeaders.MESSAGE_ID, messageId);
            }
        } catch (Exception ex) {
            logger.info("failed to read JMSMessageID property, skipping", ex);
        }
        try {
            headers.put(JmsHeaders.PRIORITY, jmsMessage.getJMSPriority());
        } catch (Exception ex) {
            logger.info("failed to read JMSPriority property, skipping", ex);
        }
        try {
            Destination replyTo = jmsMessage.getJMSReplyTo();
            if (replyTo != null) {
                headers.put(JmsHeaders.REPLY_TO, replyTo);
            }
        } catch (Exception ex) {
            logger.info("failed to read JMSReplyTo property, skipping", ex);
        }
        try {
            headers.put(JmsHeaders.REDELIVERED, jmsMessage.getJMSRedelivered());
        } catch (Exception ex) {
            logger.info("failed to read JMSRedelivered property, skipping", ex);
        }
        try {
            String type = jmsMessage.getJMSType();
            if (type != null) {
                headers.put(JmsHeaders.TYPE, type);
            }
        } catch (Exception ex) {
            logger.info("failed to read JMSType property, skipping", ex);
        }
        try {
            headers.put(JmsHeaders.TIMESTAMP, jmsMessage.getJMSTimestamp());
        } catch (Exception ex) {
            logger.info("failed to read JMSTimestamp property, skipping", ex);
        }

        Enumeration<?> jmsPropertyNames = jmsMessage.getPropertyNames();
        if (jmsPropertyNames != null) {
            while (jmsPropertyNames.hasMoreElements()) {
                String propertyName = jmsPropertyNames.nextElement().toString();
                try {
                    String headerName = this.toHeaderName(propertyName);
                    headers.put(headerName, jmsMessage.getObjectProperty(propertyName));
                } catch (Exception ex) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Error occurred while mapping JMS property '" + propertyName
                                + "' to Message header", ex);
                    }
                }
            }
        }
    } catch (JMSException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Error occurred while mapping from JMS properties to MessageHeaders", ex);
        }
    }
    return new MessageHeaders(headers);
}