KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > juc > MessageComparator


1 package net.walend.somnifugi.juc;
2
3 import java.util.Comparator JavaDoc;
4
5 import javax.jms.Message JavaDoc;
6 import javax.jms.JMSException JavaDoc;
7
8 import net.walend.somnifugi.SomniRuntimeException;
9 import net.walend.somnifugi.SomniMessage;
10
11 /**
12 A Comparator for Messages that supports Priorities and message ordering.
13
14 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
15  */

16
17 class MessageComparator
18     implements Comparator JavaDoc<Message JavaDoc>
19 {
20     MessageComparator()
21     {}
22
23     /**
24 Compares two messages for order in the queue.
25 <p>
26 This comparator is for messagine ordering in the priority queue. (compare(message1,message2)==0) may be true even when message1.equals(message2) is false.
27 <p>
28 @return -1 if messsage1 should arrive before message2, 0 if no decision can be made, and 1 if message2 should arrive before message1. message1 should come first if it is of higher priority. If both messages are of equal priority, message1 should come first if its getJMSTimestamp() is earlier (smaller).
29     */

30     public int compare(Message JavaDoc message1,Message JavaDoc message2)
31     {
32         try
33         {
34             //check priorities
35
if(message1.getJMSPriority()>message2.getJMSPriority())
36             {
37                 return -1;
38             }
39             if(message1.getJMSPriority()<message2.getJMSPriority())
40             {
41                 return 1;
42             }
43
44             //if priorities are the same, check time stamps
45
if(message1.getJMSTimestamp()<message2.getJMSTimestamp())
46             {
47                 return -1;
48             }
49             else if(message1.getJMSTimestamp()>message2.getJMSTimestamp())
50             {
51                 return 1;
52             }
53             else
54             {
55                 //if timestamps are the same, use the SomniProducerCount if it's a SomniMessage
56
if((message1 instanceof SomniMessage)&&(message2 instanceof SomniMessage))
57                 {
58                     int message1ID = ((SomniMessage)message1).getSomniProducerCount();
59                     int message2ID = ((SomniMessage)message2).getSomniProducerCount();
60
61                     if(message1ID < message2ID)
62                     {
63                         return -1;
64                     }
65                     else if(message1ID > message2ID)
66                     {
67                         return 1;
68                     }
69                 }
70                 //if not, then use the id string field.
71
if(message1.getJMSMessageID()!=null)
72                 {
73                     return message1.getJMSMessageID().compareTo(message2.getJMSMessageID());
74                 }
75
76             }
77             //there's no way to choose one messages over another.
78
return 0;
79         }
80         catch(JMSException JavaDoc jmse)
81         {
82             throw new SomniRuntimeException("Trouble comparing time stamps.",jmse);
83         }
84     }
85 }
86
87 /* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
88 All rights reserved.
89
90 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
91
92 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
93
94 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
95
96 Neither the name of the SomnifugiJMS Project, walend.net, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission from David Walend.
97
98 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
99
100 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
101 The net.walend.somnifugi.sql92 package is modified code from the openmq project, https://mq.dev.java.net/ , Copyright (c) of Sun, and carries the CDDL license, repeated here: You can obtain a copy of the license at https://glassfish.dev.java.net/public/CDDLv1.0.html. See the License for the specific language governing permissions and limitations under the License.
102
103 =================================================================================
104
105 For more information and the latest version of this software, please see http://somnifugi.sourceforge.net and http://walend.net or email <a HREF="mailto:david@walend.net">david@walend.net</a>.
106  */

107
Popular Tags