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

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

Introduction

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

Prototype

public static void closeMessageConsumer(@Nullable MessageConsumer consumer) 

Source Link

Document

Close the given JMS MessageConsumer and ignore any thrown exception.

Usage

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

@Test
public void testMessageConsumer() 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;/*w w w.  jav  a2  s. c  o m*/
    MessageConsumer consumer = 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");
        consumer = session.createConsumer(myQueue);

        assertEquals(1, connectionFactory.getActiveMessageConsumerCount());

        consumer.receiveNoWait();

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

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

@Override
public void run(final SourceContext<T> context) throws Exception {
    TopicSession session = null;/*from  w w w  . j  a va2  s . c om*/
    TopicSubscriber consumer = null;

    try {
        session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        consumer = session.createSubscriber(destination, messageSelector, false);

        connection.start();

        while (isRunning) {
            context.collect(convert(consumer.receive()));
        }
    } catch (JMSException e) {
        logger.error("Error receiving message from [{}]: {}", destination.getTopicName(),
                e.getLocalizedMessage());
        throw new UncategorizedJmsException(e);
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
        JmsUtils.closeSession(session);
    }
}

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

@Override
public void run(final SourceContext<T> context) throws Exception {
    QueueSession session = null;/*from  w w w . j  a va2  s  . co m*/
    QueueReceiver consumer = null;

    try {
        session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        consumer = session.createReceiver(destination, messageSelector);

        connection.start();

        while (isRunning) {
            context.collect(convert(consumer.receive()));
        }
    } catch (JMSException e) {
        logger.error("Error receiving message from [{}]: {}", destination.getQueueName(),
                e.getLocalizedMessage());
        throw new UncategorizedJmsException(e);
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
        JmsUtils.closeSession(session);
    }
}

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

/**
* Actually receive a JMS message.//from   ww w  .j a  v  a2  s  . c o m
* @param session the JMS Session to operate on
* @param consumer the JMS MessageConsumer to receive with
* @return the JMS Message received, or <code>null</code> if none
* @throws JMSException if thrown by JMS API methods
*/
protected Message doReceive(Session session, MessageConsumer consumer) throws JMSException {
    try {
        // Use transaction timeout (if available).
        long timeout = getReceiveTimeout();
        JmsResourceHolder resourceHolder = (JmsResourceHolder) TransactionSynchronizationManager
                .getResource(getConnectionFactory());
        if (resourceHolder != null && resourceHolder.hasTimeout()) {
            timeout = resourceHolder.getTimeToLiveInMillis();
        }
        Message message = doReceive(consumer, timeout);
        if (session.getTransacted()) {
            // Commit necessary - but avoid commit call within a JTA transaction.
            if (isSessionLocallyTransacted(session)) {
                // Transacted session created by this template -> commit.
                JmsUtils.commitIfNecessary(session);
            }
        } else if (isClientAcknowledge(session)) {
            // Manually acknowledge message, if any.
            if (message != null) {
                message.acknowledge();
            }
        }
        return message;
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
    }
}

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

private String sendWithTempQueue(final String msg) {
    String resultString = jmsTemplate.execute(new SessionCallback<String>() {
        @Override//from w ww  .ja  v  a  2  s  .  c o m
        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.JmsOutboundGateway.java

/**
 * Creates the MessageConsumer before sending the request Message since we do not need any correlation.
 *///from w w  w  .  j  a  va 2  s  .  c  o m
private javax.jms.Message doSendAndReceiveWithTemporaryReplyToDestination(Destination requestDestination,
        javax.jms.Message jmsRequest, Destination replyTo, Session session, int priority) throws JMSException {
    MessageProducer messageProducer = null;
    MessageConsumer messageConsumer = null;
    try {
        messageProducer = session.createProducer(requestDestination);
        messageConsumer = session.createConsumer(replyTo);
        this.sendRequestMessage(jmsRequest, messageProducer, priority);
        return this.receiveReplyMessage(messageConsumer);
    } finally {
        JmsUtils.closeMessageProducer(messageProducer);
        JmsUtils.closeMessageConsumer(messageConsumer);
    }
}

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

private javax.jms.Message retryableReceiveReply(Session session, Destination replyTo, String messageSelector)
        throws JMSException {
    Connection consumerConnection = null; //NOSONAR
    Session consumerSession = session;// w  w w.  j  av  a2s  . c o  m
    MessageConsumer messageConsumer = null;
    JMSException exception = null;
    boolean isTemporaryReplyTo = replyTo instanceof TemporaryQueue || replyTo instanceof TemporaryTopic;
    long replyTimeout = isTemporaryReplyTo ? Long.MIN_VALUE
            : this.receiveTimeout < 0 ? Long.MAX_VALUE : System.currentTimeMillis() + this.receiveTimeout;
    try {
        do {
            try {
                messageConsumer = consumerSession.createConsumer(replyTo, messageSelector);
                javax.jms.Message reply = receiveReplyMessage(messageConsumer);
                if (reply == null) {
                    if (replyTimeout > System.currentTimeMillis()) {
                        throw new JMSException("Consumer closed before timeout");
                    }
                }
                return reply;
            } catch (JMSException e) {
                exception = e;
                if (logger.isDebugEnabled()) {
                    logger.debug("Connection lost waiting for reply, retrying: " + e.getMessage());
                }
                do {
                    try {
                        consumerConnection = createConnection();
                        consumerSession = createSession(consumerConnection);
                        break;
                    } catch (JMSException ee) {
                        exception = ee;
                        if (logger.isDebugEnabled()) {
                            logger.debug("Could not reconnect, retrying: " + ee.getMessage());
                        }
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                            Thread.currentThread().interrupt();
                            return null;
                        }
                    }
                } while (replyTimeout > System.currentTimeMillis());
            }
        } while (replyTimeout > System.currentTimeMillis());
        if (isTemporaryReplyTo) {
            return null;
        } else {
            throw exception;
        }
    } finally {
        if (consumerSession != session) {
            JmsUtils.closeSession(consumerSession);
            JmsUtils.closeConnection(consumerConnection);
        }
        JmsUtils.closeMessageConsumer(messageConsumer);
    }
}

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

protected Message doReceive(Session session, Destination destination) throws JMSException {
    MessageConsumer consumer = createConsumer(session, destination);
    try {//w w w.  j  a v a  2  s.  c o  m
        // use transaction timeout if available
        long timeout = getReceiveTimeout();
        ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager
                .getResource(getConnectionFactory());
        if (conHolder != null && conHolder.hasTimeout()) {
            timeout = conHolder.getTimeToLiveInMillis();
        }
        Message message = (timeout >= 0) ? consumer.receive(timeout) : consumer.receive();
        if (session.getTransacted()) {
            if (conHolder == null) {
                // transacted session created by this template -> commit
                session.commit();
            }
        } else if (message != null && isClientAcknowledge(session)) {
            message.acknowledge();
        }
        return message;
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
    }
}