Example usage for javax.jms MessageListener onMessage

List of usage examples for javax.jms MessageListener onMessage

Introduction

In this page you can find the example usage for javax.jms MessageListener onMessage.

Prototype


void onMessage(Message message);

Source Link

Document

Passes a message to the listener.

Usage

From source file:com.amazon.sqs.javamessaging.SQSSessionCallbackScheduler.java

@Override
public void run() {
    CallbackEntry callbackEntry = null;//from  w w w  .j  a v a 2 s .  com
    try {
        while (true) {
            try {
                if (closed) {
                    break;
                }
                synchronized (callbackQueue) {
                    callbackEntry = callbackQueue.pollFirst();
                    if (callbackEntry == null) {
                        try {
                            callbackQueue.wait();
                        } catch (InterruptedException e) {
                            /**
                             * Will be retried on the next loop, and
                             * break if the callback scheduler is closed.
                             */
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("wait on empty callback queue interrupted: " + e.getMessage());
                            }
                        }
                        continue;
                    }
                }

                MessageListener messageListener = callbackEntry.getMessageListener();
                MessageManager messageManager = callbackEntry.getMessageManager();
                SQSMessage message = (SQSMessage) messageManager.getMessage();
                SQSMessageConsumer messageConsumer = messageManager.getPrefetchManager().getMessageConsumer();
                if (messageConsumer.isClosed()) {
                    nackReceivedMessage(message);
                    continue;
                }

                try {
                    // this takes care of start and stop
                    session.startingCallback(messageConsumer);
                } catch (JMSException e) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Not running callback: " + e.getMessage());
                    }
                    break;
                }

                try {
                    /**
                     * Notifying consumer prefetch thread so that it can
                     * continue to prefetch
                     */
                    messageManager.getPrefetchManager().messageDispatched();
                    int ackMode = acknowledgeMode.getOriginalAcknowledgeMode();
                    boolean tryNack = true;
                    try {
                        if (messageListener != null) {
                            if (ackMode != Session.AUTO_ACKNOWLEDGE) {
                                acknowledger.notifyMessageReceived(message);
                            }
                            boolean callbackFailed = false;
                            try {
                                messageListener.onMessage(message);
                            } catch (Throwable ex) {
                                LOG.info("Exception thrown from onMessage callback for message "
                                        + message.getSQSMessageId(), ex);
                                callbackFailed = true;
                            } finally {
                                if (!callbackFailed) {
                                    if (ackMode == Session.AUTO_ACKNOWLEDGE) {
                                        message.acknowledge();
                                    }
                                    tryNack = false;
                                }
                            }
                        }
                    } catch (JMSException ex) {
                        LOG.warn("Unable to complete message dispatch for the message "
                                + message.getSQSMessageId(), ex);
                    } finally {
                        if (tryNack) {
                            nackReceivedMessage(message);
                        }
                    }

                    /**
                     * The consumer close is delegated to the session thread
                     * if consumer close is called by its message listener's
                     * onMessage method on its own consumer.
                     */
                    if (consumerCloseAfterCallback != null) {
                        consumerCloseAfterCallback.doClose();
                        consumerCloseAfterCallback = null;
                    }
                } finally {
                    session.finishedCallback();
                }
            } catch (Throwable ex) {
                LOG.error("Unexpected exception thrown during the run of the scheduled callback", ex);
            }
        }
    } finally {
        if (callbackEntry != null) {
            nackReceivedMessage((SQSMessage) callbackEntry.getMessageManager().getMessage());
        }
        nackQueuedMessages();
    }
}

From source file:org.openanzo.combus.CombusConnection.java

/**
 * Start the message executor// w ww.  j  ava2  s.  c om
 * 
 * @throws JMSException
 */
public void startMessageExecutor() throws JMSException {
    if (!messageExecutorClosed) {
        return;
    }
    messageExecutorClosed = false;
    messageExecutor = new Thread("MessageExecutor") {
        @Override
        public void run() {
            Message message = null;
            while (!interrupted() && !messageExecutorClosed && !closed) {
                eventLock.lock();
                try {
                    message = (!messageBuffer.isEmpty()) ? messageBuffer.removeFirst() : null;
                    if (message == null) {
                        try {
                            newEventMessage.await();
                        } catch (InterruptedException ie) {
                        }
                    }
                } finally {
                    eventLock.unlock();
                }
                if (message != null) {
                    if (log.isTraceEnabled()) {
                        if (messageListeners.size() == 0) {
                            try {
                                log.trace(LogUtils.COMBUS_MARKER, MessageUtils.prettyPrint(message,
                                        "No message listeners.  Message getting dropped: "));
                            } catch (JMSException e) {
                                log.trace(LogUtils.COMBUS_MARKER, "error printing jms message", e);
                            }
                        }
                    }
                    for (MessageListener listener : messageListeners) {
                        listener.onMessage(message);
                    }
                }
                message = null;
            }
        }
    };
    messageExecutor.setDaemon(true);
    messageExecutor.start();
}

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

@Test
public void dispatcherHasNoSubscribersQueue() throws Exception {
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(true);
    factoryBean.setConnectionFactory(this.connectionFactory);
    factoryBean.setDestinationName("noSubscribersQueue");
    factoryBean.setBeanName("noSubscribersChannel");
    factoryBean.afterPropertiesSet();/*w  ww .j  a v  a 2s . c o  m*/
    SubscribableJmsChannel channel = (SubscribableJmsChannel) factoryBean.getObject();
    channel.afterPropertiesSet();

    AbstractMessageListenerContainer container = TestUtils.getPropertyValue(channel, "container",
            AbstractMessageListenerContainer.class);
    MessageListener listener = (MessageListener) container.getMessageListener();
    try {
        listener.onMessage(new StubTextMessage("Hello, world!"));
        fail("Exception expected");
    } catch (MessageDeliveryException e) {
        assertEquals("Dispatcher has no subscribers for jms-channel 'noSubscribersChannel'.", e.getMessage());
    }
}

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

@Test
public void dispatcherHasNoSubscribersTopic() throws Exception {
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(true);
    factoryBean.setConnectionFactory(this.connectionFactory);
    factoryBean.setDestinationName("noSubscribersTopic");
    factoryBean.setBeanName("noSubscribersChannel");
    factoryBean.setPubSubDomain(true);//from  ww  w  . ja  va 2  s .  co  m
    factoryBean.afterPropertiesSet();
    SubscribableJmsChannel channel = (SubscribableJmsChannel) factoryBean.getObject();
    channel.afterPropertiesSet();

    AbstractMessageListenerContainer container = TestUtils.getPropertyValue(channel, "container",
            AbstractMessageListenerContainer.class);
    MessageListener listener = (MessageListener) container.getMessageListener();
    List<String> logList = insertMockLoggerInListener(channel);
    listener.onMessage(new StubTextMessage("Hello, world!"));
    verifyLogReceived(logList);
}