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.apache.qpid.disttest.jms.ClientJmsDelegate.java

public Message sendNextMessage(final CreateProducerCommand command) {
    Message sentMessage = null;/*w  ww. j  ava 2s.c  om*/
    MessageProvider messageProvider = _testMessageProviders.get(command.getMessageProviderName());
    if (messageProvider == null) {
        messageProvider = _defaultMessageProvider;
    }

    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.frameworkset.mq.RequestDispatcher.java

public void send(int destinationType, String destination_, Message message, boolean persistent, int priority,
        long timeToLive, JMSProperties properties) throws JMSException {

    assertStarted();//from   ww  w .  j  a  v a 2s  .com
    MessageProducer producer = null;
    try {
        Destination destination = null;
        //         if (destinationType == MQUtil.TYPE_QUEUE)
        //         {
        //            destination = session.createQueue(destination_);
        //         }
        //         else
        //         {
        //            destination = session.createTopic(destination_);
        //         }

        LOG.debug("send message to " + destination_ + " build destination");
        destination = connection.createDestination(session, destination_, destinationType);
        LOG.debug("send message to " + destination_ + " build destination end.");

        int deliveryMode = persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT;
        producer = session.createProducer(destination);
        if (properties != null)
            MQUtil.initMessage(message, properties);
        producer.send(message, deliveryMode, priority, timeToLive);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sent! to destination: " + destination + " message: " + message);
        }
        message = null;
    } catch (JMSException e) {
        throw e;
    } catch (Exception e) {
        throw new JMSException(e.getMessage());
    } finally {
        if (producer != null)
            try {
                producer.close();
            } catch (Exception e) {

            }

    }
}

From source file:org.frameworkset.mq.RequestDispatcher.java

public void send(String msg, JMSProperties properties) throws JMSException {

    assertStarted();// w  w  w.  jav a  2 s .c o  m
    MessageProducer producer = null;
    try {
        Destination destination = null;

        //         if (this.destinationType == MQUtil.TYPE_QUEUE)
        //         {
        //            destination = session.createQueue(this.destination);
        //         }
        //         else
        //         {
        //            destination = session.createTopic(this.destination);
        //         }

        LOG.debug("send message to " + this.destination + " build destination");
        destination = connection.createDestination(session, this.destination, destinationType);
        LOG.debug("send message to " + this.destination + " build destination end.");

        int deliveryMode = this.persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT;
        producer = session.createProducer(destination);
        Message message = createTextMessage(msg);
        if (properties != null)
            MQUtil.initMessage(message, properties);
        producer.send(message, deliveryMode, this.priovity, this.timetolive);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sent! to destination: " + destination + " message: " + message);
        }
    } catch (JMSException e) {
        throw e;
    } catch (Exception e) {
        throw new JMSException(e.getMessage());
    } finally {
        if (producer != null)
            try {
                producer.close();
            } catch (Exception e) {

            }

    }

}

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

private void sendRequestMessage(javax.jms.Message jmsRequest, MessageProducer messageProducer, int priority)
        throws JMSException {
    if (this.explicitQosEnabled) {
        messageProducer.send(jmsRequest, this.deliveryMode, priority, this.timeToLive);
    } else {/*from  w  w w . ja va 2  s.  c o m*/
        messageProducer.send(jmsRequest);
    }
}

From source file:com.fusesource.forge.jmstest.tests.AsyncProducer.java

