Example usage for org.springframework.jms.support JmsUtils closeMessageProducer

List of usage examples for org.springframework.jms.support JmsUtils closeMessageProducer

Introduction

In this page you can find the example usage for org.springframework.jms.support JmsUtils closeMessageProducer.

Prototype

public static void closeMessageProducer(@Nullable MessageProducer producer) 

Source Link

Document

Close the given JMS MessageProducer and ignore any thrown exception.

Usage

From source file:org.apache.flink.streaming.connectors.jms.JmsQueueSink.java

@Override
public void close() throws Exception {
    super.close();
    JmsUtils.closeMessageProducer(producer);
    JmsUtils.closeSession(session);//from  w  w  w  . ja  v  a2  s  .co  m
    JmsUtils.closeConnection(connection, true);
}

From source file:fr.xebia.springframework.jms.ManagedCachingConnectionFactoryTest.java

@Test
public void testMessageProducer() throws Exception {
    ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(
            "vm://localhost?broker.persistent=false&broker.useJmx=true");
    ManagedConnectionFactory connectionFactory = new ManagedConnectionFactory(activeMQConnectionFactory);
    Connection connection = null;
    Session session = null;// ww w  . j  av a 2  s .c  om
    MessageProducer messageProducer = null;
    try {
        connection = connectionFactory.createConnection();
        assertEquals(1, connectionFactory.getActiveConnectionCount());
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        assertEquals(1, connectionFactory.getActiveSessionCount());
        Destination myQueue = session.createQueue("test-queue");
        messageProducer = session.createProducer(myQueue);

        assertEquals(1, connectionFactory.getActiveMessageProducerCount());

        messageProducer.send(myQueue, session.createTextMessage("test"));

        assertEquals(0, connectionFactory.getActiveMessageConsumerCount());
    } finally {
        JmsUtils.closeMessageProducer(messageProducer);
        assertEquals(0, connectionFactory.getActiveMessageProducerCount());
        JmsUtils.closeSession(session);
        assertEquals(0, connectionFactory.getActiveSessionCount());
        JmsUtils.closeConnection(connection);
        assertEquals(0, connectionFactory.getActiveConnectionCount());
    }
}

From source file:fr.xebia.sample.springframework.jms.requestreply.RequestReplyClientInvoker.java

/**
 * Request/Reply SpringFramework sample.
 * /*from w w  w.j a v  a  2  s  . c o m*/
 * @param request
 *            sent to the remote service
 * @return reply returned by the remote service
 * @throws JMSException
 */
public String requestReply(String request) throws JMSException {

    Connection connection = connectionFactory.createConnection();
    try {
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        try {
            MessageProducer messageProducer = session.createProducer(this.requestDestination);
            try {
                Message requestMessage = session.createTextMessage(request);
                requestMessage.setJMSReplyTo(this.replyToDestination);
                // requestMessage.setJMSCorrelationID(String.valueOf(random.nextLong()));

                messageProducer.send(requestMessage);
                String messageSelector = "JMSCorrelationID  LIKE '" + requestMessage.getJMSMessageID() + "'";

                MessageConsumer messageConsumer = session.createConsumer(this.replyToDestination,
                        messageSelector);
                TextMessage replyMessage = (TextMessage) messageConsumer.receive(timeoutInMillis);
                Assert.notNull(replyMessage, "Timeout waiting for jms response");
                logger.debug(
                        "requestReply " + "\r\nrequest : " + requestMessage + "\r\nreply : " + replyMessage);
                String result = replyMessage.getText();
                logger.debug("requestReply('" + request + "'): '" + result + "'");
                return result;
            } finally {
                JmsUtils.closeMessageProducer(messageProducer);
            }
        } finally {
            JmsUtils.closeSession(session);
        }
    } finally {
        JmsUtils.closeConnection(connection);
    }
}

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

public <T> T execute(final Destination destination, final ProducerCallback<T> action) throws JmsException {
    Assert.notNull(action, "Callback object must not be null");
    return execute(new SessionCallback<T>() {
        public T doInJms(Session session) throws JMSException {
            MessageProducer producer = createProducer(session, destination);
            try {
                return action.doInJms(session, producer);
            } finally {
                JmsUtils.closeMessageProducer(producer);
            }//ww  w  . j  a v  a 2s . c om
        }
    }, false);
}

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

