KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > common > overflow > TTLOverflowHandler


1 package com.ubermq.jms.common.overflow;
2
3 import com.ubermq.jms.common.datagram.*;
4 import com.ubermq.kernel.*;
5 import com.ubermq.kernel.overflow.*;
6 import javax.jms.*;
7
8 /**
9  * An extension to the exponential backoff overflow handler
10  * that potentially drops messages if we would wait for space longer than
11  * their TTL.<P>
12  *
13  * This handler also looks at the delivery mode property and assumes
14  * negligible TTL for non persistent messages.<P>
15  */

16 public class TTLOverflowHandler
17     extends ExponentialBackoff
18 {
19     public TTLOverflowHandler() {super();}
20     public TTLOverflowHandler(long initial,
21                               int factor,
22                               long max,
23                               boolean shouldFail)
24     {
25         super(initial, factor, max, shouldFail);
26     }
27     
28     public IOverflowHandler getRetryHandler()
29     {
30         return new TTLOverflowHandler(Math.min(maximumTimeout, timeout * factor),
31                                       factor,
32                                       maximumTimeout,
33                                       shouldFailIfMaximumReached);
34     }
35     
36     /**
37      * If this is a message datagram, and the timeout we're about to wait is
38      * greater or equal to the TTL, drop the message. If not, we delegate
39      * to our superclass.
40      */

41     public int overflow(IDatagram d)
42     {
43         if (d instanceof IMessageDatagram)
44         {
45             int ttl = ((Integer JavaDoc)((IMessageDatagram)d).getStandardProperty(IMessageDatagram.STDPROP_TTL)).intValue();
46             int dmode = ((Integer JavaDoc)((IMessageDatagram)d).getStandardProperty(IMessageDatagram.STDPROP_DELIVERYMODE)).intValue();
47             if (dmode == DeliveryMode.NON_PERSISTENT ||
48                     (ttl > 0 && this.timeout >= ttl))
49                 return IOverflowHandler.ACTION_IGNORE;
50         }
51         
52         return super.overflow(d);
53     }
54 }
55
Popular Tags