public void run() {

    try {/*from   www .ja  v a  2s . co  m*/
        Connection conn = getConnection();
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

        if (isExpectReply()) {
            Destination replyTo = getDestinationProvider().getDestination(session, getReplyDestination());
            MessageConsumer consumer = session.createConsumer(replyTo);
            consumer.setMessageListener(this);
        }

        Destination dest = getDestinationProvider().getDestination(session, getDestinationName());
        MessageProducer producer = session.createProducer(dest);
        producer.setDeliveryMode(getDeliveryMode().getCode());
        getConnection().start();

        LOG.info(">>Starting Message send loop");
        while (!done.get()) {
            try {
                locallySent++;
                Destination replyDest = null;
                Message msg = getMessageFactory().createMessage(session);
                if (getMsgGroup() != null) {
                    LOG.debug("Setting message group to : " + getMsgGroup());
                    msg.setStringProperty("JMSXGroupID", getMsgGroup());
                    if (getMessagesToSend() > 0) {
                        if (locallySent == getMessagesToSend()) {
                            LOG.debug("Closing message group: " + getMsgGroup());
                            msg.setIntProperty("JMSXGroupSeq", 0);
                        }
                    }
                }
                msg.setLongProperty("MsgNr", locallySent);
                if (isExpectReply()) {
                    corrId = getReplyDestination() + "Seq-" + locallySent;
                    msg.setStringProperty("JMSCorrelationID", corrId);
                    replyDest = getDestinationProvider().getDestination(session, getReplyDestination());
                    msg.setJMSReplyTo(replyDest);
                    receivedResponse = false;
                }
                long sendTime = System.currentTimeMillis();
                producer.send(msg, deliveryMode.getCode(), 4, ttl);
                if (sent != null) {
                    sent.incrementAndGet();
                }
                done.set((getMessagesToSend() > 0) && ((locallySent) == getMessagesToSend()));
                if (isExpectReply()) {
                    try {
                        LOG.debug("Waiting for response ...");
                        synchronized (corrId) {
                            try {
                                if (getReplyTimeOut() > 0) {
                                    corrId.wait(getReplyTimeOut());
                                } else {
                                    corrId.wait();
                                }
                            } catch (InterruptedException ie) {
                            }
                            if (receivedResponse) {
                                long duration = System.currentTimeMillis() - sendTime;
                                LOG.debug("Got response from peer in " + duration + " ms");
                            } else {
                                LOG.error("Response not received within time frame...");
                                if (timeOuts != null) {
                                    timeOuts.incrementAndGet();
                                }
                            }
                        }
                    } catch (Exception e) {
                        if (exceptions != null) {
                            exceptions.incrementAndGet();
                        }
                    }
                }
                if (sleep > 0L) {
                    try {
                        Thread.sleep(sleep);
                    } catch (InterruptedException ie) {
                    }
                }
            } catch (JMSException e) {
                if (exceptions != null) {
                    exceptions.incrementAndGet();
                }
            }
        }
    } catch (Exception e) {
    } finally {
        try {
            closeConnection();
        } catch (Throwable e) {
        }
    }
    LOG.info(">>MessageSender done...(" + sent + ")");
}

From source file:org.apache.servicemix.jms.endpoints.AbstractConsumerEndpoint.java

protected void send(Message msg, Session session, Destination dest) throws JMSException {
    MessageProducer producer;
    if (isJms102()) {
        if (isPubSubDomain()) {
            producer = ((TopicSession) session).createPublisher((Topic) dest);
        } else {/*from w  ww  .j  av  a  2s  . c o m*/
            producer = ((QueueSession) session).createSender((Queue) dest);
        }
    } else {
        producer = session.createProducer(dest);
    }
    try {
        if (replyProperties != null) {
            for (Map.Entry<String, Object> e : replyProperties.entrySet()) {
                msg.setObjectProperty(e.getKey(), e.getValue());
            }
        }
        if (isJms102()) {
            if (isPubSubDomain()) {
                if (replyExplicitQosEnabled) {
                    ((TopicPublisher) producer).publish(msg, replyDeliveryMode, replyPriority, replyTimeToLive);
                } else {
                    ((TopicPublisher) producer).publish(msg);
                }
            } else {
                if (replyExplicitQosEnabled) {
                    ((QueueSender) producer).send(msg, replyDeliveryMode, replyPriority, replyTimeToLive);
                } else {
                    ((QueueSender) producer).send(msg);
                }
            }
        } else {
            if (replyExplicitQosEnabled) {
                producer.send(msg, replyDeliveryMode, replyPriority, replyTimeToLive);
            } else {
                producer.send(msg);
            }
        }
    } finally {
        JmsUtils.closeMessageProducer(producer);
    }
}

From source file:org.frameworkset.mq.RequestDispatcher.java

