KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > connection > MultiThreadSyncChannel


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 2000 Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): ScalAgent Distributed Technologies
22  */

23 package org.objectweb.joram.client.jms.connection;
24
25 import java.util.Timer JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.objectweb.joram.shared.client.AbstractJmsReply;
29 import org.objectweb.joram.shared.client.AbstractJmsRequest;
30 import org.objectweb.joram.shared.client.JmsRequestGroup;
31
32 /**
33  * Class wrapping the <code>RequestChannel</code> in order to group the
34  * requests. It allows best performances with multiples senders.
35  */

36 public class MultiThreadSyncChannel implements RequestChannel {
37   /**
38    * Synchronization round.
39    */

40   private SyncRound currentRound;
41   
42   /**
43    * Synchronized requests.
44    */

45   private Vector JavaDoc syncRequests;
46   
47   /**
48    * The maximum time the threads hang if 'multiThreadSync' is true.
49    * Either they wake up (wait time out) or they are notified (by the
50    * first woken up thread).
51    */

52   private int multiThreadSyncDelay;
53
54   /**
55    * The maximum numbers of threads that hang if 'multiThreadSync' is true.
56    */

57   private int multiThreadSyncThreshold;
58   
59   /** The related RequestChannel. */
60   private RequestChannel channel;
61   
62   MultiThreadSyncChannel(RequestChannel rc, int delay, int threshold) {
63     channel = rc;
64     multiThreadSyncDelay = delay;
65     multiThreadSyncThreshold = threshold;
66     currentRound = new SyncRound();
67     syncRequests = new Vector JavaDoc();
68   }
69
70   public synchronized void send(AbstractJmsRequest request) throws Exception JavaDoc {
71     SyncRound localRound = currentRound;
72     syncRequests.addElement(request);
73     if (syncRequests.size() < multiThreadSyncThreshold) {
74       try {
75         wait(multiThreadSyncDelay);
76       } catch (InterruptedException JavaDoc ie) {
77       }
78     }
79     if (!localRound.done) {
80       // syncRequests.size() must be > 0
81
AbstractJmsRequest[] requests =
82         new AbstractJmsRequest[syncRequests.size()];
83       syncRequests.copyInto(requests);
84       syncRequests.clear();
85       localRound.done = true;
86       currentRound = new SyncRound();
87       channel.send(new JmsRequestGroup(requests));
88       notifyAll();
89     }
90     // else do nothing
91
}
92
93   /*
94    * @see org.objectweb.joram.client.jms.connection.RequestChannel#setTimer(java.util.Timer)
95    */

96   public void setTimer(Timer JavaDoc timer) {
97     channel.setTimer(timer);
98   }
99
100   /*
101    * @see org.objectweb.joram.client.jms.connection.RequestChannel#connect()
102    */

103   public void connect() throws Exception JavaDoc {
104     channel.connect();
105   }
106
107   /*
108    * @see org.objectweb.joram.client.jms.connection.RequestChannel#receive()
109    */

110   public AbstractJmsReply receive() throws Exception JavaDoc {
111     return channel.receive();
112   }
113
114   /*
115    * @see org.objectweb.joram.client.jms.connection.RequestChannel#close()
116    */

117   public void close() {
118     channel.close();
119   }
120   
121   private static class SyncRound {
122     private volatile boolean done = false;
123   }
124 }
125
Popular Tags