Java tutorial
/** * Copyright (c) Acroquest Technology Co, Ltd. All Rights Reserved. * Please read the associated COPYRIGHTS file for more details. * * THE SOFTWARE IS PROVIDED BY Acroquest Technolog Co., Ltd., * WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY * CLAIM, DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ package acromusashi.stream.component.kestrel.spout; import java.text.MessageFormat; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import net.sf.json.JSONException; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import acromusashi.stream.constants.FieldName; import acromusashi.stream.util.JsonValueExtractor; import backtype.storm.spout.Scheme; import backtype.storm.spout.SchemeAsMultiScheme; import backtype.storm.spout.SpoutOutputCollector; import backtype.storm.task.TopologyContext; import backtype.storm.tuple.Fields; /** * Kestrel?JSON??????????Bolt???Spout<br/> * <ol> * <li> Kestrel?JSON????</li> * <li>Spout?? ???JSON?????</li> * <li> ?JSON?</li> * </ol> * * ??????<br/> * ?????Spout?????<br/> * <br/>------------------------------------------------- * <br/> * {<br/> * "?":<br/> * {<br/> * "?": "?",<br/> * <br/> * }<br/> * <br/> * }<br/> * ------------------------------------------------- * <br/> * * @author kimura */ public class KestrelJsonSpout extends KestrelSpout { /** Kestrel???? */ public static final String KESTREL_SERVERS = "kestrel.servers"; /** Kestrel????? */ public static final String KESTREL_QUEUE = "kestrel.queue"; /** Kestrel?? */ public static final String KESTREL_TIMEOUT = "kestrel.timeout"; /** Kestrel???? */ public static final String KESTREL_BATCH_SIZE = "kestrel.batch.size"; /** Kestrel??? */ public static final String KESTREL_BLACKLISTTIME = "kestrel.blacklist.time"; /** header?? */ private static final String HEADER_TAG = "header"; /** messageKey?? */ private static final String MESSAGEKEY_TAG = "messageKey"; /** serialVersionUID */ private static final long serialVersionUID = -3331796053960250415L; /** logger */ private static final Logger logger = LoggerFactory.getLogger(KestrelJsonSpout.class); /** ?? */ protected transient RestrictWatcher restrictWatcher; /** ? */ private String restrictFilePath; /** * * * @param hosts ????:port?? * @param queueName ??? * @param scheme ??? */ public KestrelJsonSpout(List<String> hosts, String queueName, Scheme scheme) { super(hosts, queueName, new SchemeAsMultiScheme(scheme)); if (StringUtils.isEmpty(queueName) == true) { throw new IllegalArgumentException("Must configure queueName"); } if (scheme == null) { throw new IllegalArgumentException("Must configure scheme"); } } /** * {@inheritDoc} */ @SuppressWarnings({ "rawtypes" }) @Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { super.open(conf, context, collector); Number timeout = (Number) conf.get(KESTREL_TIMEOUT); this.messageTimeoutMs = (int) TimeUnit.SECONDS.toMillis(timeout.intValue()); this.restrictWatcher = new RestrictWatcher(this.restrictFilePath); // Spout????????Queue?Queue??? int componentIndex = context.getThisTaskIndex(); String baseQueueName = getQueueName(); String queueName = baseQueueName + "_" + componentIndex; setQueueName(queueName); } /** * {@inheritDoc} */ @Override public Fields getOutputFields() { return new Fields(Arrays.asList(FieldName.MESSAGE_KEY, FieldName.MESSAGE_VALUE)); } /** * {@inheritDoc} */ @Override protected boolean isRestricted() { return this.restrictWatcher.isRestrict(); } /** * {@inheritDoc} */ @Override protected EmitItem generateEmitItem(List<Object> retItems, KestrelSourceId sourceId) { String groupingInfo = null; String jsonMessage = (String) retItems.get(0); try { // ?? groupingInfo = JsonValueExtractor.extractValue(jsonMessage, HEADER_TAG, MESSAGEKEY_TAG); } catch (JSONException jex) { String logFormat = "Received message is not json. : message={0}"; logger.debug(MessageFormat.format(logFormat, jsonMessage), jex); return null; } // ???Bolt??? EmitItem generatedItem = new EmitItem(Arrays.asList((Object) groupingInfo, jsonMessage), sourceId); return generatedItem; } /** * @param restrictFilePath the restrictFilePath to set */ public void setRestrictFilePath(String restrictFilePath) { this.restrictFilePath = restrictFilePath; } }