}
} else if (actionParam != null && actionParam.equals("ReceiveMessage")) {
String queueUrl = com.comcast.cqs.util.Util.getRelativeForAbsoluteQueueUrl(request.getRequestURL().toString());
CQSQueue queue;
int waitTimeSeconds = 0;
try {
queue = CQSCache.getCachedQueue(queueUrl);
if (queue != null && queue.getReceiveMessageWaitTimeSeconds() > 0) {
waitTimeSeconds = queue.getReceiveMessageWaitTimeSeconds();
}
} catch (Exception ex) {
logger.warn("event=lookup_queue queue_url=" + queueUrl, ex);
}
if (waitTimeSeconds > 0) {
asyncContext.setTimeout(waitTimeSeconds*1000);
((CQSHttpServletRequest)asyncContext.getRequest()).setWaitTime(waitTimeSeconds*1000);
logger.debug("event=set_queue_timeout secs=" + waitTimeSeconds + " queue_url=" + queueUrl);
} else {
asyncContext.setTimeout(HARD_TIMEOUT_SEC*1000);
logger.debug("event=set_default_timeout secs="+CMBProperties.getInstance().getCMBRequestTimeoutSec());
}
} else {
asyncContext.setTimeout(HARD_TIMEOUT_SEC*1000);
logger.debug("event=set_default_timeout secs=" + CMBProperties.getInstance().getCMBRequestTimeoutSec());
}
asyncContext.addListener(new AsyncListener() {
@Override
public void onComplete(AsyncEvent asyncEvent) throws IOException {
AsyncContext asyncContext = asyncEvent.getAsyncContext();
CQSHttpServletRequest request = (CQSHttpServletRequest)asyncContext.getRequest();
String action = request.getParameter("Action");
if(action.equals("ReceiveMessage")){
String lpValue = (String)request.getAttribute("lp");
if(lpValue!=null){
User user = authModule.getUserByRequest(request);
Object lp_ms = request.getAttribute("lp_ms");
String logLine = null;
if(lp_ms!=null){
logLine = getLogLine(asyncContext, request, user, (Long)request.getAttribute("lp_ms"), "ok");
}else{
logLine = getLogLine(asyncContext, request, user, 0, "ok");
}
logger.info(logLine);
}
}
}
@Override
public void onError(AsyncEvent asyncEvent) throws IOException {
int httpCode = CMBErrorCodes.InternalError.getHttpCode();
String code = CMBErrorCodes.InternalError.getCMBCode();
String message = "There is an internal problem with CMB";
if (asyncEvent.getThrowable() instanceof CMBException) {
httpCode = ((CMBException)asyncEvent.getThrowable()).getHttpCode();
code = ((CMBException)asyncEvent.getThrowable()).getCMBCode();
message = asyncEvent.getThrowable().getMessage();
}
String errXml = CMBControllerServlet.createErrorResponse(code, message);
HttpServletResponse response = ((HttpServletResponse)asyncEvent.getSuppliedResponse());
response.setStatus(httpCode);
Action.writeResponse(errXml, response);
if (!(asyncEvent.getSuppliedRequest() instanceof CQSHttpServletRequest)) {
logger.error("event=invalid_request stage=on_error");
return;
}
CQSQueue queue = ((CQSHttpServletRequest)asyncEvent.getSuppliedRequest()).getQueue();
AsyncContext asyncContext = asyncEvent.getAsyncContext();
if (queue != null) {
logger.info("event=on_error queue_url=" + queue.getAbsoluteUrl());
ConcurrentLinkedQueue<AsyncContext> queueContextsList = CQSLongPollReceiver.contextQueues.get(queue.getArn());
if (queueContextsList != null && asyncContext != null) {
queueContextsList.remove(asyncContext);
}
} else {
logger.info("event=on_error");
}
asyncContext.complete();
}
@Override
public void onTimeout(AsyncEvent asyncEvent) throws IOException {
if (!(asyncEvent.getSuppliedRequest() instanceof CQSHttpServletRequest)) {
logger.error("event=invalid_request stage=on_timeout");
return;
}
//first check if it is long poll receive, if so, complete and return.
AsyncContext asyncContext = asyncEvent.getAsyncContext();
CQSHttpServletRequest request = (CQSHttpServletRequest)asyncContext.getRequest();
String action = request.getParameter("Action");
if(action.equals("ReceiveMessage")){
String lpValue = (String)request.getAttribute("lp");
if(lpValue!=null){
String out = CQSMessagePopulator.getReceiveMessageResponseAfterSerializing(new ArrayList<CQSMessage>(), new ArrayList<String>());
asyncEvent.getSuppliedResponse().getWriter().println(out);
CQSQueue queue = ((CQSHttpServletRequest)asyncEvent.getSuppliedRequest()).getQueue();
asyncContext = asyncEvent.getAsyncContext();
if (queue != null) {
logger.debug("event=on_timeout queue_url=" + queue.getAbsoluteUrl());
ConcurrentLinkedQueue<AsyncContext> queueContextsList = CQSLongPollReceiver.contextQueues.get(queue.getArn());
if (queueContextsList != null && asyncContext != null) {
queueContextsList.remove(asyncContext);
}
} else {
logger.debug("event=on_timeout");
}
asyncContext.complete();
return;
}
}
//for other Time out show log error and return error response.
int httpCode = CMBErrorCodes.InternalError.getHttpCode();
String code = CMBErrorCodes.InternalError.getCMBCode();
String message = "CMB timeout after "+CMBProperties.getInstance().getCMBRequestTimeoutSec()+" seconds";
if (asyncEvent.getThrowable() instanceof CMBException) {
httpCode = ((CMBException)asyncEvent.getThrowable()).getHttpCode();
code = ((CMBException)asyncEvent.getThrowable()).getCMBCode();
message = asyncEvent.getThrowable().getMessage();
}
String errXml = CMBControllerServlet.createErrorResponse(code, message);
HttpServletResponse response = ((HttpServletResponse)asyncEvent.getSuppliedResponse());
response.setStatus(httpCode);
Action.writeResponse(errXml, response);
if (!(asyncEvent.getSuppliedRequest() instanceof CQSHttpServletRequest)) {
logger.error("event=invalid_request stage=on_timeout");
return;
}
CQSQueue queue = ((CQSHttpServletRequest)asyncEvent.getSuppliedRequest()).getQueue();
asyncContext = asyncEvent.getAsyncContext();
if (queue != null) {
logger.error("event=on_timeout queue_url=" + queue.getAbsoluteUrl());
ConcurrentLinkedQueue<AsyncContext> queueContextsList = CQSLongPollReceiver.contextQueues.get(queue.getArn());
if (queueContextsList != null && asyncContext != null) {
queueContextsList.remove(asyncContext);
}