ChannelHandlerContext ctx, MessageEvent e) {
if (LOG.isTraceEnabled()) {
LOG.trace("messageReceived: Got " + e.getMessage().getClass());
}
WritableRequest writableRequest = (WritableRequest) e.getMessage();
// Simulate a closed connection on the first request (if desired)
if (closeFirstRequest && !ALREADY_CLOSED_FIRST_REQUEST) {
LOG.info("messageReceived: Simulating closing channel on first " +
"request " + writableRequest.getRequestId() + " from " +
writableRequest.getClientId());
ALREADY_CLOSED_FIRST_REQUEST = true;
ctx.getChannel().close();
return;
}
// Only execute this request exactly once
int alreadyDone = 1;
if (workerRequestReservedMap.reserveRequest(
writableRequest.getClientId(),
writableRequest.getRequestId())) {
if (LOG.isDebugEnabled()) {
startProcessingNanoseconds = TIME.getNanoseconds();
}
processRequest((R) writableRequest);
if (LOG.isDebugEnabled()) {
LOG.debug("messageReceived: Processing client " +
writableRequest.getClientId() + ", " +
"requestId " + writableRequest.getRequestId() +
", " + writableRequest.getType() + " took " +
Times.getNanosSince(TIME, startProcessingNanoseconds) + " ns");
}
alreadyDone = 0;
} else {
LOG.info("messageReceived: Request id " +
writableRequest.getRequestId() + " from client " +
writableRequest.getClientId() +
" was already processed, " +
"not processing again.");
}
// Send the response with the request id
ChannelBuffer buffer = ChannelBuffers.directBuffer(RESPONSE_BYTES);
buffer.writeInt(myTaskInfo.getTaskId());
buffer.writeLong(writableRequest.getRequestId());
buffer.writeByte(alreadyDone);
e.getChannel().write(buffer);
}