public void send(int destinationType, String destination_, boolean persistent, int priority, long timeToLive,
        Message message, JMSProperties properties) throws JMSException {

    LOG.debug("send message to " + destination_ + " assertStarted(),message=" + message);
    assertStarted();/*from  ww w.  j  av a  2  s. c o m*/
    MessageProducer producer = null;
    try {
        Destination destination = null;
        //         destinationType = JMSConnectionFactory.evaluateDestinationType(destination_, destinationType);

        //         destination_ = JMSConnectionFactory.evaluateDestination(destination_);
        //         boolean isqueue = destinationType == MQUtil.TYPE_QUEUE;
        //         if (isqueue)
        //         {
        //            LOG.debug("send message to " + destination_
        //                  + " build QUEUE destination");
        //            destination = session.createQueue(destination_);
        //            LOG.debug("send message to " + destination_
        //                  + " build QUEUE destination end");
        //         }
        //         else
        //         {
        //            LOG.debug("send message to " + destination_
        //                  + " build Topic destination");
        //            destination = session.createTopic(destination_);
        //            LOG.debug("send message to " + destination_
        //                  + " build Topic destination end");
        //         }
        LOG.debug("send message to " + destination_ + " build destination");
        destination = connection.createDestination(session, destination_, destinationType);
        LOG.debug("send message to " + destination_ + " build destination end.");
        int deliveryMode = persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT;
        LOG.debug("send message to " + destination_ + " this.client.isPersistent =" + persistent);
        LOG.debug("send message to " + destination + " send started....");
        producer = session.createProducer(destination);
        if (properties != null)
            MQUtil.initMessage(message, properties);
        producer.send(message, deliveryMode, priority, timeToLive);
        LOG.debug("send message to " + destination + " send end....");
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sent! to destination: " + destination + " message: " + message);
        }
    } catch (JMSException e) {
        throw e;
    } catch (Exception e) {
        throw new JMSException(e.getMessage());
    } finally {
        if (producer != null)
            try {
                producer.close();
            } catch (Exception e) {

            }

    }
}

From source file:org.frameworkset.mq.RequestDispatcher.java

public void send(int destinationType, String destination_, boolean persistent, int priority, long timeToLive,
        Message message, Logger step, JMSProperties properties) throws JMSException {
    if (step != null)
        step.logBasic("send message to " + destination_ + " assertStarted(),message=" + message);
    assertStarted();// ww  w  .ja  v a2s.  c o m
    MessageProducer producer = null;
    try {
        Destination destination = null;
        //         boolean isqueue = destinationType == MQUtil.TYPE_QUEUE;
        //         if (isqueue)
        //         {
        //             if(step != null)
        //                  step.logBasic("send message to " + destination_
        //                  + " build QUEUE destination");
        //            destination = session.createQueue(destination_);
        //            if(step != null)
        //                  step.logBasic("send message to " + destination_
        //                  + " build QUEUE destination end");
        //         }
        //         else
        //         {
        //             if(step != null)
        //                  step.logBasic("send message to " + destination_
        //                  + " build Topic destination");
        //            destination = session.createTopic(destination_);
        //            if(step != null)
        //                  step.logBasic("send message to " + destination_
        //                  + " build Topic destination end");
        //         }
        if (step != null)
            step.logBasic("send message to " + destination_ + " build destination.");
        destination = this.connection.createDestination(session, destination_, destinationType);
        if (step != null)
            step.logBasic("send message to " + destination_ + " build destination end");
        int deliveryMode = persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT;
        if (step != null)
            step.logBasic("send message to " + destination_ + " this.client.isPersistent =" + persistent);
        if (step != null)
            step.logBasic("send message to " + destination + " send started....");
        producer = session.createProducer(destination);
        if (properties != null)
            MQUtil.initMessage(message, properties);
        producer.send(message, deliveryMode, priority, timeToLive);
        if (step != null)
            step.logBasic("send message to " + destination + " send end....");
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sent! to destination: " + destination + " message: " + message);
        }
    } catch (JMSException e) {
        throw e;
    } catch (Exception e) {
        throw new JMSException(e.getMessage());
    } finally {
        if (producer != null)
            try {
                producer.close();
            } catch (Exception e) {

            }

    }
}

From source file:DurableChat.java

