KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jms > inflow > dlq > GenericDLQHandler


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.adapter.jms.inflow.dlq;
23
24 import java.util.HashMap JavaDoc;
25
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.Message JavaDoc;
28
29 /**
30  * A Generic DLQ Handler
31  *
32  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
33  * @version $Revision: 38447 $
34  */

35 public class GenericDLQHandler extends JBossMQDLQHandler
36 {
37    /** Resent messages Map<MessageID, Integer> */
38    protected HashMap JavaDoc resent = new HashMap JavaDoc();
39
40    public void messageDelivered(Message JavaDoc msg)
41    {
42       try
43       {
44          String JavaDoc id = msg.getJMSMessageID();
45          if (id == null)
46          {
47             log.error("Message id is null? " + msg);
48             return;
49          }
50
51          clearResentCounter(id);
52       }
53       catch (Throwable JavaDoc t)
54       {
55          log.warn("Unexpected error processing delivery notification " + msg, t);
56       }
57    }
58
59    protected boolean handleDelivery(Message JavaDoc msg)
60    {
61       // Check for JBossMQ specific
62
boolean handled = super.handleDelivery(msg);
63       if (handled)
64          return true;
65       
66       try
67       {
68          if (msg.propertyExists(JMS_JBOSS_REDELIVERY_COUNT))
69             return false;
70
71          String JavaDoc id = msg.getJMSMessageID();
72          if (id == null)
73          {
74             log.error("Message id is null? " + msg);
75             return false;
76          }
77
78          int count = 0;
79          
80          try
81          {
82             if (msg.propertyExists(PROPERTY_DELIVERY_COUNT))
83                count = msg.getIntProperty(PROPERTY_DELIVERY_COUNT) - 1;
84          }
85          catch (JMSException JavaDoc ignored)
86          {
87             count = incrementResentCounter(id);
88          }
89          
90          if (count > maxResent)
91          {
92             warnDLQ(msg, count, maxResent);
93             clearResentCounter(id);
94             return true;
95          }
96       }
97       catch (Throwable JavaDoc t)
98       {
99          log.warn("Unexpected error checking whether dlq should be used " + msg, t);
100       }
101       
102       return false;
103    }
104    
105    /**
106     * Increment the resent counter for the message id
107     *
108     * @param id the message id of the message
109     */

110    protected int incrementResentCounter(String JavaDoc id)
111    {
112       ResentInfo info;
113       synchronized (resent)
114       {
115          info = (ResentInfo) resent.get(id);
116          if (info == null)
117          {
118             info = new ResentInfo();
119             resent.put(id, info);
120          }
121       }
122       return ++info.count;
123    }
124    
125    /**
126     * Remove the resent counter for the message id
127     *
128     * @param id the message id of the message
129     */

130    protected void clearResentCounter(String JavaDoc id)
131    {
132       synchronized (resent)
133       {
134          resent.remove(id);
135       }
136    }
137    
138    /**
139     * Resent Info
140     */

141    protected static class ResentInfo
142    {
143       int count = 0;
144    }
145 }
Popular Tags