List of usage examples for javax.jms MessageConsumer close
void close() throws JMSException;
From source file:nl.nn.adapterframework.jms.JmsSender.java
public String sendMessage(String correlationID, String message, ParameterResolutionContext prc, String soapHeader) throws SenderException, TimeOutException { Session s = null;//from ww w . j a v a2 s. com MessageProducer mp = null; ParameterValueList pvl = null; if (prc != null && paramList != null) { try { pvl = prc.getValues(paramList); } catch (ParameterException e) { throw new SenderException(getLogPrefix() + "cannot extract parameters", e); } } if (isSoap()) { if (soapHeader == null) { if (pvl != null && StringUtils.isNotEmpty(getSoapHeaderParam())) { ParameterValue soapHeaderParamValue = pvl.getParameterValue(getSoapHeaderParam()); if (soapHeaderParamValue == null) { log.warn("no SoapHeader found using parameter [" + getSoapHeaderParam() + "]"); } else { soapHeader = soapHeaderParamValue.asStringValue(""); } } } message = soapWrapper.putInEnvelope(message, getEncodingStyleURI(), getServiceNamespaceURI(), soapHeader); if (log.isDebugEnabled()) log.debug(getLogPrefix() + "correlationId [" + correlationID + "] soap message [" + message + "]"); } try { s = createSession(); mp = getMessageProducer(s, getDestination(prc)); Destination replyQueue = null; // create message Message msg = createTextMessage(s, correlationID, message); if (getMessageType() != null) { msg.setJMSType(getMessageType()); } if (getDeliveryModeInt() > 0) { msg.setJMSDeliveryMode(getDeliveryModeInt()); mp.setDeliveryMode(getDeliveryModeInt()); } if (getPriority() >= 0) { msg.setJMSPriority(getPriority()); mp.setPriority(getPriority()); } // set properties if (pvl != null) { setProperties(msg, pvl); } if (replyToName != null) { replyQueue = getDestination(replyToName); } else { if (isSynchronous()) { replyQueue = getMessagingSource().getDynamicReplyQueue(s); } } if (replyQueue != null) { msg.setJMSReplyTo(replyQueue); if (log.isDebugEnabled()) log.debug("replyTo set to queue [" + replyQueue.toString() + "]"); } // send message send(mp, msg); if (log.isDebugEnabled()) { log.debug("[" + getName() + "] " + "sent message [" + message + "] " + "to [" + mp.getDestination() + "] " + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID [" + msg.getJMSCorrelationID() + "] " + "using deliveryMode [" + getDeliveryMode() + "] " + ((replyToName != null) ? "replyTo [" + replyToName + "]" : "")); } else { if (log.isInfoEnabled()) { log.info("[" + getName() + "] " + "sent message to [" + mp.getDestination() + "] " + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID [" + msg.getJMSCorrelationID() + "] " + "using deliveryMode [" + getDeliveryMode() + "] " + ((replyToName != null) ? "replyTo [" + replyToName + "]" : "")); } } if (isSynchronous()) { String replyCorrelationId = null; if (replyToName != null) { if ("CORRELATIONID".equalsIgnoreCase(getLinkMethod())) { replyCorrelationId = correlationID; } else if ("CORRELATIONID_FROM_MESSAGE".equalsIgnoreCase(getLinkMethod())) { replyCorrelationId = msg.getJMSCorrelationID(); } else { replyCorrelationId = msg.getJMSMessageID(); } } if (log.isDebugEnabled()) log.debug("[" + getName() + "] start waiting for reply on [" + replyQueue + "] requestMsgId [" + msg.getJMSMessageID() + "] replyCorrelationId [" + replyCorrelationId + "] for [" + getReplyTimeout() + "] ms"); MessageConsumer mc = getMessageConsumerForCorrelationId(s, replyQueue, replyCorrelationId); try { Message rawReplyMsg = mc.receive(getReplyTimeout()); if (rawReplyMsg == null) { throw new TimeOutException("did not receive reply on [" + replyQueue + "] requestMsgId [" + msg.getJMSMessageID() + "] replyCorrelationId [" + replyCorrelationId + "] within [" + getReplyTimeout() + "] ms"); } return getStringFromRawMessage(rawReplyMsg, prc != null ? prc.getSession() : null, isSoap(), getReplySoapHeaderSessionKey(), soapWrapper); } finally { if (mc != null) { try { mc.close(); } catch (JMSException e) { log.warn("JmsSender [" + getName() + "] got exception closing message consumer for reply", e); } } } } return msg.getJMSMessageID(); } catch (JMSException e) { throw new SenderException(e); } catch (IOException e) { throw new SenderException(e); } catch (NamingException e) { throw new SenderException(e); } catch (DomBuilderException e) { throw new SenderException(e); } catch (TransformerException e) { throw new SenderException(e); } catch (JmsException e) { throw new SenderException(e); } finally { if (mp != null) { try { mp.close(); } catch (JMSException e) { log.warn("JmsSender [" + getName() + "] got exception closing message producer", e); } } closeSession(s); } }
From source file:nl.nn.adapterframework.jms.PullingJmsListener.java
protected void releaseReceiver(MessageConsumer receiver, String correlationId) throws ListenerException { if ((isSessionsArePooled() || StringUtils.isNotEmpty(correlationId)) && receiver != null) { try {//from w ww .j av a 2s . c o m receiver.close(); // do not write to log, this occurs too often } catch (Exception e) { throw new ListenerException(getLogPrefix() + "exception closing QueueReceiver", e); } } }
From source file:nl.nn.adapterframework.jms.PullingJmsListener.java
/** * @see nl.nn.adapterframework.core.IPostboxListener#retrieveRawMessage(java.lang.String, java.util.Map) */// w w w. j a v a 2 s . c om public Object retrieveRawMessage(String messageSelector, Map threadContext) throws ListenerException { Session session = null; try { session = getSession(threadContext); MessageConsumer mc = null; try { mc = getMessageConsumer(session, getDestination(), messageSelector); Object result = (getTimeOut() < 0) ? mc.receiveNoWait() : mc.receive(getTimeOut()); return result; } finally { if (mc != null) { try { mc.close(); } catch (JMSException e) { log.warn(getLogPrefix() + "exception closing messageConsumer", e); } } } } catch (Exception e) { throw new ListenerException(getLogPrefix() + "exception preparing to retrieve message", e); } finally { releaseSession(session); } }
From source file:org.aludratest.service.jms.impl.JmsActionImpl.java
@Override public Message receiveMessage(String destinationName, long timeout) { MessageConsumer consumer = null; try {/*from w w w .jav a2s. c o m*/ Destination dest = (Destination) context.lookup(destinationName); consumer = getSession().createConsumer(dest); this.startConnection(); Message msg; if (timeout == -1) { msg = consumer.receive(); } else { msg = consumer.receive(timeout); } this.stopConnection(); if (msg == null) { throw new PerformanceFailure( "Destination " + destinationName + " did not deliver a message within timeout"); } return msg; } catch (NamingException e) { throw new AutomationException("Could not lookup destination " + destinationName, e); } catch (ClassCastException e) { throw new AutomationException("JNDI object with name " + destinationName + " is no destination", e); } catch (JMSException e) { throw new AccessFailure("Could not receive JMS message", e); } finally { if (consumer != null) { try { consumer.close(); } catch (JMSException e) { } } } }
From source file:org.apache.activemq.apollo.JmsQueueBrowserTest.java
/** * Tests the queue browser. Browses the messages then the consumer tries to receive them. The messages should still * be in the queue even when it was browsed. * * Re-enable once https://issues.apache.org/jira/browse/APLO-226 is fixed. * * @throws Exception//from w w w.j a v a 2 s . c om */ public void testReceiveBrowseReceive() throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQQueue destination = new ActiveMQQueue("TEST"); MessageProducer producer = session.createProducer(destination); MessageConsumer consumer = session.createConsumer(destination); connection.start(); Message[] outbound = new Message[] { session.createTextMessage("First Message"), session.createTextMessage("Second Message"), session.createTextMessage("Third Message") }; // lets consume any outstanding messages from previous test runs while (consumer.receive(1000) != null) { } producer.send(outbound[0]); producer.send(outbound[1]); producer.send(outbound[2]); // Get the first. assertEquals(outbound[0], consumer.receive(1000)); consumer.close(); //Thread.sleep(200); QueueBrowser browser = session.createBrowser((Queue) destination); Enumeration enumeration = browser.getEnumeration(); // browse the second assertTrue("should have received the second message", enumeration.hasMoreElements()); assertEquals(outbound[1], (Message) enumeration.nextElement()); // browse the third. assertTrue("Should have received the third message", enumeration.hasMoreElements()); assertEquals(outbound[2], (Message) enumeration.nextElement()); // There should be no more. boolean tooMany = false; while (enumeration.hasMoreElements()) { LOG.info("Got extra message: " + ((TextMessage) enumeration.nextElement()).getText()); tooMany = true; } assertFalse(tooMany); browser.close(); // Re-open the consumer. consumer = session.createConsumer(destination); // Receive the second. assertEquals(outbound[1], consumer.receive(1000)); // Receive the third. assertEquals(outbound[2], consumer.receive(1000)); consumer.close(); }
From source file:org.apache.activemq.apollo.JmsQueueBrowserTest.java
public void testBrowseReceive() throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQQueue destination = new ActiveMQQueue("TEST"); connection.start();/*from w ww .j av a 2s. com*/ Message[] outbound = new Message[] { session.createTextMessage("First Message"), session.createTextMessage("Second Message"), session.createTextMessage("Third Message") }; MessageProducer producer = session.createProducer(destination); producer.send(outbound[0]); // create browser first QueueBrowser browser = session.createBrowser((Queue) destination); Enumeration enumeration = browser.getEnumeration(); // create consumer MessageConsumer consumer = session.createConsumer(destination); // browse the first message assertTrue("should have received the first message", enumeration.hasMoreElements()); assertEquals(outbound[0], (Message) enumeration.nextElement()); // Receive the first message. assertEquals(outbound[0], consumer.receive(1000)); consumer.close(); browser.close(); producer.close(); }
From source file:org.apache.activemq.apollo.JmsQueueBrowserTest.java
public void testBrowseClose() throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQQueue destination = new ActiveMQQueue("TEST"); connection.start();/*from www .j ava2 s .c o m*/ TextMessage[] outbound = new TextMessage[] { session.createTextMessage("First Message"), session.createTextMessage("Second Message"), session.createTextMessage("Third Message") }; MessageProducer producer = session.createProducer(destination); producer.send(outbound[0]); producer.send(outbound[1]); producer.send(outbound[2]); // create browser first QueueBrowser browser = session.createBrowser((Queue) destination); Enumeration enumeration = browser.getEnumeration(); // browse some messages assertEquals(outbound[0], (Message) enumeration.nextElement()); assertEquals(outbound[1], (Message) enumeration.nextElement()); //assertEquals(outbound[2], (Message) enumeration.nextElement()); browser.close(); // create consumer MessageConsumer consumer = session.createConsumer(destination); // Receive the first message. TextMessage msg = (TextMessage) consumer.receive(1000); assertEquals("Expected " + outbound[0].getText() + " but received " + msg.getText(), outbound[0], msg); msg = (TextMessage) consumer.receive(1000); assertEquals("Expected " + outbound[1].getText() + " but received " + msg.getText(), outbound[1], msg); msg = (TextMessage) consumer.receive(1000); assertEquals("Expected " + outbound[2].getText() + " but received " + msg.getText(), outbound[2], msg); consumer.close(); producer.close(); }
From source file:org.apache.activemq.artemis.tests.integration.persistence.metrics.JournalPendingMessageTest.java
@Test public void testTopicMessageSizeShared() throws Exception { AtomicLong publishedMessageSize = new AtomicLong(); Connection connection = cf.createConnection(); connection.setClientID("clientId"); connection.start();// www. ja v a2s .co m Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createSharedConsumer(session.createTopic(defaultTopicName), "sub1"); MessageConsumer consumer2 = session.createSharedConsumer(session.createTopic(defaultTopicName), "sub1"); publishTestTopicMessages(200, publishedMessageSize); verifyPendingStats(defaultTopicName, 200, publishedMessageSize.get()); verifyPendingDurableStats(defaultTopicName, 0, 0); consumer2.close(); // consume all messages consumeTestMessages(consumer, 200); // All messages should now be gone verifyPendingStats(defaultTopicName, 0, 0); verifyPendingDurableStats(defaultTopicName, 0, 0); connection.close(); }
From source file:org.apache.activemq.artemis.tests.integration.persistence.metrics.JournalPendingMessageTest.java
@Test public void testMessageSizeSharedDurable() throws Exception { AtomicLong publishedMessageSize = new AtomicLong(); Connection connection = cf.createConnection(); connection.setClientID("clientId"); connection.start();//from w w w.j a v a 2 s . c o m // The publish method will create a second shared consumer Session s = connection.createSession(); MessageConsumer c = s.createSharedDurableConsumer(s.createTopic(defaultTopicName), "sub1"); publishTestMessagesDurable(connection, new String[] { "sub1", }, 200, publishedMessageSize, DeliveryMode.PERSISTENT, true); // verify the count and size - double because two durables so two queue // bindings verifyPendingStats(defaultTopicName, 200, publishedMessageSize.get()); verifyPendingDurableStats(defaultTopicName, 200, publishedMessageSize.get()); c.close(); // consume messages for sub1 consumeDurableTestMessages(connection, "sub1", 200, publishedMessageSize); verifyPendingStats(defaultTopicName, 0, publishedMessageSize.get()); verifyPendingDurableStats(defaultTopicName, 0, publishedMessageSize.get()); connection.close(); }
From source file:org.apache.activemq.jms.pool.PooledSession.java
@Override public void close() throws JMSException { if (ignoreClose) { return;//from w w w. j a v a 2 s. c om } if (closed.compareAndSet(false, true)) { boolean invalidate = false; try { // lets reset the session getInternalSession().setMessageListener(null); // Close any consumers and browsers that may have been created. for (Iterator<MessageConsumer> iter = consumers.iterator(); iter.hasNext();) { MessageConsumer consumer = iter.next(); consumer.close(); } for (Iterator<QueueBrowser> iter = browsers.iterator(); iter.hasNext();) { QueueBrowser browser = iter.next(); browser.close(); } if (transactional && !isXa) { try { getInternalSession().rollback(); } catch (JMSException e) { invalidate = true; LOG.warn( "Caught exception trying rollback() when putting session back into the pool, will invalidate. " + e, e); } } } catch (JMSException ex) { invalidate = true; LOG.warn( "Caught exception trying close() when putting session back into the pool, will invalidate. " + ex, ex); } finally { consumers.clear(); browsers.clear(); for (PooledSessionEventListener listener : this.sessionEventListeners) { listener.onSessionClosed(this); } sessionEventListeners.clear(); } if (invalidate) { // lets close the session and not put the session back into the pool // instead invalidate it so the pool can create a new one on demand. if (sessionHolder != null) { try { sessionHolder.close(); } catch (JMSException e1) { LOG.trace("Ignoring exception on close as discarding session: " + e1, e1); } } try { sessionPool.invalidateObject(key, sessionHolder); } catch (Exception e) { LOG.trace("Ignoring exception on invalidateObject as discarding session: " + e, e); } } else { try { sessionPool.returnObject(key, sessionHolder); } catch (Exception e) { javax.jms.IllegalStateException illegalStateException = new javax.jms.IllegalStateException( e.toString()); illegalStateException.initCause(e); throw illegalStateException; } } sessionHolder = null; } }