KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > impl > PersistentBatchSequenceProvider


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.objectserver.persistence.impl;
5
6 import com.tc.async.api.AbstractEventHandler;
7 import com.tc.async.api.EventContext;
8 import com.tc.async.api.Sink;
9 import com.tc.objectserver.persistence.api.PersistentSequence;
10 import com.tc.util.sequence.BatchSequenceProvider;
11 import com.tc.util.sequence.BatchSequenceReceiver;
12
13 public class PersistentBatchSequenceProvider extends AbstractEventHandler implements BatchSequenceProvider {
14
15   private Sink requestBatchSink;
16   private final PersistentSequence sequence;
17   
18   public PersistentBatchSequenceProvider(PersistentSequence sequence) {
19     this.sequence = sequence;
20   }
21
22   public void setRequestBatchSink(Sink sink) {
23     this.requestBatchSink = sink;
24   }
25   
26   // EventHandler interface
27
public void handleEvent(EventContext context) {
28     GlobalTransactionIDBatchRequestContext ctxt = (GlobalTransactionIDBatchRequestContext)context;
29     BatchSequenceReceiver receiver = ctxt.getReceiver();
30     long start = sequence.nextBatch(ctxt.getBatchSize());
31     receiver.setNextBatch(start, start + ctxt.getBatchSize());
32   }
33
34   // BatchSequenceProvider interface
35
public void requestBatch(BatchSequenceReceiver receiver, int size) {
36     this.requestBatchSink.add(new GlobalTransactionIDBatchRequestContext(receiver, size));
37   }
38
39   public static final class GlobalTransactionIDBatchRequestContext implements EventContext {
40     private final BatchSequenceReceiver receiver;
41     private final int size;
42
43     public GlobalTransactionIDBatchRequestContext(BatchSequenceReceiver receiver, int size) {
44       this.receiver = receiver;
45       this.size = size;
46     }
47     
48     public BatchSequenceReceiver getReceiver() {
49       return this.receiver;
50     }
51     
52     public int getBatchSize() {
53       return this.size;
54     }
55   }
56 }
57
Popular Tags