From WebSphere to JBoss

All

Intro

From a variety of reasons, we plan to move from WebSphere to Jboss, but just following the guide here that we will be able to do it.  There will be things that will need to be adapted – keep in my the following:

Migrating Websphere to JBoss

As rule, we have to follow the basic steps

1) port over the server side resources.

2) create JBoss deployment descriptors.

3) install the application

4) resolve anything that does not conform to or violates the JEE spec.

REFs

[1] https://docs.jboss.org/author/display/AS72/How+do+I+migrate+my+application+from+WebSphere+to+AS+7

 

Java Performance good reference

All

Practical Java performance tuning

Learning from Benjamin Evans and James Gough, on java was really good, the video is not open I guess, but can be accessed on O’Reilly platform.

They highlight the fact that performance is never alone, you always learning together. Starting with cache misses, and other basic issues they build upon that.

REFs

[1]https://www.oreilly.com/learning/practical-java-performance-tuning

 

 

GC – GC and GC’s

All

Intro

Today I’ll describe two tools for GC: GCViwer and JMeter.

In Eclipse you just need to do:

-Xloggc:/whatever/Downloads/LOG.log -verbose:gc

> in the server configuration as shown here Garbagecat

GCViewer

This is a tool for quite look up on GC files. Just download the source and run mvn clean install and it’s all done.

Here below is one of my experiments with a gc.log file in the GC viewer. Just used [viewer] [log.file] and it will pop up:

water_analysis.png

JMeter

JVM tuning is not a magic trick. So JMeter is a way to stop the gambling and magic and do some metrics based on load simulations, so then we can tune the JVM options for the optional.

Unified JVM logger

Well, the JEPS 158, Java 9, introduces a unified logger mechanism.

REFs

The source or GCViwer.

The JMeter can be found here

K-framework

All

Intro

“Formal semantics can avoid stupid errors, for example, programs that give different results for different for different compilers version.” – Prof Grigore Rosu.

K-framework

K-framework, from the Runtime Verification team, and by the group of Prof Grigore Rosu at University Urbana Campaign.

A Meta-language

K-framework is a language to describe other languages.

Smart-contract utilization

His main idea is to use the k-framework to verify smart contracts.

REFs

Youtube interview with Prof Grigore Rosu

 

Undertow

All

Intro

Undertow is a lightweight server that is a subsystem of JBoss.

Undertow

The source code is here: https://github.com/undertow-io/ and it’s quite interesting to see java code again, after a long time with python/c++.

Documentation

Undertow documentation is pretty neat and can be found here. It states that the core part of Undertow is the lightweight async handlers.

Tutorial

There are many tutorials on the internet but the ones I recommended are: this and this, especially Baeldung tutorial.

Deploy simple servlet

package com.baeldung.undertow;

import io.undertow.Undertow;
import io.undertow.Handlers;
import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;

import java.io.IOException;

import io.undertow.server.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.undertow.util.Headers;

public class SimpleServer {

static void simpleHandler(HttpServerExchange exchange)
{
exchange.getResponseHeaders().add(Headers.CONTENT_TYPE, “text/plain”);
exchange.getResponseSender().send(“Adding Handler!”);
}

public static void main(String[] args) throws Exception
{
int port = 8080;
/*
* “localhost” will ONLY listen on local host.
* If you want the server reachable from the outside you need to set “0.0.0.0”
*/
String host = “localhost”;

/*
* This web server has a single handler with no routing.
* ALL urls are handled by the helloWorldHandler.
*/
//Undertow server = Undertow.builder()
// Add the helloWorldHandler as a method reference.
// .addHttpListener(port, host, SimpleServer::simpleHandler)
// .build();

//Deploy Get
DeploymentInfo servletBuilder = Servlets.deployment().setClassLoader(SimpleServer.class.getClassLoader())
.setDeploymentName(“myapp”).setContextPath(“/myapp”)
.addServlets(Servlets.servlet(“myservlet”,
new HttpServlet() {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write(“Hello World!”);
}
}.getClass()).addMapping(“/myservlet”));

DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
manager.deploy();
PathHandler path = Handlers.path(Handlers.redirect(“/myapp”)).addPrefixPath(“/myapp”, manager.start());

Undertow server = Undertow.builder().addHttpListener(port, host).setHandler(path).build();

//logger.debug(“starting on http://” + host + “:” + port);
server.start();
}

/* Version of setBufferSize – TomCat*/
/*@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setBufferSize(bufferSize);

resp.setContentType(“text/plain”);
resp.setCharacterEncoding(“UTF-8”);
resp.setContentLength(responseSize);

PrintWriter pw = resp.getWriter();
for (int i = 0; i < responseSize; i++) {
pw.append(‘X’);
}
}*/

}

REFs

 

Byteman experiments

All

Intro

Byteman helps to insert code and change stuff on the run – after compiling, therefore – Java Code. This is quite interesting.

Byteman

Byteman can be downloaded from here and have some good tutorials here

To execute:

Well, we just run java with the agent:

java -javaagent:/path/byteman.jar=script:myrule.btm [-jar] [application] [arguments]

           WARNING THE -JAR AND THE APPLICATION ARE THE LAST ARGUMENTS!

Rule – basic

RULE Make it me

CLASS Hello

METHOD main

AT ENTRY

IF TRUE

DO args[0] = “Andrew”;

ENDRULE

      This rule is gonna replace the first argument in args by Andrew, who is the guy that made the presentation.

