Comparing Java Lists Without Implementing equals
Problem StatementIn 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 pa...
Implementing Redis hmsetnx Command Using Lua Scripts and Jedis
In Redis’s Hash operations, HSETNX ensures that a field is only written when it does not already exist. However, HMSET (which sets multiple fields in batch) has no corresponding HMSETNX command. If you need to batch-initialize a cache without overwriting existing data, issuing N HSETNX commands would require N network round-trips — terrible performance in batch scenarios. This can be solved elegantly with a Lua script — pushing the logic to the Redis server side and requiring only a single ne...
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
Why Compare Serialization SolutionsFor 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 ...
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
Java’s Integer caching mechanism is a frequently asked interview topic and a common source of pitfalls in daily development. To understand it, let’s start with a counterintuitive experiment. The Curious Case of 1281234567Integer a = 148;Integer b = 148;System.out.println(a == b); // false — as expected, two different objectsInteger c = 48;Integer d = 48;System.out.println(c == d); // true — wait, why? The same == comparison returns false for 148 but true for 48. The reason lies in Intege...
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...













