Skip to content

Instantly share code, notes, and snippets.

@cloudbow
Created March 12, 2014 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloudbow/9513470 to your computer and use it in GitHub Desktop.
Save cloudbow/9513470 to your computer and use it in GitHub Desktop.
inbound handler
package gcm.netty.handlers;
import gcm.netty.constants.ApplicationContextComponents;
import gcm.netty.constants.TraceLogs;
import gcm.netty.queues.batch.BatchGcmMessageQueue;
import gcm.netty.syslog.Statistics;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.CharsetUtil;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import platform.pns.common.factory.ConnectionPoolManagerFactory;
import platform.pns.common.handlers.ext.DecoderAction;
/**
* The Class BatchApnsMessagesHandler.
* @author arung
*/
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Component
public class BatchGcmMessagesHandler extends SimpleChannelInboundHandler<FullHttpResponse> {
/** The Constant logger. */
private static final Logger logger = Logger.getLogger(BatchGcmMessagesHandler.class.getName());
/** The application context. */
@Autowired
ApplicationContext applicationContext;
/** The batch message queue. */
@Autowired
BatchGcmMessageQueue batchAdmMessageQueue;
@Autowired
Statistics statistics;
/** The connection pool manager factory. */
@Autowired
ConnectionPoolManagerFactory connectionPoolManagerFactory;
/*
* (non-Javadoc)
* @see
* io.netty.channel.SimpleChannelInboundHandler#messageReceived(io.netty
* .channel.ChannelHandlerContext, java.lang.Object)
*/
@Override
protected void messageReceived(final ChannelHandlerContext ctx, final FullHttpResponse msg) throws Exception {
BatchGcmMessagesHandler.logger.trace(TraceLogs.MSG_CONTENT_TO_STRING_CHARSET_UTIL_UTF_8
+ msg.content().toString(CharsetUtil.UTF_8));
BatchGcmMessagesHandler.logger.trace(msg);
// Do zeus integration.
if (msg != null && msg.getStatus().equals(HttpResponseStatus.OK)) {
try {
statistics.addSuccesMessages(1l);
statistics.updateLastMessageTime(System.nanoTime());
final DecoderAction action = (DecoderAction) applicationContext
.getBean(ApplicationContextComponents.DECODER_PROVIDER); // Must be defined in app
// context;
if (action != null) {
action.afterDecode(msg);
}
} catch (final NoSuchBeanDefinitionException e) {
// BatchGcmMessagesHandler.logger.error(TraceLogs.BEAN_UNDEFINED);
}
}
}
/*
* (non-Javadoc)
* @see
* io.netty.channel.ChannelHandlerAdapter#exceptionCaught(io.netty.channel
* .ChannelHandlerContext, java.lang.Throwable)
*/
@Override
public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) throws Exception {
BatchGcmMessagesHandler.logger.trace(TraceLogs.UNEXPECTED_EXCEPTION_FROM_DOWNSTREAM + cause);
BatchGcmMessagesHandler.logger.trace(TraceLogs.CLOSING_THE_CONTEXT_AND_RETRYING);
if (cause instanceof IOException) {
if (ctx.channel().isActive()) {
ctx.close();
}
// batchMessageQueue.pushQueue(ctx.pipeline()
// .get(BatchMessageCodec.class).getApnsBatchMessage());
} else {
BatchGcmMessagesHandler.logger.trace(TraceLogs.PROGRAMMER_ERROR_CONNECTION_NOT_TERMINATED);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment