001: /*
002: * $Id: DefaultRedeliveryHandler.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.jms;
012:
013: import org.mule.api.MessagingException;
014: import org.mule.transport.jms.i18n.JmsMessages;
015:
016: import java.util.Collections;
017: import java.util.Map;
018:
019: import javax.jms.JMSException;
020: import javax.jms.Message;
021:
022: import org.apache.commons.collections.map.LRUMap;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: /**
027: * <code>DefaultRedeliveryHandler</code> TODO
028: */
029: public class DefaultRedeliveryHandler implements RedeliveryHandler {
030: /**
031: * logger used by this class
032: */
033: protected static final Log logger = LogFactory
034: .getLog(DefaultRedeliveryHandler.class);
035:
036: private Map messages = null;
037:
038: protected JmsConnector connector;
039:
040: public DefaultRedeliveryHandler() {
041: messages = Collections.synchronizedMap(new LRUMap(256));
042: }
043:
044: /**
045: * The connector associated with this handler is set before
046: * <code>handleRedelivery()</code> is called
047: *
048: * @param connector the connector associated with this handler
049: */
050: public void setConnector(JmsConnector connector) {
051: this .connector = connector;
052: }
053:
054: /**
055: * process the redelivered message. If the Jms receiver should process the
056: * message, it should be returned. Otherwise the connector should throw a
057: * <code>MessageRedeliveredException</code> to indicate that the message should
058: * be handled by the connector Exception Handler.
059: *
060: * @param message
061: */
062: public void handleRedelivery(Message message) throws JMSException,
063: MessagingException {
064: if (connector.getMaxRedelivery() <= 0) {
065: return;
066: }
067:
068: String id = message.getJMSMessageID();
069: Integer i = (Integer) messages.remove(id);
070: if (i == null) {
071: if (logger.isDebugEnabled()) {
072: logger.debug("Message with id: " + id
073: + " has been redelivered for the first time");
074: }
075: messages.put(id, new Integer(1));
076: return;
077: } else if (i.intValue() == connector.getMaxRedelivery()) {
078: if (logger.isDebugEnabled()) {
079: logger
080: .debug("Message with id: "
081: + id
082: + " has been redelivered "
083: + (i.intValue() + 1)
084: + " times, which exceeds the maxRedelivery setting on the connector");
085: }
086: JmsMessageAdapter adapter = (JmsMessageAdapter) connector
087: .getMessageAdapter(message);
088: throw new MessageRedeliveredException(JmsMessages
089: .tooManyRedeliveries(id, String.valueOf(i
090: .intValue() + 1)), adapter);
091:
092: } else {
093: messages.put(id, new Integer(i.intValue() + 1));
094: if (logger.isDebugEnabled()) {
095: logger.debug("Message with id: " + id
096: + " has been redelivered " + i.intValue()
097: + " times");
098: }
099: }
100: }
101: }
|