KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > mom > proxies > FlowControl


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2004 - ScalAgent Distributed Technologies
4  * Copyright (C) 2004 - France Telecom R&D
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.mom.proxies;
24
25 public class FlowControl {
26   /** Flow control. */
27   private static Object JavaDoc lock = new Object JavaDoc();
28   private static int inFlow =
29       org.objectweb.joram.mom.proxies.ConnectionManager.inFlow;
30   private static long flowControl = 0;
31   private static long start = 0L;
32   private static long end = 0L;
33   private static int nbmsg = 0;
34
35   public static void flowControl() {
36     // Flow control: gives the opportunity to other threads to consume the
37
// messages.
38
if (inFlow != -1) {
39       synchronized (lock) {
40         if (start == 0L) start = System.currentTimeMillis();
41         nbmsg += 1;
42         if (nbmsg == inFlow) {
43           end = System.currentTimeMillis();
44           flowControl = 1000L - (end - start);
45           if (flowControl > 0) {
46             try {
47               Thread.sleep(flowControl);
48             } catch (InterruptedException JavaDoc exc) {}
49             start = System.currentTimeMillis();
50           } else {
51             start = end;
52           }
53           nbmsg = 0;
54         }
55       }
56     }
57   }
58 }
59
Popular Tags