Undertow [pt2]

All

Intro

Ok, quick stuff, just reading about Undertow, I didn’t know it was so flexible actually.

Undertow two way to do it:

Way 1

There is basically two way to use undertow, and in fact,  I already made a post about the first way here, the idea is simple: use the Builder API after we create the handler we want as in here. The listener on the example is created for the port 8080, i.e. bind to localhost.

The builder will default values for performance-related stuff, as a number of threads and buffers.

public class HelloWorldServer {

    public static void main(final String[] args) {
        Undertow server = Undertow.builder()                                                    //Undertow builder
                .addHttpListener(8080, "localhost")                                             //Listener binding
                .setHandler(new HttpHandler() {                                                 //Default Handler
                    @Override
                    public void handleRequest(final HttpServerExchange exchange) throws Exception {
                        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");  //Response Headers
                        exchange.getResponseSender().send("Hello World");                       //Response Sender
                    }
                }).build();
        server.start();
    }

}

Way 2

Otherwise, the second way to do stuff is by manually assembling stuff to create a server.

The main steps are described here:

  1. Create an XNIO Worker. This worker manages both the IO and Worker threads for the server.
  2. Create an XNIO SSL instance (optional, only required if HTTPS is in use)
  3. Create an instance of the relevant Undertow listener class
  4. Open a server socket using XNIO and set its accept listener

 

 

REFs

http://undertow.io/

 

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

 

Thanks, Grammarly

All

Someone asked about the fact that sometimes the verbs are out of order in my posts. Even missing verbs. Well, full disclosure here, I’m dyslexic and the more I learn German the worse my English gets. As my Italian degrades my French too. I don’t know why.   Thanks, Grammarly.

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.