protected void send(Message msg, Session session, Destination dest) throws JMSException {
    MessageProducer producer;//from ww  w.  jav  a  2s  .  c  o m
    if (isJms102()) {
        if (isPubSubDomain()) {
            producer = ((TopicSession) session).createPublisher((Topic) dest);
        } else {
            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:com.ccc.ccm.client.JMSTemplateAutowired.java

public <T> T execute(final String destinationName, final ProducerCallback<T> action) throws JmsException {
    Assert.notNull(action, "Callback object must not be null");
    return execute(new SessionCallback<T>() {
        public T doInJms(Session session) throws JMSException {
            Destination destination = resolveDestinationName(session, destinationName);
            MessageProducer producer = createProducer(session, destination);
            try {
                return action.doInJms(session, producer);
            } finally {
                JmsUtils.closeMessageProducer(producer);
            }/*from w w  w  .  ja va 2 s . com*/
        }
    }, false);
}

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

/**
* Send the given JMS message.//w  w  w. ja v  a 2  s  .  c o  m
* @param session the JMS Session to operate on
* @param destination the JMS Destination to send to
* @param messageCreator callback to create a JMS Message
* @throws JMSException if thrown by JMS API methods
*/
protected void doSend(Session session, Destination destination, MessageCreator messageCreator)
        throws JMSException {

    Assert.notNull(messageCreator, "MessageCreator must not be null");
    MessageProducer producer = createProducer(session, destination);
    try {
        Message message = messageCreator.createMessage(session);
        if (logger.isDebugEnabled()) {
            logger.debug("Sending created message: " + message);
        }
        doSend(producer, message);
        // Check commit - avoid commit call within a JTA transaction.
        if (session.getTransacted() && isSessionLocallyTransacted(session)) {
            // Transacted session created by this template -> commit.
            JmsUtils.commitIfNecessary(session);
        }
    } finally {
        JmsUtils.closeMessageProducer(producer);
    }
}

From source file:org.openengsb.ports.jms.JMSPortTest.java

private String sendWithTempQueue(final String msg) {
    String resultString = jmsTemplate.execute(new SessionCallback<String>() {
        @Override// w  w  w.  ja  va2s . com
        public String doInJms(Session session) throws JMSException {
            Queue queue = session.createQueue("receive");
            MessageProducer producer = session.createProducer(queue);
            TemporaryQueue tempQueue = session.createTemporaryQueue();
            MessageConsumer consumer = session.createConsumer(tempQueue);
            TextMessage message = session.createTextMessage(msg);
            message.setJMSReplyTo(tempQueue);
            producer.send(message);
            TextMessage response = (TextMessage) consumer.receive(10000);
            assertThat(
                    "server should set the value of the correltion ID to the value of the received message id",
                    response.getJMSCorrelationID(), is(message.getJMSMessageID()));
            JmsUtils.closeMessageProducer(producer);
            JmsUtils.closeMessageConsumer(consumer);
            return response != null ? response.getText() : null;
        }
    }, true);
    return resultString;
}

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 {//from ww w .j  av  a  2 s .com
        if (this.explicitQosEnabledForReplies) {
            producer.send(replyMessage, this.replyDeliveryMode, this.replyPriority, this.replyTimeToLive);
        } else {
            producer.send(replyMessage);
        }
    } finally {
        JmsUtils.closeMessageProducer(producer);
    }
}

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

/**
 * Creates the MessageConsumer before sending the request Message since we are generating
 * our own correlationId value for the MessageSelector.
 *//*from w w  w. j a va2 s.  c o  m*/
private javax.jms.Message doSendAndReceiveWithGeneratedCorrelationId(Destination requestDestination,
        javax.jms.Message jmsRequest, Destination replyTo, Session session, int priority) throws JMSException {
    MessageProducer messageProducer = null;
    try {
        messageProducer = session.createProducer(requestDestination);
        Assert.state(this.correlationKey != null, "correlationKey must not be null");
        String messageSelector = null;
        if (!this.correlationKey.equals("JMSCorrelationID*") || jmsRequest.getJMSCorrelationID() == null) {
            String correlationId = UUID.randomUUID().toString().replaceAll("'", "''");
            if (this.correlationKey.equals("JMSCorrelationID")) {
                jmsRequest.setJMSCorrelationID(correlationId);
                messageSelector = "JMSCorrelationID = '" + correlationId + "'";
            } else {
                jmsRequest.setStringProperty(this.correlationKey, correlationId);
                jmsRequest.setJMSCorrelationID(null);
                messageSelector = this.correlationKey + " = '" + correlationId + "'";
            }
        } else {
            messageSelector = "JMSCorrelationID = '" + jmsRequest.getJMSCorrelationID() + "'";
        }

        this.sendRequestMessage(jmsRequest, messageProducer, priority);
        return retryableReceiveReply(session, replyTo, messageSelector);
    } finally {
        JmsUtils.closeMessageProducer(messageProducer);
    }
}