Example usage for javax.jms TextMessage setJMSPriority

List of usage examples for javax.jms TextMessage setJMSPriority

Introduction

In this page you can find the example usage for javax.jms TextMessage setJMSPriority.

Prototype


void setJMSPriority(int priority) throws JMSException;

Source Link

Document

Sets the priority level for this message.

Usage

From source file:org.dawnsci.commandserver.core.consumer.RemoteSubmission.java

/**
 * Submits the bean onto the server. From there events about this
 * bean are tacked by monitoring the status queue.
 * /*from  w  w  w.j  a  v  a2  s.c  om*/
 * @param uri
 * @param bean
 */
public synchronized TextMessage submit(StatusBean bean, boolean prepareBean) throws Exception {

    if (getQueueName() == null || "".equals(getQueueName()))
        throw new Exception("Please specify a queue name!");

    Connection send = null;
    Session session = null;
    MessageProducer producer = null;

    try {
        QueueConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri);
        send = connectionFactory.createConnection();

        session = send.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(queueName);

        producer = session.createProducer(queue);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);

        ObjectMapper mapper = new ObjectMapper();

        if (getTimestamp() < 1)
            setTimestamp(System.currentTimeMillis());
        if (getPriority() < 1)
            setPriority(1);
        if (getLifeTime() < 1)
            setLifeTime(7 * 24 * 60 * 60 * 1000); // 7 days in ms

        if (prepareBean) {
            if (bean.getUserName() == null)
                bean.setUserName(System.getProperty("user.name"));
            bean.setUniqueId(uniqueId);
            bean.setSubmissionTime(getTimestamp());
        }
        String jsonString = mapper.writeValueAsString(bean);

        TextMessage message = session.createTextMessage(jsonString);

        message.setJMSMessageID(uniqueId);
        message.setJMSExpiration(getLifeTime());
        message.setJMSTimestamp(getTimestamp());
        message.setJMSPriority(getPriority());

        producer.send(message);

        return message;

    } finally {
        if (send != null)
            send.close();
        if (session != null)
            session.close();
        if (producer != null)
            producer.close();
    }

}

From source file:nl.nn.adapterframework.jms.JMSFacade.java

public String send(Session session, Destination dest, String correlationId, String message, String messageType,
        long timeToLive, int deliveryMode, int priority, boolean ignoreInvalidDestinationException,
        Map properties) throws NamingException, JMSException, SenderException {
    TextMessage msg = createTextMessage(session, correlationId, message);
    MessageProducer mp;/*ww w.  j av  a 2 s . co  m*/
    try {
        if (useJms102()) {
            if ((session instanceof TopicSession) && (dest instanceof Topic)) {
                mp = getTopicPublisher((TopicSession) session, (Topic) dest);
            } else {
                if ((session instanceof QueueSession) && (dest instanceof Queue)) {
                    mp = getQueueSender((QueueSession) session, (Queue) dest);
                } else {
                    throw new SenderException(
                            "classes of Session [" + session.getClass().getName() + "] and Destination ["
                                    + dest.getClass().getName() + "] do not match (Queue vs Topic)");
                }
            }
        } else {
            mp = session.createProducer(dest);
        }
    } catch (InvalidDestinationException e) {
        if (ignoreInvalidDestinationException) {
            log.warn("queue [" + dest + "] doesn't exist");
            return null;
        } else {
            throw e;
        }
    }
    if (messageType != null) {
        msg.setJMSType(messageType);
    }
    if (deliveryMode > 0) {
        msg.setJMSDeliveryMode(deliveryMode);
        mp.setDeliveryMode(deliveryMode);
    }
    if (priority >= 0) {
        msg.setJMSPriority(priority);
        mp.setPriority(priority);
    }
    if (timeToLive > 0) {
        mp.setTimeToLive(timeToLive);
    }
    if (properties != null) {
        for (Iterator it = properties.keySet().iterator(); it.hasNext();) {
            String key = (String) it.next();
            Object value = properties.get(key);
            log.debug("setting property [" + name + "] to value [" + value + "]");
            msg.setObjectProperty(key, value);
        }
    }
    String result = send(mp, msg, ignoreInvalidDestinationException);
    mp.close();
    return result;
}

