Example usage for javax.jms MessageProducer send

List of usage examples for javax.jms MessageProducer send

Introduction

In this page you can find the example usage for javax.jms MessageProducer send.

Prototype


void send(Message message, int deliveryMode, int priority, long timeToLive) throws JMSException;

Source Link

Document

Sends a message, specifying delivery mode, priority, and time to live.

Usage

From source file:org.dawnsci.commandserver.core.util.JSONUtils.java

/**
 * Generic way of sending a topic notification
 * @param connection - does not get closed afterwards nust be started before.
 * @param message// w ww . j  av  a 2s.c o  m
 * @param topicName
 * @param uri
 * @throws Exception
 */
private static final void sendTopic(Connection connection, Object message, String topicName, URI uri)
        throws Exception {

    // JMS messages are sent and received using a Session. We will
    // create here a non-transactional session object. If you want
    // to use transactions you should set the first parameter to 'true'
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    try {
        Topic topic = session.createTopic(topicName);

        MessageProducer producer = session.createProducer(topic);

        final ObjectMapper mapper = new ObjectMapper();

        // Here we are sending the message out to the topic
        TextMessage temp = session.createTextMessage(mapper.writeValueAsString(message));
        producer.send(temp, DeliveryMode.NON_PERSISTENT, 1, 5000);

    } finally {
        session.close();
    }
}

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

public void send(MessageProducer producer, Message message, boolean persistent, int priority, long ttl,
        boolean topic, ImmutableEndpoint endpoint) throws JMSException {
    producer.send(message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), priority, ttl);
}

From source file:org.springframework.integration.jms.ChannelPublishingJmsMessageListener.java

private void sendReply(javax.jms.Message replyMessage, Destination destination, Session session)
        throws JMSException {
    MessageProducer producer = session.createProducer(destination);
    try {/*w  w w. j a v  a2s  .c om*/
        if (this.explicitQosEnabledForReplies) {
            producer.send(replyMessage, this.replyDeliveryMode, this.replyPriority, this.replyTimeToLive);
        } else {
            producer.send(replyMessage);
        }
    } finally {
        JmsUtils.closeMessageProducer(producer);
    }
}

From source file:com.eviware.soapui.impl.wsdl.submit.transports.jms.HermesJmsRequestTransport.java

private Message send(SubmitContext submitContext, Request request, Hermes hermes,
        MessageProducer messageProducer, Message message, Destination replyToDestination) throws JMSException {
    JMSHeader jmsHeader = createJMSHeader(submitContext, request, hermes, message, replyToDestination);
    messageProducer.send(message, message.getJMSDeliveryMode(), message.getJMSPriority(),
            jmsHeader.getTimeTolive());/*from  w  w  w . j av  a  2s  .c  o  m*/
    submitContext.setProperty(JMS_MESSAGE_SEND, message);
    return message;
}

From source file:me.norman.maurer.james.queue.HornetQMailQueue.java

@Override
protected void produceMail(Session session, Map<String, Object> props, int msgPrio, Mail mail)
        throws JMSException, MessagingException, IOException {
    MessageProducer producer = null;
    try {// w  w  w  .  jav  a  2s.  c o  m
        BytesMessage message = session.createBytesMessage();
        Iterator<String> propIt = props.keySet().iterator();
        while (propIt.hasNext()) {
            String key = propIt.next();
            message.setObjectProperty(key, props.get(key));
        }
        // set the stream to read frome
        message.setObjectProperty("JMS_HQ_InputStream",
                new BufferedInputStream(new MimeMessageInputStream(mail.getMessage())));
        Queue q = session.createQueue(queuename);
        producer = session.createProducer(q);
        producer.send(message, Message.DEFAULT_DELIVERY_MODE, msgPrio, Message.DEFAULT_TIME_TO_LIVE);
    } finally {
        if (producer != null) {
            producer.close();
        }
    }
}

From source file:org.dawnsci.commandserver.core.producer.AliveConsumer.java