public void DurableChatter(String broker, String username, String password) {
    javax.jms.MessageProducer publisher = null;
    javax.jms.MessageConsumer subscriber = null;
    javax.jms.Topic topic = null;/*from w  ww.jav a 2 s.c o  m*/

    //Create a connection:
    try {
        javax.jms.ConnectionFactory factory;
        factory = new ActiveMQConnectionFactory(username, password, broker);
        connection = factory.createConnection(username, password);

        //Durable Subscriptions are indexed by username, clientID and subscription name
        //It is a good practice to set the clientID:
        connection.setClientID(username);
        pubSession = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        subSession = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
    } catch (javax.jms.JMSException jmse) {
        System.err.println("Error: Cannot connect to Broker - " + broker);
        jmse.printStackTrace();
        System.exit(1);
    }

    //Create Publisher and Durable Subscriber:
    try {

        topic = pubSession.createTopic(APP_TOPIC);
        subscriber = subSession.createDurableSubscriber(topic, username);
        subscriber.setMessageListener(this);
        publisher = pubSession.createProducer(topic);
        connection.start();
    } catch (javax.jms.JMSException jmse) {
        System.out.println("Error: connection not started.");
        jmse.printStackTrace();
        System.exit(1);
    }

    //Wait for user input
    try {
        System.out.println("\nDurableChat application:\n" + "========================\n" + "The user "
                + username + " connects to the broker at " + DEFAULT_BROKER_NAME + ".\n"
                + "The application will publish messages to the " + APP_TOPIC + " topic.\n"
                + "The application also creates a durable subscription to that topic to consume any messages published there.\n\n"
                + "Type some text, and then press Enter to publish it as a TextMesssage from " + username
                + ".\n");
        java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
        while (true) {
            String s = stdin.readLine();

            if (s == null) {
                exit();
            } else if (s.length() > 0) {
                try {
                    javax.jms.TextMessage msg = pubSession.createTextMessage();
                    msg.setText(username + ": " + s);
                    //Publish the message persistantly:
                    publisher.send(msg, //message
                            javax.jms.DeliveryMode.PERSISTENT, //publish persistently
                            javax.jms.Message.DEFAULT_PRIORITY, //priority
                            MESSAGE_LIFESPAN); //Time to Live
                } catch (javax.jms.JMSException jmse) {
                    System.err.println("Error publishing message:" + jmse.getMessage());
                }
            }
        }
    } catch (java.io.IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:org.apache.james.queue.activemq.ActiveMQMailQueue.java

/**
 * Produce the mail to the JMS Queue//from   w  ww  .jav a2s .  c  o m
 */
protected void produceMail(Session session, Map<String, Object> props, int msgPrio, Mail mail)
        throws JMSException, MessagingException, IOException {
    MessageProducer producer = null;
    BlobMessage blobMessage = null;
    boolean reuse = false;

    try {

        // check if we should use a blob message here
        if (useBlob) {
            MimeMessage mm = mail.getMessage();
            MimeMessage wrapper = mm;

            ActiveMQSession amqSession = getAMQSession(session);

            /*
             * Remove this optimization as it could lead to problems when the same blob content
             * is shared across different messages. 
             * 
             * I still think it would be a good idea to somehow do this but at the moment it's just 
             * safer to disable it.
             * 
             * TODO: Re-Enable it again once it works!
             * 
             * See JAMES-1240
            if (wrapper instanceof MimeMessageCopyOnWriteProxy) {
            wrapper = ((MimeMessageCopyOnWriteProxy) mm).getWrappedMessage();
            }
                    
            if (wrapper instanceof MimeMessageWrapper) {
            URL blobUrl = (URL) mail.getAttribute(JAMES_BLOB_URL);
            String fromQueue = (String) mail.getAttribute(JAMES_QUEUE_NAME);
            MimeMessageWrapper mwrapper = (MimeMessageWrapper) wrapper;
                    
            if (blobUrl != null && fromQueue != null && mwrapper.isModified() == false) {
                // the message content was not changed so don't need to
                // upload it again and can just point to the url
                blobMessage = amqSession.createBlobMessage(blobUrl);
                reuse = true;
            }
                    
            }*/
            if (blobMessage == null) {
                // just use the MimeMessageInputStream which can read every
                // MimeMessage implementation
                blobMessage = amqSession.createBlobMessage(new MimeMessageInputStream(wrapper));
            }

            // store the queue name in the props
            props.put(JAMES_QUEUE_NAME, queuename);

            Queue queue = session.createQueue(queuename);

            producer = session.createProducer(queue);
            for (Map.Entry<String, Object> entry : props.entrySet()) {
                blobMessage.setObjectProperty(entry.getKey(), entry.getValue());
            }
            producer.send(blobMessage, Message.DEFAULT_DELIVERY_MODE, msgPrio, Message.DEFAULT_TIME_TO_LIVE);

        } else {
            super.produceMail(session, props, msgPrio, mail);
        }
    } catch (JMSException e) {
        if (!reuse && blobMessage != null && blobMessage instanceof ActiveMQBlobMessage) {
            ((ActiveMQBlobMessage) blobMessage).deleteFile();
        }
        throw e;
    } finally {

        try {
            if (producer != null)
                producer.close();
        } catch (JMSException e) {
            // ignore here
        }
    }

}