Skip to content

Instantly share code, notes, and snippets.

@sfloess
Last active February 24, 2022 18:17
Show Gist options
  • Save sfloess/5c524322e04ac41ea0961c382ac895e2 to your computer and use it in GitHub Desktop.
Save sfloess/5c524322e04ac41ea0961c382ac895e2 to your computer and use it in GitHub Desktop.
JBoss Tips and Tricks

JBoss

Helpful tips and tricks for JBoss.

Miscellaneous

  • Set JAVA_OPTS: /etc/default/jboss-eap.conf

Properties

  • Modified Accented Characters on REST calls: if your REST calls containing accented characters are being modified, add the following to your standalone-full.xml:
<system-properties>
     <property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/>
     <property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="false"/>
</system-properties>

standalone-full.xml:

    <system-properties>
        <property name="jboss.as.management.blocking.timeout" value="600"/>
    </system-properties>

List All Rest End Points

Stackoverflow provided a nice explanation on how to list all running REST end points in JBoss.

Simply add the following to your web.xml:

<context-param>
    <param-name>resteasy.resources</param-name>
    <param-value>org.jboss.resteasy.plugins.stats.RegistryStatsResource</param-value>
</context-param

Once JBoss is running, the URL follows this format: http://[hostname]:[port]/[context]/[api-path]/resteasy/registry

Generate a Unique Id for Logging

To generate a unique id to use in logging (for example in your server.log), do the following:

Write a Class that Extends org.apache.catalina.valves.ValveBase

package org.my.personal.stuff;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;

import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;

import org.jboss.logging.Logger;
import org.jboss.logging.MDC;

public class MyJBossLogger extends ValveBase {
    public static final String UID = "uid";
    
    static String computeUid() {
        return UUID.randomUUID().toString();
    }
    
    static ThreadLocal<String> setLoggingUid(final ThreadLocal<String> threadLocal, final String uid) {
        threadLocal.set(uid);
        
        return threadLocal;
    }
    
    static ThreadLocal<String> setupLogging(final String uid) {
        MDC.put(UID, AndreasLoggingContext.setTransactionId(uid));

        return setLoggingUid(new ThreadLocal<>(), uid);
    }
    
    static String cleanupUid(final ThreadLocal<String> theadLocal) {
        final String retVal = threadLocal.get();
        threadLocal.remove();
        
        return retVal;
    }
    
    static void cleaupLogging(final ThreadLocal<String> theadLocal) {
        MDC.remove(cleanupUid(threadLocal));
    }
    
    void invoke(final ThreadLocal<String> theadLocal, final Request request, final Response response) throws IOException, ServletException {
        try {
            getNext().invoke(request, response);
        } finally {
            cleaupLogging(theadLocal);
        }
    }
    
    @Override
    public void invoke(final Request request, final Response response) throws IOException, ServletException {
        invoke(setupLogging(computeUid()), request, response);
    }
}

Add a Reference to the Class in Your jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <context-root>[your sub URL]</context-root>
    <valve>
        <class-name>org.my.personal.stuff.MyJBossLogger</class-name>
    </valve> 	
</jboss-web>

Add the UID to Your standalone*.xml

...
<profile>
    <subsystem xmlns="urn:jboss:domain:logging:1.3">
        ...
        <console-handler name="CONSOLE">
            ...
            <formatter>
                 <pattern-formatter pattern="...&quote;%X{uid}&quot;..."/>
                 ...
            </formatter>
            ...
        </console>
        ...
    </subsystem>
    ...
</profile>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment