Observability of the Java Virtual Machine

Image
The JVM is one of the most observable runtimes. It provides us lots of tools for troubleshooting a JVM application in production. 1. Thread observability Threads are how the JVM actually does work. When something is wrong in production, the symptom is almost always a thread: stopped, blocked, leaking etc. Thread dumps work on any JVM with no  instrumentation, no agents, no restarts. <Example project link with /threaddump endpoint>         // (1) Deadlock — two threads grab the same pair of locks in opposite order.         new Thread(() -> grab(LOCK_A, LOCK_B), "deadlock-A-then-B").start();         new Thread(() -> grab(LOCK_B, LOCK_A), "deadlock-B-then-A").start(); http://localhost:8080/actuator/threaddump To list the JVMS, we can use the command below. PS C:\observe-jvm> jps -lv 25296 jdk.jcmd/sun.tools.jps.Jps -Dapplication.home=C:\Program Files\Microsoft\jdk-21.0.3.9-hotspot -Xms8m -Djdk.module.main=...

Fluent Interface Example in an Enterprise Integration Project

While working on an enterprise integration project about virtual bank payments, I utilized a simple fluent interface approach. The purpose of the code is creating strongly typed objects from a generic SMO (ServiceMessageObject) instance.

Note: In the context of IBM integration technologies, ServiceMessageObject represents the “message”. In our example, it represents a Web Service request.

Old Code:

if (paymentInfo.getDataObject("totalAmount") != null) {
totalAmount = paymentInfo.getDataObject("totalAmount");

DataObject totalAmount2 = totalAmount.getDataObject("totalAmount");
if (totalAmount2.getBigDecimal("amount") != null) {
parameters.setAmount(totalAmount2.getBigDecimal("amount"));
}

String currencyCode = totalAmount2.getString("currencyCode");
parameters.setCurrencyCode(currencyCode);

}
Byte numberOfInstallments = paymentInfo.getByte("numberOfInstallments");
if (numberOfInstallments != null) {
parameters.setNumberOfInstallments(numberOfInstallments);
}

if (transactionInfo.size() != 0) {
if (((DataObject) transactionInfo.get(0)).getString("orderID") != null) {
parameters.setOrderId(((DataObject) transactionInfo.get(0))
.getString("orderID"));
if (((DataObject) transactionInfo.get(0))
.getString("transactionID") != null) {
parameters.setTransactionID(((DataObject) transactionInfo
.get(0)).getString("transactionID"));
}
parameters.setBankRefId(((DataObject) transactionInfo.get(0)).getString("bankReferenceID"));
//...more


It is really painful to read and modify this code. But actually, what it does is really simple. It should look simple.

New Code:

parameters.setCreditCardNumber(body
.getDataObject("paymentInfo")
.getDataObject("creditCardInfo")
.getString("creditCardNumber"));

parameters.setCreditCardExpiryMonth(body
.getDataObject("paymentInfo")
.getDataObject("creditCardInfo")
.getByte("creditCardExpiryMonth"));

parameters.setAmount(body
.getDataObject("paymentInfo")
.getDataObject("totalAmount")
.getDataObject("totalAmount")
.getBigDecimal("amount"));

parameters.setTransactionID(body
.getListItem("transactionInfo")
.getString("transactionID"));

This is a simple example of an internal DSL. The point I want to stress is this: “Use internal DSLs whenever you need better readability, among other things.”
This simple refactoring helped us a lot to read/modify the code easier than before.

To implement the DSL, what I did is hiding some details in a class that I named DataObjectWrapper. This class contains IBM's DataObject:

public class DataObjectWrapper {

private DataObject dataObject;
public DataObjectWrapper getListItem(String string) {

List list = dataObject.getList(string);

if (list.size() != 0)
return new DataObjectWrapper((DataObject)list.get(0));

return new NullDataObject();
}

public DataObjectWrapper getDataObject(String field) {

if (dataObject.getDataObject(field) != null)
return new DataObjectWrapper(dataObject.getDataObject(field));

return new NullDataObject();
}

public String getString(String field) {
return dataObject.getString(field);
}

public Byte getByte(String field) {
return dataObject.getByte(field);
}
}


To support fluent statements, NullDataObject is an important abstraction:

public class NullDataObject extends DataObjectWrapper {
@Override
public DataObjectWrapper getDataObject(String string) {
return new NullDataObject();
}

@Override
public String getString(String field) {
return null;
}
// ...
}

Comments

Popular posts from this blog

The WeakReference class, monitoring memory leak and garbage collection in a Java application

Simplescalar Simulator - Part 2: sim-outorder.c

Notes on Java Performance