KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > idprovider > impl > RemoteObjectIDBatchSequenceProvider


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.idprovider.impl;
5
6 import com.tc.async.api.AbstractEventHandler;
7 import com.tc.async.api.EventContext;
8 import com.tc.object.msg.ObjectIDBatchRequestMessage;
9 import com.tc.object.msg.ObjectIDBatchRequestMessageFactory;
10 import com.tc.object.msg.ObjectIDBatchRequestResponseMessage;
11 import com.tc.util.Assert;
12 import com.tc.util.sequence.BatchSequenceProvider;
13 import com.tc.util.sequence.BatchSequenceReceiver;
14
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 /**
19  * @author steve manages requests to servers
20  */

21 public class RemoteObjectIDBatchSequenceProvider extends AbstractEventHandler implements BatchSequenceProvider {
22   private long requestID;
23   private Map JavaDoc requests = new HashMap JavaDoc();
24   private final ObjectIDBatchRequestMessageFactory mf;
25
26   public RemoteObjectIDBatchSequenceProvider(ObjectIDBatchRequestMessageFactory mf) {
27     this.mf = mf;
28   }
29
30   public synchronized void requestBatch(BatchSequenceReceiver receiver, int size) {
31     requests.put(new Long JavaDoc(requestID), new Request(receiver, size, requestID));
32     ObjectIDBatchRequestMessage m = mf.newObjectIDBatchRequestMessage();
33     m.initialize(requestID++, size);
34     m.send();
35   }
36
37   private static class Request {
38     private BatchSequenceReceiver receiver;
39
40     private Request(BatchSequenceReceiver receiver, int batchSize, long requestID) {
41       this.receiver = receiver;
42     }
43
44     public BatchSequenceReceiver getReceiver() {
45       return receiver;
46     }
47   }
48
49   public void handleEvent(EventContext context) {
50     ObjectIDBatchRequestResponseMessage m = (ObjectIDBatchRequestResponseMessage) context;
51     Long JavaDoc reqID = new Long JavaDoc(m.getRequestID());
52     Request r = (Request) requests.get(reqID);
53     Assert.eval(r != null);
54     requests.remove(reqID);
55     r.getReceiver().setNextBatch(m.getBatchStart(), m.getBatchEnd());
56   }
57 }
Popular Tags