Created
March 12, 2014 18:21
-
-
Save cloudbow/9513080 to your computer and use it in GitHub Desktop.
monotonous loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gcm.netty.codec; | |
import gcm.netty.config.GcmConfig; | |
import gcm.netty.constants.ErrorConstants; | |
import gcm.netty.model.BatchFullGcmMessage; | |
import gcm.netty.syslog.GcmSysLogger; | |
import gcm.netty.syslog.Statistics; | |
import io.netty.buffer.ByteBuf; | |
import io.netty.channel.ChannelHandlerContext; | |
import io.netty.handler.codec.MessageToMessageEncoder; | |
import io.netty.handler.codec.http.DefaultFullHttpRequest; | |
import io.netty.handler.codec.http.FullHttpRequest; | |
import io.netty.handler.codec.http.HttpHeaders; | |
import io.netty.handler.codec.http.HttpMethod; | |
import io.netty.handler.codec.http.HttpVersion; | |
import io.netty.util.CharsetUtil; | |
import java.util.List; | |
import javax.json.Json; | |
import javax.json.stream.JsonGeneratorFactory; | |
import org.apache.log4j.Logger; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.config.ConfigurableBeanFactory; | |
import org.springframework.context.annotation.Scope; | |
import org.springframework.stereotype.Component; | |
import platform.pns.common.constants.PlatformConstants; | |
import platform.pns.common.model.gcm.impl.GcmMessage; | |
import platform.pns.common.syslog.AbstractSysLogger.ErrorContexts; | |
import platform.pns.common.syslog.AbstractSysLogger.Operation; | |
/** | |
* The Class GcmBatchMessageEncoder. | |
* @author arung | |
*/ | |
@Component | |
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) | |
public class GcmBatchMessageEncoder extends MessageToMessageEncoder<BatchFullGcmMessage> { | |
/** The logger. */ | |
private static final Logger logger = Logger.getLogger(GcmBatchMessageEncoder.class); | |
/** The gcm config. */ | |
@Autowired | |
private GcmConfig gcmConfig; | |
@Autowired | |
private GcmSysLogger gcmSysLogger; | |
@Autowired | |
private Statistics statistics; | |
/** The json generator factory. */ | |
private final JsonGeneratorFactory jsonGeneratorFactory = Json.createGeneratorFactory(null); | |
/** | |
* Instantiates a new gcm batch message encoder. | |
*/ | |
public GcmBatchMessageEncoder() { | |
} | |
/* | |
* (non-Javadoc) | |
* @see | |
* io.netty.handler.codec.MessageToMessageEncoder#encode(io.netty.channel | |
* .ChannelHandlerContext, java.lang.Object, java.util.List) | |
*/ | |
@Override | |
protected void encode(final ChannelHandlerContext ctx, final BatchFullGcmMessage msg, final List<Object> out) | |
throws Exception { | |
final String error = platform.pns.common.syslog.AbstractSysLogger.Error.SUCCESS; | |
final String errorContext = ErrorContexts.SUCCESS_CONTEXT; | |
final int errorCode = ErrorConstants.NO_ERROR; | |
final String operation = Operation.BATCH_GCM_ENCODE; | |
final long d1 = System.nanoTime(); | |
try { | |
for (final GcmMessage aMsg : msg.invariantMessages()) { | |
final String gcmUrl = gcmConfig.getGcmSendUrl(); | |
final String requestBody = aMsg.getPayLoad(); | |
GcmBatchMessageEncoder.logger.trace("post body is :" + requestBody); | |
final ByteBuf buf = ctx.alloc().directBuffer(); | |
buf.writeBytes(requestBody.getBytes(CharsetUtil.UTF_8)); | |
final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, | |
gcmUrl, buf); | |
// final ByteBuf buf2 = Unpooled.copiedBuffer("", | |
// CharsetUtil.UTF_8); | |
// Add headers | |
request.headers().add(gcmConfig.getDefaultHeader(msg.getAppId())); | |
request.headers().add(HttpHeaders.Names.CONTENT_LENGTH, buf.readableBytes()); | |
GcmBatchMessageEncoder.logger.trace("Making request == " + request); | |
out.add(request); | |
} | |
} finally { | |
final long opTime = gcmSysLogger.getEndTime(d1); | |
statistics.addTotalTime(opTime); | |
statistics.addMessageCount(msg.size()); | |
if (gcmConfig.isSyslogEnabled()) { | |
gcmSysLogger.sendBatchSyslog(error, errorCode, errorContext, operation, String.valueOf(opTime), | |
msg.getAppId(), msg.getTxId(), PlatformConstants.APNS, msg.size()); | |
} | |
} | |
} | |
/** | |
* Gets the json generator factory. | |
* @return the json generator factory | |
*/ | |
public JsonGeneratorFactory getJsonGeneratorFactory() { | |
return jsonGeneratorFactory; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment