KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > handler > ManagedObjectRequestHandler


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

5 package com.tc.objectserver.handler;
6
7 import com.tc.async.api.AbstractEventHandler;
8 import com.tc.async.api.ConfigurationContext;
9 import com.tc.async.api.EventContext;
10 import com.tc.async.api.Sink;
11 import com.tc.logging.TCLogger;
12 import com.tc.logging.TCLogging;
13 import com.tc.net.protocol.tcm.ChannelID;
14 import com.tc.net.protocol.tcm.MessageChannel;
15 import com.tc.object.msg.RequestManagedObjectMessage;
16 import com.tc.object.net.ChannelStats;
17 import com.tc.objectserver.api.ObjectRequestManager;
18 import com.tc.objectserver.context.ManagedObjectRequestContext;
19 import com.tc.objectserver.core.api.ServerConfigurationContext;
20 import com.tc.objectserver.l1.api.ClientStateManager;
21 import com.tc.stats.counter.Counter;
22
23 import java.util.Collection JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * Converts the request into a call to the objectManager with the proper next steps initialized I'm not convinced that
28  * this stage is necessary. May be able to merge it with another stage.
29  *
30  * @author steve
31  */

32 public class ManagedObjectRequestHandler extends AbstractEventHandler {
33
34   private ObjectRequestManager objectRequestManager;
35   private ClientStateManager stateManager;
36   private ChannelStats channelStats;
37   private final Counter globalObjectRequestCounter;
38   private final Counter globalObjectFlushCounter;
39   private Sink respondObjectRequestSink;
40
41   private static final TCLogger logger = TCLogging.getLogger(ManagedObjectRequestHandler.class);
42
43   public ManagedObjectRequestHandler(Counter globalObjectRequestCounter, Counter globalObjectFlushCounter) {
44     this.globalObjectRequestCounter = globalObjectRequestCounter;
45     this.globalObjectFlushCounter = globalObjectFlushCounter;
46   }
47
48   public void handleEvent(EventContext context) {
49     if (context instanceof RequestManagedObjectMessage) {
50       handleEventFromClient((RequestManagedObjectMessage) context);
51     } else if (context instanceof ManagedObjectRequestContext) {
52       handleEventFromServer((ManagedObjectRequestContext) context);
53     }
54   }
55
56   private void handleEventFromServer(ManagedObjectRequestContext context) {
57     Collection JavaDoc ids = context.getLookupIDs();
58     // XXX::TODO:: Server initiated lookups are not updated to the channel counter for now
59
final int numObjectsRequested = ids.size();
60     if (numObjectsRequested != 0) {
61       globalObjectRequestCounter.increment(numObjectsRequested);
62     }
63     objectRequestManager.requestObjects(ids, context, context.getMaxRequestDepth());
64   }
65
66   private void handleEventFromClient(RequestManagedObjectMessage rmom) {
67
68     MessageChannel channel = rmom.getChannel();
69     Set JavaDoc requestedIDs = rmom.getObjectIDs();
70     ChannelID channelID = rmom.getChannelID();
71     Set JavaDoc removedIDs = rmom.getRemoved();
72     int maxRequestDepth = rmom.getRequestDepth();
73
74     final int numObjectsRequested = requestedIDs.size();
75     if (numObjectsRequested != 0) {
76       globalObjectRequestCounter.increment(numObjectsRequested);
77       channelStats.getCounter(channel, ChannelStats.OBJECT_REQUEST_RATE).increment(numObjectsRequested);
78     }
79
80     final int numObjectsRemoved = removedIDs.size();
81     if (numObjectsRemoved != 0) {
82       globalObjectFlushCounter.increment(numObjectsRemoved);
83       channelStats.getCounter(channel, ChannelStats.OBJECT_FLUSH_RATE).increment(numObjectsRemoved);
84     }
85
86     long t = System.currentTimeMillis();
87     stateManager.removeReferences(channelID, removedIDs);
88     t = System.currentTimeMillis() - t;
89     if (t > 1000 || numObjectsRemoved > 100000) {
90       logger.warn("Time to Remove " + numObjectsRemoved + " is " + t + " ms");
91     }
92     if (numObjectsRequested > 0) {
93       ManagedObjectRequestContext reqContext = new ManagedObjectRequestContext(channelID, rmom.getRequestID(),
94                                                                                requestedIDs, maxRequestDepth,
95                                                                                this.respondObjectRequestSink);
96       objectRequestManager.requestObjects(requestedIDs, reqContext, maxRequestDepth);
97     }
98   }
99
100   public void initialize(ConfigurationContext context) {
101     super.initialize(context);
102     ServerConfigurationContext oscc = (ServerConfigurationContext) context;
103     objectRequestManager = oscc.getObjectRequestManager();
104     stateManager = oscc.getClientStateManager();
105     channelStats = oscc.getChannelStats();
106     this.respondObjectRequestSink = oscc.getStage(ServerConfigurationContext.RESPOND_TO_OBJECT_REQUEST_STAGE).getSink();
107   }
108
109 }
110
Popular Tags