ActiveMQ Plugin Development Guide and Examples
ActiveMQ provides a plugin development mechanism (http://activemq.apache.org/developing-plugins.html) that allows you to conveniently add various custom functionalities. The effect is similar to directly modifying ActiveMQ’s source code, but using plugins is much better than modifying source code in terms of both convenience and risk. In theory, we can implement almost any functionality imaginable through this approach. Development GuideFirst, you need to add the ActiveMQ dependency to your p...
Running Java Programs from the Command Line by Adding Third-Party Jars to the Classpath
When running a Java program from the command line, how do you include third-party jar packages? There are actually many methods, most of which involve tinkering with the classloader. Here I’ll introduce a relatively simple approach: adding all the required jar packages to the classpath before running. Specifically, you write a shell script that defines a parameter — you can call it CLASSPATH or something else. CLASSPATH=yourownjar.jar:xxx.jar:/xx/xx/xxx1.jar:”$CLASSPATH” ...
Fixing the SecurityException: Invalid Signature File Digest for Manifest Main Attributes in Fat Jars
Recently I encountered an error when trying to run a fat jar: 1234567891011121314151617181920Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:287) at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:240) at java.util.jar.JarVerifier.processEntry(JarVerifier.java:317) at java.util.jar.JarVerifier.update(JarVerifi...
Understanding Redis Cluster Through Practice
Redis Cluster is an official clustering solution provided by Redis. It has been available as a stable release since version 3.0, is widely adopted, and has stood the test of time. Personally, I believe it can fully replace other clustering solutions like Codis and Twemproxy. Cluster PrinciplesCluster implements data sharding. A Redis Cluster contains 16,384 hash slots. Any key can be mapped to a specific slot using the formula CRC16(key) % 16384. Which slot belongs to which node is predetermi...
An Approach to Storing Sets in HBase
As we know, HBase stores data as binary key-value pairs, unlike Redis which provides built-in support for various data structures. So how can we store set-type data in HBase? One approach is to serialize the entire set as a single object and store it in HBase. However, this means that for any subsequent add, delete, update, or query operation, you must first retrieve the entire stored content, make the necessary modifications, and then overwrite the original value. This approach is clearly no...
Log4j2 XML Configuration Examples and Differences from Log4j
1234567891011121314151617181920212223242526272829303132333435<?xml version="1.0" encoding="UTF-8" ?><configuration status="warn"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="[%p] %d %c %l - %m%n"/> </Console> <RollingFile name="activity" fileName="/opt/fox.log" filePattern="/opt/f...
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...