Comparing Java Lists Without Implementing equals
In Java, there are several ways to compare whether two Lists have the same values regardless of order, such as sorting followed by using equals, or executing containsAll in both directions, etc. All of these methods require us to implement the equals and hashCode methods for the element classes in the list. However, there are special cases where it’s not convenient for us to implement the equals method of a class — for example, when it comes from an ancient third-party JAR package where modif...
Implementing Redis hmsetnx Command Using Lua Scripts and Jedis
The set family of commands in Redis (including set, hset, etc.) basically have two versions: pure set and setnx. Setnx means “set not exist”, which only executes the set operation when the key does not exist, without overwriting the existing value. However, for the hmset command, neither Redis itself nor Jedis provides an nx version. Of course, the hset command has a corresponding hsetnx version. Hmset means “multi hset” — it can operate on multiple key-value pairs at once, thereby reducing n...
Embedding Hawtio Monitoring in a Standalone Java Application
Hawtio (hawt.io) is an open-source monitoring system that provides multiple startup methods. It can run as a standalone JAR or WAR package and remotely connect to other applications for monitoring, or it can be directly embedded into our own applications. This article introduces how to embed Hawtio in a standalone Java application, corresponding to the official documentation (http://hawt.io/getstarted/index.html) section “Using hawtio inside a stand alone Java application”. However, that se...
Java Serialization Performance Comparison: kryo/protobuf/protostuff vs Json
For serializing a Java object, I wanted to test the differences in time and space performance between using JSON and using general-purpose serialization tools. For JSON, I chose fastjson. For serialization tools, I used protostuff and kryo. Why not protobuf? Because for an existing Java class with hundreds of properties, creating a matching proto file feels somewhat inhumane. Protostuff is an improved version of protobuf — it can directly serialize a Java object, and the usage is somewhat s...
Java Distributed Lock Practice for Beginners
This article covers usage only, no theory. Simple and straightforward. A distributed lock is, as the name suggests, a lock for distributed systems, used in scenarios where multiple instances need coordination. For example, if a service is deployed on multiple machines and may operate on the same record in a database, a distributed lock becomes necessary. There are many ways to implement distributed locks. Generally, a lock requires an entity to represent it — when acquiring the lock, this ent...
LeetCode Problem 3: Longest Substring Without Repeating Characters
ProblemGiven a string, find the length of the longest substring without repeating characters. Examples: Given “abcabcbb”, the answer is “abc”, which the length is 3. Given “bbbbb”, the answer is “b”, with the length of 1. Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring. In other words, given a string, output the length of the longest substring without repeating characters. ApproachThe problem can ...
Investigating a Kafka Disk Usage Spike: Data Compression, Batch Sending, and More
The ProblemOur company needs to receive a large amount of external data from various sources, including IBM MQ, ActiveMQ, Redis pub/sub, and more. To funnel all this data into our internal AMQ/Kafka, we previously ran a large number of separate processes, which were quite complex to manage. Recently, we consolidated these processes using Apache Camel. A few hours after deployment, Kafka disk space alerts started firing. We initially determined that this was caused by the new deploym...
The Magic of 128 in Java: Understanding Integer Caching
The Integer class is essentially just an ordinary Java class — even if the values are the same, they are different objects. For example: Integer a = <span class="hljs-number">148</span>; Integer b = <span class="hljs-number">148</span>; System.out.println(a==b);` The output here is false. Easy to understand. However, if you change the value to a number below 128, such as 48: <pre class="prettyprint">` ...
Storm/JStorm Ecosystem and Peripheral Tools: Connecting Storm to ActiveMQ, Kafka, HDFS and More
Storm has a very rich surrounding ecosystem. There are ready-made toolkits available for interacting with Kafka, ActiveMQ, HDFS, HBase, and others. Most of these tools, including those introduced today, can also be used normally in JStorm. storm-jmsImplements interaction with JMS implementations such as ActiveMQ. Here we mainly introduce JmsSpout. Since sending queue data in Storm is no different from regular Java programs, wrapping it in a dedicated bolt seems unnecessary. https://github.com...
JStorm UI Introduction
UI OverviewCompared to Storm, JStorm’s UI provides more detailed monitoring items. The UI itself is a WAR package running in Tomcat, making secondary development relatively easy. Cluster PageCluster Summary, Cluster Stats, Topology SummaryOverall cluster information. The conf section contains the configuration of the nimbus node. Topology SummaryA list of all currently running topologies along with summary information. The conf section corresponds to topology-specific configuration items. Sup...