protected void startNotifications() throws Exception {

    if (uri == null)
        throw new NullPointerException("Please set the URI before starting notifications!");
    this.cbean = new ConsumerBean();
    cbean.setStatus(ConsumerStatus.STARTING);
    cbean.setName(getName());/*from  w  w w .  j ava  2 s .c o  m*/
    cbean.setConsumerId(consumerId);
    cbean.setVersion(consumerVersion);
    cbean.setStartTime(System.currentTimeMillis());
    try {
        cbean.setHostName(InetAddress.getLocalHost().getHostName());
    } catch (UnknownHostException e) {
        // Not fatal but would be nice...
        e.printStackTrace();
    }

    System.out.println("Running events on topic " + Constants.ALIVE_TOPIC + " to notify of '" + getName()
            + "' service being available.");

    final Thread aliveThread = new Thread(new Runnable() {
        public void run() {

            try {
                ConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri);
                aliveConnection = connectionFactory.createConnection();
                aliveConnection.start();

                final Session session = aliveConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                final Topic topic = session.createTopic(Constants.ALIVE_TOPIC);
                final MessageProducer producer = session.createProducer(topic);

                final ObjectMapper mapper = new ObjectMapper();

                // Here we are sending the message out to the topic
                while (isActive()) {
                    try {
                        Thread.sleep(Constants.NOTIFICATION_FREQUENCY);

                        TextMessage temp = session.createTextMessage(mapper.writeValueAsString(cbean));
                        producer.send(temp, DeliveryMode.NON_PERSISTENT, 1, 5000);

                        if (ConsumerStatus.STARTING.equals(cbean.getStatus())) {
                            cbean.setStatus(ConsumerStatus.RUNNING);
                        }

                    } catch (InterruptedException ne) {
                        break;
                    } catch (Exception neOther) {
                        neOther.printStackTrace();
                    }
                }
            } catch (Exception ne) {
                ne.printStackTrace();
                setActive(false);
            }
        }
    });
    aliveThread.setName("Alive Notification Topic");
    aliveThread.setDaemon(true);
    aliveThread.setPriority(Thread.MIN_PRIORITY);
    aliveThread.start();
}

From source file:org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener.java

/**
 * Send the given response message to the given destination.
 * @param response the JMS message to send
 * @param destination the JMS destination to send to
 * @param session the JMS session to operate on
 * @throws JMSException if thrown by JMS API methods
 * @see #postProcessProducer//from   ww  w  . jav  a  2  s.c o  m
 * @see javax.jms.Session#createProducer
 * @see javax.jms.MessageProducer#send
 */
protected void sendResponse(Session session, Destination destination, Message response) throws JMSException {
    MessageProducer producer = session.createProducer(destination);
    try {
        postProcessProducer(producer, response);
        QosSettings settings = getResponseQosSettings();
        if (settings != null) {
            producer.send(response, settings.getDeliveryMode(), settings.getPriority(),
                    settings.getTimeToLive());
        } else {
            producer.send(response);
        }
    } finally {
        JmsUtils.closeMessageProducer(producer);
    }
}

From source file:org.springframework.jms.core.JmsTemplate.java

protected void doSend(MessageProducer producer, Message message) throws JMSException {
    if (isExplicitQosEnabled()) {
        producer.send(message, getDeliveryMode(), getPriority(), getTimeToLive());
    } else {/*www  .  jav  a 2s  .  c  om*/
        producer.send(message);
    }
}

From source file:org.apache.activemq.usecases.ConcurrentProducerDurableConsumerTest.java

/**
 * @return max and ave send time/*from ww w  . j  av a2s .  c o m*/
 * @throws Exception
 */
private double[] produceMessages(Destination destination, final int toSend, final int numIterations,
        Session session, MessageProducer producer, Object addConsumerSignal) throws Exception {
    long start;
    long count = 0;
    double batchMax = 0, max = 0, sum = 0;
    for (int i = 0; i < numIterations; i++) {
        start = System.currentTimeMillis();
        for (int j = 0; j < toSend; j++) {
            long singleSendstart = System.currentTimeMillis();
            TextMessage msg = createTextMessage(session, "" + j);
            // rotate
            int priority = ((int) count % 10);
            producer.send(msg, DeliveryMode.PERSISTENT, priority, 0);
            max = Math.max(max, (System.currentTimeMillis() - singleSendstart));
            if (++count % 500 == 0) {
                if (addConsumerSignal != null) {
                    synchronized (addConsumerSignal) {
                        addConsumerSignal.notifyAll();
                        LOG.info("Signalled add consumer");
                    }
                }
            }
            ;
            if (count % 5000 == 0) {
                LOG.info("Sent " + count + ", singleSendMax:" + max);
            }

        }
        long duration = System.currentTimeMillis() - start;
        batchMax = Math.max(batchMax, duration);
        sum += duration;
        LOG.info("Iteration " + i + ", sent " + toSend + ", time: " + duration + ", batchMax:" + batchMax
                + ", singleSendMax:" + max);
    }

    LOG.info("Sent: " + toSend * numIterations + ", batchMax: " + batchMax + " singleSendMax: " + max);
    return new double[] { batchMax, sum / numIterations };
}

From source file:com.ccc.ccm.client.JMSTemplateAutowired.java

/**
* Actually send the given JMS message.//from  ww w. j av a2s.  co  m
* @param producer the JMS MessageProducer to send with
* @param message the JMS Message to send
* @throws JMSException if thrown by JMS API methods
*/
protected void doSend(MessageProducer producer, Message message) throws JMSException {
    if (isExplicitQosEnabled()) {
        producer.send(message, getDeliveryMode(), getPriority(), getTimeToLive());
    } else {
        producer.send(message);
    }
}