KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > protocol > MantaBusMessageUtil


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 /*
47  * Created on Jan 25, 2004
48  * Manta LTD
49  */

50 package org.mr.core.protocol;
51
52
53 import java.util.Collections JavaDoc;
54 import java.util.Comparator JavaDoc;
55 import java.util.List JavaDoc;
56
57 import javax.jms.JMSException JavaDoc;
58
59 import org.mr.api.jms.MantaMessage;
60
61
62
63 /**
64  * MantaBusMessageUtil is a general util object for manta messages
65  * @since Jan 25, 2004
66  * @version 1.0
67  * @author Amir Shevat
68  *
69
70  */

71 public class MantaBusMessageUtil {
72
73     public static MantaBusMessage createACKMessage(MantaBusMessage msg){
74
75         MantaBusMessage result = MantaBusMessage.getInstance();
76         result.getElements().putAll(msg.getElements());
77
78         result.setDeliveryMode(msg.getDeliveryMode());
79         DeadEndRecipient recipient = DeadEndRecipient.createDeadEndRecipient(msg.getSource().getAgentName(), msg.getSource().getDomainName());
80         result.setRecipient(recipient);
81         result.setMessageType(msg.getMessageType());
82         result.setPriority(msg.getPriority());
83         /// we should send an ack anyway to avoid memory problems in the message producer
84
result.setValidUntil(Long.MAX_VALUE);//msg.getValidUntil());
85

86
87         return result;
88     }
89
90     /**
91      * Sorts a set of messages by time of sending
92      * @param tempList the list of mantaBusMessages to be sorted
93      */

94     public static void sortMessagesBySendTime( List JavaDoc tempList){
95         Collections.sort(tempList, new Comparator JavaDoc(){
96
97             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
98                 MantaBusMessage first = (MantaBusMessage) o1;
99                 MantaBusMessage sec = (MantaBusMessage) o2;
100                 if(first.getPayload() instanceof MantaMessage && sec.getPayload() instanceof MantaMessage){
101                     MantaMessage m1 = (MantaMessage) first.getPayload();
102                     MantaMessage m2 = (MantaMessage) sec.getPayload();
103                     long m1Time = 0;
104                     long m2Time = 0;
105
106                      try {
107                         m1Time = m1.getJMSTimestamp();
108                         m2Time = m2.getJMSTimestamp();
109                     } catch (JMSException JavaDoc e) {
110                     }
111                     if(m1Time == m2Time && m2Time != 0){
112                         // if time is same check message ID
113
return (int) (first.getMessageIdAsLong()-sec.getMessageIdAsLong());
114                     }
115
116                     return (int)(m1Time-m2Time);
117                 }
118                 return 0;
119             }
120
121         });
122     }
123
124     /**
125      * Sorts a set of messages by a spesific header that can be casted to long
126      * @param tempList the list of mantaBusMessages to be sorted
127      * @param headerName the header of MantaBusMessage that can be casted to long
128      */

129     public static void sortMessagesByEnqueueTime( List JavaDoc tempList,String JavaDoc headerName){
130         final String JavaDoc fhn = headerName;
131         Collections.sort(tempList, new Comparator JavaDoc(){
132
133             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
134                 MantaBusMessage first = (MantaBusMessage) o1;
135                 MantaBusMessage sec = (MantaBusMessage) o2;
136                 if(first.getPayload() instanceof MantaMessage && sec.getPayload() instanceof MantaMessage){
137                     long m1Time = 0;
138                     long m2Time = 0;
139
140                         m1Time = Long.parseLong(first.getHeader(fhn));
141                         m2Time = Long.parseLong(first.getHeader(fhn));
142                     if(m1Time == m2Time && m2Time != 0){
143                         // if time is same check message ID
144
return (int) (first.getMessageIdAsLong()-sec.getMessageIdAsLong());
145                     }
146
147                     return (int)(m1Time-m2Time);
148                 }
149                 return 0;
150             }
151
152         });
153     }
154 }
155
Popular Tags