001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.adapter.jms.inflow.dlq;
023:
024: import java.util.HashMap;
025:
026: import javax.jms.JMSException;
027: import javax.jms.Message;
028:
029: /**
030: * A Generic DLQ Handler
031: *
032: * @author <a href="adrian@jboss.com">Adrian Brock</a>
033: * @version $Revision: 57189 $
034: */
035: public class GenericDLQHandler extends JBossMQDLQHandler {
036: /** Resent messages Map<MessageID, Integer> */
037: protected HashMap resent = new HashMap();
038:
039: public void messageDelivered(Message msg) {
040: try {
041: String id = msg.getJMSMessageID();
042: if (id == null) {
043: log.error("Message id is null? " + msg);
044: return;
045: }
046:
047: clearResentCounter(id);
048: } catch (Throwable t) {
049: log.warn(
050: "Unexpected error processing delivery notification "
051: + msg, t);
052: }
053: }
054:
055: protected boolean handleDelivery(Message msg) {
056: // Check for JBossMQ specific
057: boolean handled = super .handleDelivery(msg);
058: if (handled)
059: return true;
060:
061: try {
062: if (msg.propertyExists(JMS_JBOSS_REDELIVERY_COUNT))
063: return false;
064:
065: String id = msg.getJMSMessageID();
066: if (id == null) {
067: log.error("Message id is null? " + msg);
068: return false;
069: }
070:
071: int count = 0;
072:
073: try {
074: if (msg.propertyExists(PROPERTY_DELIVERY_COUNT))
075: count = msg.getIntProperty(PROPERTY_DELIVERY_COUNT) - 1;
076: } catch (JMSException ignored) {
077: count = incrementResentCounter(id);
078: }
079:
080: if (count > maxResent) {
081: warnDLQ(msg, count, maxResent);
082: clearResentCounter(id);
083: return true;
084: }
085: } catch (Throwable t) {
086: log.warn(
087: "Unexpected error checking whether dlq should be used "
088: + msg, t);
089: }
090:
091: return false;
092: }
093:
094: /**
095: * Increment the resent counter for the message id
096: *
097: * @param id the message id of the message
098: */
099: protected int incrementResentCounter(String id) {
100: ResentInfo info;
101: synchronized (resent) {
102: info = (ResentInfo) resent.get(id);
103: if (info == null) {
104: info = new ResentInfo();
105: resent.put(id, info);
106: }
107: }
108: return ++info.count;
109: }
110:
111: /**
112: * Remove the resent counter for the message id
113: *
114: * @param id the message id of the message
115: */
116: protected void clearResentCounter(String id) {
117: synchronized (resent) {
118: resent.remove(id);
119: }
120: }
121:
122: /**
123: * Resent Info
124: */
125: protected static class ResentInfo {
126: int count = 0;
127: }
128: }
|