/** * Creates a <CODE>MessageConsumer</CODE> for the specified destination. * Since <CODE>Queue</CODE> and <CODE> Topic</CODE> both inherit from * <CODE>Destination</CODE>, they can be used in the destination * parameter to create a <CODE>MessageConsumer</CODE>. * * @param destination the <CODE>Destination</CODE> to access. * @param messageListener the listener to use for async consumption of messages * @return the MessageConsumer * @throws JMSException if the session fails to create a consumer due to * some internal error. * @throws InvalidDestinationException if an invalid destination is * specified. * @since 1.1 */ public MessageConsumer createConsumer(Destination destination, MessageListener messageListener) throws JMSException { return createConsumer(destination, null, messageListener); }
asyncSendPacket(earlyAck); } catch (Throwable t) { LOG.error("error dispatching ack: {} ", earlyAck, t); connection.onClientInternalException(t); } finally { continue; if (isClientAcknowledge()||isIndividualAcknowledge()) { message.setAcknowledgeCallback(new Callback() { @Override md.setDeliverySequenceId(getNextDeliveryId()); lastDeliveredSequenceId = message.getMessageId().getBrokerSequenceId(); ack.setFirstMessageId(md.getMessage().getMessageId()); doStartTransaction(); ack.setTransactionId(getTransactionContext().getTransactionId()); if (ack.getTransactionId() != null) { getTransactionContext().addSynchronization(new Synchronization() { if (!isClosed()) { LOG.error("{} error dispatching message: {} ", this, message.getMessageId(), e); if (getTransactionContext() != null && getTransactionContext().isInXATransaction()) { LOG.debug("Marking transaction: {} rollbackOnly", getTransactionContext()); getTransactionContext().setRollbackOnly(true); connection.onClientInternalException(e); } finally {
protected void onConsumerControl(ConsumerControl command) { if (command.isClose()) { for (ActiveMQSession session : this.sessions) { session.close(command.getConsumerId()); } } else { for (ActiveMQSession session : this.sessions) { session.setPrefetchSize(command.getConsumerId(), command.getPrefetch()); } for (ActiveMQConnectionConsumer connectionConsumer: connectionConsumers) { ConsumerInfo consumerInfo = connectionConsumer.getConsumerInfo(); if (consumerInfo.getConsumerId().equals(command.getConsumerId())) { consumerInfo.setPrefetchSize(command.getPrefetch()); } } } }
/** * Creates a <CODE>QueueReceiver</CODE> object to receive messages from * the specified queue. * * @param queue the <CODE>Queue</CODE> to access * @return a new QueueBrowser instance. * @throws JMSException if the session fails to create a receiver due to * some internal error. * @throws JMSException * @throws InvalidDestinationException if an invalid queue is specified. */ @Override public QueueReceiver createReceiver(Queue queue) throws JMSException { checkClosed(); return createReceiver(queue, null); }
/** * Creates a <CODE>QueueBrowser</CODE> object to peek at the messages on * the specified queue. * * @param queue the <CODE>queue</CODE> to access * @return the Queue Browser * @throws JMSException if the session fails to create a browser due to some * internal error. * @throws InvalidDestinationException if an invalid destination is * specified * @since 1.1 */ @Override public QueueBrowser createBrowser(Queue queue) throws JMSException { checkClosed(); return createBrowser(queue, null); }
/** * Creates a nondurable subscriber to the specified topic. <p/> * <P> * A client uses a <CODE>TopicSubscriber</CODE> object to receive messages * that have been published to a topic. <p/> * <P> * Regular <CODE>TopicSubscriber</CODE> objects are not durable. They * receive only messages that are published while they are active. <p/> * <P> * In some cases, a connection may both publish and subscribe to a topic. * The subscriber <CODE>NoLocal</CODE> attribute allows a subscriber to * inhibit the delivery of messages published by its own connection. The * default value for this attribute is false. * * @param topic the <CODE>Topic</CODE> to subscribe to * @return TopicSubscriber * @throws JMSException if the session fails to create a subscriber due to * some internal error. * @throws InvalidDestinationException if an invalid topic is specified. */ @Override public TopicSubscriber createSubscriber(Topic topic) throws JMSException { checkClosed(); return createSubscriber(topic, null, false); }
public synchronized void dispose() throws JMSException { if (!closed) { try { executor.close(); for (Iterator<ActiveMQMessageConsumer> iter = consumers.iterator(); iter.hasNext();) { ActiveMQMessageConsumer consumer = iter.next(); consumer.setFailureError(connection.getFirstFailureError()); consumer.dispose(); lastDeliveredSequenceId = Math.max(lastDeliveredSequenceId, consumer.getLastDeliveredSequenceId()); } consumers.clear(); for (Iterator<ActiveMQMessageProducer> iter = producers.iterator(); iter.hasNext();) { ActiveMQMessageProducer producer = iter.next(); producer.dispose(); } producers.clear(); try { if (getTransactionContext().isInLocalTransaction()) { rollback(); } } catch (JMSException e) { } } finally { connection.removeSession(this); this.transactionContext = null; closed = true; } } }
getSession().setMessageListener(null); for (Iterator<MessageConsumer> iter = consumers.iterator(); iter.hasNext();) { MessageConsumer consumer = iter.next(); consumer.close(); consumers.clear(); for (Iterator<QueueBrowser> iter = browsers.iterator(); iter.hasNext();) { QueueBrowser browser = iter.next(); browser.close(); getSession().rollback(); session.close();
private void destroyConsumer() { if (consumer == null) { return; } try { if (session.getTransacted() && session.getTransactionContext().isInLocalTransaction()) { session.commit(); } consumer.close(); consumer = null; } catch (JMSException e) { e.printStackTrace(); } }
synchronized (unconsumedMessages.getMutex()) { if (!unconsumedMessages.isClosed()) { if (this.info.isBrowser() || !session.connection.isDuplicate(this, md.getMessage())) { if (listener != null && unconsumedMessages.isRunning()) { if (redeliveryExceeded(md)) { posionAck(md, "listener dispatch[" + md.getRedeliveryCounter() + "] to " + getConsumerId() + " exceeds redelivery policy limit:" + redeliveryPolicy); return; LOG.error("{} Exception while processing message: {}", getConsumerId(), md.getMessage().getMessageId(), e); md.setRollbackCause(e); if (isAutoAcknowledgeBatch() || isAutoAcknowledgeEach() || session.isIndividualAcknowledge()) { if (!unconsumedMessages.isRunning()) { session.connection.rollbackDuplicate(this, md.getMessage()); if (info.getCurrentPrefetchSize() == 0) { unconsumedMessages.enqueue(null); immediateIndividualTransactedAck(md); } else { session.sendAck(new MessageAck(md, MessageAck.DELIVERED_ACK_TYPE, 1)); LOG.warn("{} delivering duplicate {}, pending transaction completion on {} will rollback", getConsumerId(), md.getMessage(), consumerWithPendingTransaction); session.getConnection().rollbackDuplicate(this, md.getMessage()); dispatch(md); } else {
protected void onConsumerControl(ConsumerControl command) { if (command.isClose()) { for (Iterator<ActiveMQSession> i = this.sessions.iterator(); i.hasNext();) { ActiveMQSession s = i.next(); s.close(command.getConsumerId()); } } else { for (Iterator<ActiveMQSession> i = this.sessions.iterator(); i.hasNext();) { ActiveMQSession s = i.next(); s.setPrefetchSize(command.getConsumerId(), command.getPrefetch()); } } }
ActiveMQBlobMessage msg = new ActiveMQBlobMessage(); ActiveMQConnection connection = ((ActiveMQSession) session) .getConnection(); msg.setConnection(connection); msg.setBlobDownloader(new BlobDownloader(connection .getBlobTransferPolicy())); try { msg.setURL(blobMessage.getURL()); message.getStringProperty("JMSXGroupID")); } catch (IOException e) { throw new JMSException(e.getMessage());
if (clearDeliveredList) { if (!deliveredMessages.isEmpty()) { if (session.isTransacted()) { previouslyDeliveredMessages = new PreviouslyDeliveredMap<MessageId, Boolean>(session.getTransactionContext().getTransactionId()); previouslyDeliveredMessages.put(delivered.getMessage().getMessageId(), false); if (session.isClientAcknowledge()) { LOG.debug("{} rolling back delivered list ({}) on transport interrupt", getConsumerId(), deliveredMessages.size()); if (!this.info.isBrowser()) { for (MessageDispatch md: deliveredMessages) { this.session.connection.rollbackDuplicate(this, md.getMessage());
ActiveMQMessage m = (ActiveMQMessage)md.getMessage().copy(); if (m.getDataStructureType()==CommandTypes.ACTIVEMQ_BLOB_MESSAGE) { ((ActiveMQBlobMessage)m).setBlobDownloader(new BlobDownloader(session.getBlobTransferPolicy())); ((ActiveMQObjectMessage)m).setTrustAllPackages(session.getConnection().isTrustAllPackages()); ((ActiveMQObjectMessage)m).setTrustedPackages(session.getConnection().getTrustedPackages()); if (session.isClientAcknowledge()) { m.setAcknowledgeCallback(new Callback() { @Override } else if (session.isIndividualAcknowledge()) { m.setAcknowledgeCallback(new Callback() { @Override
if (session.getTransacted()) { registerSync(); MessageAck oldPendingAck = pendingAck; pendingAck = new MessageAck(md, ackType, deliveredCounter); pendingAck.setTransactionId(session.getTransactionContext().getTransactionId()); if( oldPendingAck==null ) { pendingAck.setFirstMessageId(pendingAck.getLastMessageId()); session.sendAck(oldPendingAck); } else { LOG.debug("dropping old pending ack {}, new pending: {}", oldPendingAck, pendingAck); if ((0.5 * info.getPrefetchSize()) <= (deliveredCounter + ackCounter - additionalWindowSize)) { LOG.debug("ackLater: sending: {}", pendingAck); session.sendAck(pendingAck); pendingAck=null; deliveredCounter = 0;
private void clearDispatchList() { if (clearDispatchList) { synchronized (deliveredMessages) { if (clearDispatchList) { if (!deliveredMessages.isEmpty()) { if (session.isTransacted()) { if (LOG.isDebugEnabled()) { LOG.debug(getConsumerId() + " tracking existing transacted delivered list (" + deliveredMessages.size() + ") on transport interrupt"); } if (previouslyDeliveredMessages == null) { previouslyDeliveredMessages = new PreviouslyDeliveredMap<MessageId, Boolean>(session.getTransactionContext().getTransactionId()); } for (MessageDispatch delivered : deliveredMessages) { previouslyDeliveredMessages.put(delivered.getMessage().getMessageId(), false); } } else { if (LOG.isDebugEnabled()) { LOG.debug(getConsumerId() + " clearing delivered list (" + deliveredMessages.size() + ") on transport interrupt"); } deliveredMessages.clear(); pendingAck = null; } } clearDispatchList = false; } } } }
/** * Acknowledge all the messages that have been delivered to the client up to * this point. * * @throws JMSException */ public void acknowledge() throws JMSException { clearDeliveredList(); waitForRedeliveries(); synchronized(deliveredMessages) { // Acknowledge all messages so far. MessageAck ack = makeAckForAllDeliveredMessages(MessageAck.STANDARD_ACK_TYPE); if (ack == null) { return; // no msgs } if (session.getTransacted()) { rollbackOnFailedRecoveryRedelivery(); session.doStartTransaction(); ack.setTransactionId(session.getTransactionContext().getTransactionId()); } pendingAck = null; session.sendAck(ack); // Adjust the counters deliveredCounter = Math.max(0, deliveredCounter - deliveredMessages.size()); additionalWindowSize = Math.max(0, additionalWindowSize - deliveredMessages.size()); if (!session.getTransacted()) { deliveredMessages.clear(); } } }
@Test public void testPassMessageListenerIntoCreateConsumer() throws Exception { final AtomicInteger counter = new AtomicInteger(0); final CountDownLatch done = new CountDownLatch(1); // Receive a message with the JMS API connection.start(); ActiveMQSession session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQDestination destination = createDestination(session, destinationType); MessageConsumer consumer = session.createConsumer(destination, new MessageListener() { @Override public void onMessage(Message m) { counter.incrementAndGet(); if (counter.get() == 4) { done.countDown(); } } }); assertNotNull(consumer); // Send the messages sendMessages(session, destination, 4); assertTrue(done.await(1000, TimeUnit.MILLISECONDS)); Thread.sleep(200); // Make sure only 4 messages were delivered. assertEquals(4, counter.get()); }
checkClosed(); return createDurableSubscriber(topic, name, null, false);
private void registerSync() throws JMSException { session.doStartTransaction(); if (!synchronizationRegistered) { synchronizationRegistered = true; session.getTransactionContext().addSynchronization(new Synchronization() { @Override public void beforeEnd() throws Exception {