From source file:org.openanzo.combus.bayeux.BridgeConnectionManager.java

/**
 * Send a JMS message on behalf of the given client to a specific destination. The destination is a string that names an abstract queue such as that in
 * Constants.NOTIFICATION_SERVICE_QUEUE, etc.
 * /*  w ww .ja va2  s. co  m*/
 * @param clientId
 * @param destination
 * @param messageProperties
 * @param msgBody
 * @return returns whether or not this message was published to a topic
 * @throws JMSException
 * @throws AnzoException
 */
protected boolean sendClientMessage(String clientId, AnzoPrincipal principal, String destination,
        Map<?, ?> messageProperties, String msgBody, IOperationContext opContext)
        throws JMSException, AnzoException {
    //long destinationProfiler = profiler.start("Resolving destination.");
    Destination dest = destinations.get(destination);
    //profiler.stop(destinationProfiler);
    if (dest == null && destination.startsWith("services/")) {
        dest = session.createQueue(destination);
        destinations.put(destination, dest);
    }
    if (dest == null) { // we probably have a statement channel
        //long nullDestProfiler = profiler.start("Sending client message with null destination.");
        if (destination == null || !destination.startsWith(NAMESPACES.STREAM_TOPIC_PREFIX)) {
            //profiler.stop(nullDestProfiler);
            throw new AnzoException(ExceptionConstants.COMBUS.INVALID_TOPIC, destination);
        }
        // first we have to get the named graph uri out of the statement channel topic.
        String uri = UriGenerator.stripEncapsulatedString(NAMESPACES.STREAM_TOPIC_PREFIX, destination);
        URI graphUri = Constants.valueFactory.createURI(uri);
        if (!userHasGraphAddAccess(graphUri, principal, opContext)) {
            //profiler.stop(nullDestProfiler);
            throw new AnzoException(ExceptionConstants.COMBUS.NOT_AUTHORIZED_FOR_TOPIC,
                    opContext.getOperationPrincipal().getUserURI().toString(), destination);
        }
        Topic topic = session.createTopic(destination);

        TextMessage tmsg = session.createTextMessage();
        for (Map.Entry<?, ?> prop : messageProperties.entrySet()) {
            tmsg.setStringProperty(prop.getKey().toString(), prop.getValue().toString());
        }
        tmsg.setText(msgBody);
        mp.send(topic, tmsg);
        //profiler.stop(nullDestProfiler);
        return true;
    } else {
        TemporaryTopic tempTopicForReply;
        //long = clientStateProfiler = profiler.start("Obtaining Bayeux client state.");
        mapLock.lock();
        try {
            ClientState state = clientIdToClientState.get(clientId);
            if (state == null) {
                throw new AnzoException(ExceptionConstants.CLIENT.CLIENT_NOT_CONNECTED);
            }
            tempTopicForReply = state.topic;
        } finally {
            mapLock.unlock();
            //profiler.stop(clientStateProfiler);
        }

        //long prepareJmsProfiler = profiler.start("Preparing JMS Message.");
        TextMessage tmsg = session.createTextMessage();
        int priority = 4;
        for (Map.Entry<?, ?> prop : messageProperties.entrySet()) {
            if (JMS_MSG_PROPERTY_CORRELATION_ID.equals(prop.getKey())) {
                tmsg.setJMSCorrelationID(prop.getValue().toString());
            }
            if (JMS_MSG_PROPERTY_PRIORITY.equals(prop.getKey())) {
                priority = Integer.parseInt(prop.getValue().toString());
            } else {
                tmsg.setStringProperty(prop.getKey().toString(), prop.getValue().toString());
            }
        }
        tmsg.setJMSPriority(priority);
        tmsg.setJMSReplyTo(tempTopicForReply);
        tmsg.setText(msgBody);
        String username = principal.getName();
        tmsg.setStringProperty("runAsUser", username);
        //profiler.stop(prepareJmsProfiler);
        long sendJmsProfiler = profiler.start("Sending JMS Message");
        mp.setPriority(priority);
        mp.send(dest, tmsg);
        profiler.stop(sendJmsProfiler);
        return false;
    }

}