Sunday, November 17, 2019

Docker Compose Getting Started with JAVA RocketMQ


Just want to try Docker Compose...


Install Docker Compose on Ubuntu 18.04

JAVA JMS: RocketMQ NameServer Broker Cluster

Simple Note

Go to Dockerfile folder,

Add the docker-compose.yml below into it

then run docker-compose up

Docker Compose will build image and run container with specified config value

Testing Video


Dockerfile define "how to create the container"

docker-compose.yml describe "how to config/run containers"


Get started with Docker Compose

Compose file version 3 reference

Thursday, November 14, 2019

Install Docker Compose on Ubuntu 18.04

Simple Note

Just several lines of commands

# Run this command to download the current stable release of Docker Compose
# To install a different version of Compose, substitute 1.24.1 with the version of Compose you want to use.
sudo curl -L "$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# Apply executable permissions to the binary:
sudo chmod +x /usr/local/bin/docker-compose

# Test the installation.
docker-compose --version

# done


Install Docker Compose

Tuesday, November 12, 2019

JAVA Selenium Visual Testing


Visual Testing is convenient, let you find any change of a web page in one line.


You probably will want to view the articles in this blog below before reading this article

JAVA Selenium Browser Automation Getting Started

JAVA Selenium Commands Testing

JAVA Image Basic Operations

Java Diff Image and Highlight Differences

Simple Note

Testing file

Visual Testing Implementation

After the testing file is executed, you should find some images in the folder selenium/visualTesting/123456 under your project folder.


Converting BufferedImage to ByteBuffer

How to make a hashcode(integer value) positive

How to take Re-entrant lock on String value?

Thursday, November 7, 2019

Java Diff Image and Highlight Differences


I want to implement visual testing with Selenium.

There is already a library romankh3/image-comparison

but it says "with the same sizes" so probably not suitable in some cases.

Simple Note

Test file

Diff implementation

Assume expected image aa.png and actual image cc.png
the diff result will be output_image.png


How do you clone a BufferedImage

calculating distance between a point and a rectangular box

Filling a Multidimensional Array using a Stream

Sunday, November 3, 2019

JAVA Image Basic Operations


I want to implement visual testing with Selenium screenshot so need to know basic operations of Image in JAVA.

Simple Note

Testing file
Including read image file, get information of each pixel, draw rectangle on loaded image then save it.

Pixel class
for store information of each pixel


Java - get pixel array from image

Java 8: Replace traditional for loops with IntStreams

Thursday, October 17, 2019

Monday, October 14, 2019

JAVA JMS: RocketMQ NameServer Broker Cluster

Simple Note

After bind necessary ports
(see Connect to VirtualBox Guest Machine from Host OS with NAT Port Forwarding)

Creat Docker Image with Dockerfile
(see Create Dockerfile for Docker Image of RocketMQ NameServer)

Can give it a try finally...

Testing Video

The command for start broker is slightly different, add -c to use specific config and specify multiple nameServer address


mqbroker.cmd -c ../conf/2m-2s-sync/ -n localhost:9487;localhost:9478 autoCreateTopicEnable=true

All files in the test project


Saturday, October 12, 2019

Create Dockerfile for Docker Image of RocketMQ NameServer

Simple Note

Please refer to this Dockerfile  and


Docker run reference

docker build

Connect to VirtualBox Guest Machine from Host OS with NAT Port Forwarding


I want to connect to NameServers in Docker Containers,
and the Docker installed on Ubuntu 18.04,
and the Ubuntu 18.04 running in VirtualBox on my Windows Host OS,

... so this is required.

Simple Notes

Step 1: Right click on stopped (power off) machine, click "Settings..."

Step 2: Click Network -> Advanced -> Port Forwarding

Step 3: Click the add Sign, input Host Port and Guest Port, click OK

Now the request to localhost:9487 will be forwarded to the Guest Machine


How to Forward Ports to a Virtual Machine and Use It as a Server

Monday, October 7, 2019

Install Docker on Ubuntu 18.04


I want to try RocketMQ Cluster with Multi-NameServers/Brokers, but it seems change port of NameServer is not supported...

So want to run 2 NameServers in 2 Docker Containers and bind to different ports for testing.

Simple Note

Install Docker on Ubuntu 18.04 running in VirtualBox

# Uninstall old versions
sudo apt-get remove docker docker-engine containerd runc

## Update the apt package index
sudo apt-get update

## Install packages to allow apt to use a repository over HTTPS
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \

## Add Docker’s official GPG key
curl -fsSL | sudo apt-key add -

## Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.
sudo apt-key fingerprint 0EBFCD88

## Use the following command to set up the stable repository
sudo add-apt-repository \
   "deb [arch=amd64] \
   $(lsb_release -cs) \

## Update the apt package index.
sudo apt-get update

## Install the latest version of Docker Engine - Community and containerd, or go to the next step to install a specific version:
sudo apt-get install docker-ce docker-ce-cli

## Verify that Docker Engine - Community is installed correctly by running the hello-world image.
sudo docker run hello-world


Get Docker Engine - Community for Ubuntu

RocketMQ not support modify nameServer port

Friday, October 4, 2019

JAVA JMS: RocketMQ Getting Started

Simple Notes

