Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to encapsulate DynamicBrokerReader classes in Storm-kafka

Shulou Source: shulou.com Published: 2022-05-31 20:05:45 12月01日 Update

This article mainly introduces how to encapsulate the DynamicBrokerReader class in Storm-kafka, which is very detailed and has certain reference value. Friends who are interested must finish it!

Grasp the encapsulation class of DynamicBrokerReder-ZkBrokerReader in detail

Package com.mixbox.storm.kafka.trident;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.mixbox.storm.kafka.DynamicBrokersReader;import com.mixbox.storm.kafka.ZkHosts;import java.util.Map / * 2014-07-22 * getting GlobalPartitionInformation * * ZkBrokerReader in ZK is a simple package for DynamicBrokersReader * @ author Yin Shuai * / public class ZkBrokerReader implements IBrokerReader {public static final Logger LOG = LoggerFactory .getLogger (ZkBrokerReader.class); GlobalPartitionInformation cachedBrokers; DynamicBrokersReader reader; long lastRefreshTimeMs; long refreshMillis / * * @ param conf * @ param topic * specify the zkBrokerReader * @ param hosts * / public ZkBrokerReader of topic (Map conf, String topic, ZkHosts hosts) {reader = new DynamicBrokersReader (conf, hosts.brokerZkStr, hosts.brokerZkPath, topic) CachedBrokers = reader.getBrokerInfo (); lastRefreshTimeMs = System.currentTimeMillis (); refreshMillis = hosts.refreshFreqSecs * 1000L;} @ Override public GlobalPartitionInformation getCurrentBrokers () {long currTime = System.currentTimeMillis () / / simply specify how long you need to start refreshing Brokerlibiao if (currTime > lastRefreshTimeMs + refreshMillis) {LOG.info ("brokers need refreshing because" + refreshMillis + "ms have expired"); cachedBrokers = reader.getBrokerInfo () LastRefreshTimeMs = currTime;} return cachedBrokers;} @ Override public void close () {reader.close ();}}

An overview of our Code:

ZkBrokerReader is a simple encapsulation of DynamicBrokersReader, which holds two main Class in ZkBrokerReader

1 GlobalPartitionInformatio cachedBroker

2 DynamicBrokersReader reader

3 the latest refresh time of long lastRefreshTimeMs;

LastRefreshTimeMs = System.currentTimeMillis (); the latest refresh time is the current time of the system

4 long refreshMillis

RefreshMillis = host.refreshFreqSecs * 1000L sets the number of milliseconds to refresh

five

Public GlobalPartitionInformation getCurrentBrokers () {long currTime = System.currentTimeMillis (); / / it simply specifies how long you start refreshing Brokerlibiao if (currTime > lastRefreshTimeMs + refreshMillis) {LOG.info ("brokers need refreshing because" + refreshMillis + "ms have expired") CachedBrokers = reader.getBrokerInfo (); lastRefreshTimeMs = currTime;} return cachedBrokers;}

Every time getCurrentBrokers is called, System.currentTimeMillis will be fetched first when the current system time exceeds the earliest refresh time + refresh

The interval will go with the new again:

CachedBrokers = reader.getBrokerInfo (); every time the getBrokerInfo () method is called, it will be re-fetched in the zk

Once.

ZkBrokerReader is an encapsulation of DynamicBrokerReader, the Dynamic nature of DynamicBrokerReader is not a program dynamic factor, but simply in the process of reading ZK data, Zk data has changed dynamically?

The above is all the content of the article "how to encapsulate DynamicBrokerReader classes in Storm-kafka". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Time encapsulation middle content dynamics data articles systems values interests again just factors guys buddies properties methods more knowledge programs Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info Microsoft Shulou Technology Shulou Information