Rule – adv by patching in a real method

  1. RULE trace ClientSocketFactory
  2. CLASS org.jboss.security.ssl.ClientSocketFactory
  3. METHOD <init>
  4. IF true
  5. DO traceStack(“Invoking constructor of ClientSocketFactory\n”, 20)
  6. ENDRULE

This rule basically creates a stack trace of  20 lines when the invocation of the init method of ClientSocketFactory.

Other apps

Other apps can be used combined with Byteman so then we can see more stuff, Thermostat is a good one. I’ll add more on this later.

REFs

[1] Byteman.jboss.org

[2] Andrew Dinn youtube vide on DevNation 2016

[3] Programmer’s Guide is quite interesting and clarifying especially for traceStack.

jmap (rip jhat)

All

Intro

While developing Java applications is possible to see how the system behave by inspecting the ThreadDump, the HeapDump of the system or even the PermGen in JVisualVM – quite nice tool, all live actually.  The thing is to capture only what we need, so to capture the Heap Dump there are many ways and one of them is to use the jmap tool – which actually comes together with Jstack

HeapDump

Is a picture of the Java memory, a snapshot of it [1], it basically will contain the follow information [3]:

i. All Objects

ii. All Classes

ii. Threads info

ii. GC Root

Jmap

This tool can help solve this issue by helping to capture the data and it’s actually shipped with JDK tool:

              jmap [options] pid

Using the option Jmap -heap we can print the heap info summary of GC:

jmap -heap   [pid]

Usage

Usage:
jmap -clstats <pid>
to connect to running process and print class loader statistics
jmap -finalizerinfo <pid>
to connect to running process and print information on objects awaiting finalization
jmap -histo[:live] <pid>
to connect to running process and print histogram of java object heap
if the “live” suboption is specified, only count live objects
jmap -dump:<dump-options> <pid>
to connect to running process and dump java heap
jmap -? -h –help
to print this help message

dump-options:
live dump only live objects; if not specified,
all objects in the heap are dumped.
format=b binary format
file=<file> dump heap to <file>

Example: jmap -dump:live,format=b,file=heap.bin <pid>

Analyzing

To analyze the HeapDump we can use the Jhat tool, Java Heap Analysis Tool provided by default with the JDK, cause if you open the heap dump straight away, it will be shenanigans – since it is a binary file -actually an HPROF binary file.

2e32 0000 0000 0800 0001 682e 7487 5701
0000 0000 0000 0015 0000 7f0c a007 4be8
4d41 585f 534d 414c 4c5f 5445 4e01 0000
0000 0000 0019 0000 7f0c a416 7250 636f
6d70 7574 654d 6574 686f 6448 6173 6801
0000 0000 0000 0018 0000 7f0c a411 3940
636c 6173 7343 6173 7448 616e 646c 6572
0100 0000 0000 0000 0900 007f 0c48 1420
7801 0100 0000 0000 0000 0d00 007f 0ca4

 

Jhat

Well, if you’re using JDK 8 or below, you can use this tool, otherwise,you must use JVisualVM, like is shown below:

heap_dump

JVisualVM – Heap Dump

 

REFs

[0] jmap

[1] This article, by the GCeasy Team, is quite neat and I almost stole the idea of fish something to put as a background image – like the whale. Their course description is really good here.

[2] Jstack video from Informatica Support youtube

[3] IBM Heap Dump

Creating a servlet in Jboss

All

Intro

After playing with Undertow,  we can actually create a servlet and deploy in JBoss, which is quite simple, especially if you follow the Eclipse steps.

1. New Project – Dynamic Web Project

2. Create the Web server in Java Resources/src/package you want.

3. Change the .jsp so then your servlet can be used.

4. Change the Web.xml so then we track where is the servlet address.

Servlet

servlet.jsp

<?xml version=”1.0″ encoding=”ISO-8859-1″ ?>
<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
pageEncoding=”ISO-8859-1″%>
<html xmlns=”http://w…content-available-to-author-only…3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″ />
<title>Hello World</title>
</head>
<body>

<%

String hello = “Hello Mike”;

%>

I came here to say: <% out.println(hello); %>

<form>
First name:
<input type=”text” name=”firstname” />
Last name:
<input type=”text” name=”lastname” />
</form>

<form action=”HelloServlet”>
<input type=”submit” value=”Send” />
</form>

</body>
</html>

Web.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.5//EN” “http://java.sun.com/dtd/web-app_2_5.dtd”&gt;
<web-app xmlns=”http://java.sun.com/xml/ns/javaee&#8221; version=”2.5″>
<display-name>WebApp-01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>servlet.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>

The servlet.java

public class HelloServlet extends HttpServlet {

static int port = 8080;
static int bufferSize = 337680;
static int responseSize = 100;
static String host = “localhost”;

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StringBuffer requestURL = req.getRequestURL();

resp.setBufferSize(bufferSize);

resp.setContentType(“text/plain”);
resp.setCharacterEncoding(“UTF-8”);
resp.setContentLength(responseSize);

PrintWriter pw = resp.getWriter();
pw.write(requestURL.toString());
pw.write(Integer.toString(bufferSize));

for (int i = 0; i < responseSize; i++) {
pw.append(‘X’);
}

pw.close();
}

Deploy

We can deploy with Jboss in Deploy or we can Deploy straight on Eclipse, if we configure the server in Eclipse, for the lazy like me. So then just

Tutorial

The best tutorial I found about is this one

REFs