1. Download NameServer and Broker
  Go to
  Download release zip file
  (I choose 4.5.2/

2. Extract it
  Assume I extract it to

3. Start NameServer
  Open cmd, run commands below
  cd D:\things\Downloads\rocketmq\rocketmq-all-4.5.2-bin-release\bin
  set JAVA_HOME=D:\things\programs\Java\jdk1.8.0_221
  set ROCKETMQ_HOME=D:\things\Downloads\rocketmq\rocketmq-all-4.5.2-bin-release\
  mqnamesrv.cmd -n localhost:9876

4. Start Broker
  Open cmd, run commands below
  cd D:\things\Downloads\rocketmq\rocketmq-all-4.5.2-bin-release\bin
  set JAVA_HOME=D:\things\programs\Java\jdk1.8.0_221
  set ROCKETMQ_HOME=D:\things\Downloads\rocketmq\rocketmq-all-4.5.2-bin-release\
  mqbroker.cmd -n localhost:9876 autoCreateTopicEnable=true

5. Write Consumer and Producer for test ; ; and this is a maven project, so pom.xml
  Run TestConsumer and TestProducer, you will see something in the console.


Quick Start

Simple Message Example

Thursday, October 3, 2019

JAVA RSA Sign Data and Verify

Simple Note

Add Sign data by Signature with Private Key and verify data by Signature with Public Key

Also add Test Cases and Performance Testing

and update the Performance Testing google sheet

JAVA RSA Performance Testing


Java Cryptography Architecture (JCA) Reference Guide
Generating and Verifying a Signature Using Generated Keys

Java Doc of Signature

Sunday, September 29, 2019

JAVA RSA Generate KeyPair, Encryption, Decryption and Performance Testing


To try RSA Enc/Dec and check its performance.

Simple Note

Test file


Performance testing


Move AESUtils.encrypt and AESUtils.decrypt to CryptoUtils since they are no different from RSA encrypt/decrypt.

Still wrap them in AESUtils/RSAUtils for convenience.
(Can import less class) (Just one class less)


Performance Testing

Test over
PublicKey/PrivateKey, Encryption/Decryption,
Create Cipher each time/Reuse Cipher,
Key Size 1024/2048/4096/8192 bits
total 32 cases, 100 runs each

Results listed in the google sheet
JAVA RSA Performance Testing

Not very accurate but...


1. PublicKey Enc/Dec is faster than PrivateKey Enc/Dec

2. Key Size affects, the longer the slower.

3. Reuse Cipher similar to create Cipher each time, sometimes even slower.


1. Use proper Key Size, 2048 probably a good choice.

2. Can create Cipher each time, no need to cache it.

3. Can also cache and reuse Cipher, will slightly increase performance in most cases.


Java: How can I generate PrivateKey from a string?

Load RSA public key from file

Friday, September 27, 2019

JAVA AES Performance Testing


Want to know the better choice of coding.

Simple Note

Test functions in

Also use a simple helper class for performance testing

Test Cases

100 runs each

1. testCreateInstanceEachTimeCommon: Simple AES with pre-generated key, no salt, create Cipher each time

2. testReuseInstanceCommon: Simple AES with pre-generated key, no salt, reuse Cipher

3. testCreateInstanceEachTimePBK: Use SecretKeyFactory and PBEKeySpec to generate SecretKey with PBKDF2WithHmacSHA256 algorithm, generate key and create Cipher each time

4. testReuseInstancePBK: Use SecretKeyFactory and PBEKeySpec to generate SecretKey with PBKDF2WithHmacSHA256 algorithm, reuse Cipher



1. Use pre-generated key is much faster
(testCreateInstanceEachTimeCommon is much faster than testCreateInstanceEachTimePBK).

2. Reuse Cipher is much faster.

3. Once the Cipher is created, the speed of Encryption are similar between Common and PBK
(the 90% time of testReuseInstancePBK even faster than testReuseInstanceCommon)


Do not create Cipher each time, try to reuse it.


Round double
Round a double to 2 decimal places

List Initial Size (need to get the fastest 90% so...)
Initial size for the ArrayList

Thursday, September 26, 2019

JAVA AES Encrypt Decrypt with Any Key, IV and Salt


Convenient, that's all

Simple Note

Test File:



Java MessageDigest supported algorithms? MD5, SHA-1, SHA-256
Class MessageDigest

How to Dupe a char array
Does calling clone() on an array also clone its contents?

Wednesday, September 25, 2019

JAVA AES Encryption Decryption with IV, Salt and any length of Key

Simple Note

See the method TestEncryptDecryptWithKeyIvSalt in

and getCipher(int mode, char[] key, byte[] ivBytes, byte[] saltBytes) in


Java 256-bit AES Password-Based Encryption

Java AES and using my own Key

Java AES Key Generation, Encryption and Decryption

Simple Note

Test File:

Utils Implementation

If you get Illegal key size error, probably need to download JCE Jars, please refer to

InvalidKeyException Illegal key size


Java Security: Illegal key size or default parameters?

Other References

The article teach me use char[] over String
Why is char[] preferred over String for passwords?

This one teach me basic Encryption and Decryption
Initial bytes incorrect after Java AES/CBC decryption

How to convert between char[] and byte[]
Converting char[] to byte[]

Monday, September 23, 2019

Use ExecutorService to Run Multiple Callable and Get Results In Order


ExecutorService can run multiple Runnable/Callable, but there is no method for get their Results / Exception in order
(Similar to Promise.all in JavaScript)

So try to build a simple solution.

Simple Note

Test / Sample
Implementation to Run Multiple Callable
Wrapper Class to wrap Callable

Thursday, September 19, 2019