1 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 ; 16 import java.util.Map ; 17 18 21 public class RemoteObjectIDBatchSequenceProvider extends AbstractEventHandler implements BatchSequenceProvider { 22 private long requestID; 23 private Map requests = new HashMap (); 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 (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 reqID = new Long (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 |