Greg,

On 12/3/25 3:34 AM, Greg Huber wrote:
A while back this thread was posted here and it works great on my local dev box (using a background thread) using vhost="localhost/".  When I deploy it as a war the vhost name changes.

 From the output:

Attributes for object: Catalina:j2eeType=Servlet,WebModule=//localhost/ ##914-develop,name=MyServlet,J2EEApplication=none,J2EEServer=none:

How do I reliably get the vhost / WebModule name when using a war?

In ganeral, the WebModule name should be //[host]/[appname][##version] but you have a special case here with the ROOT context which makes the web application "name" into an empty-string.

The attribute "org.apache.catalina.webappVersion" gives "914-develop" but its missing the ##

My war is called: ROOT##914-develop.war

Right. You'll need to see if you have a non-empty "version" and if so, append not only the version string itself but a ## prefix.

Hope that helps,
-chris

On 16/10/2025 18:02, Christopher Schultz wrote:
I recently added reload capabilities to my application for users with admin roles. The code is basically (after triple-checking authentication and some app-specific bells and whistles):

        ServletContext app = getServletContext();

        MBeanServer mbs = getServer();
        if(null != mbs) {
            try {
                String vhost = getHostname();

                ObjectName objectName = new ObjectName("Catalina:j2eeType=WebModule,name=//" + vhost + app.getContextPath() + ",J2EEApplication=none,J2EEServer=none");

                if(mbs.isRegistered(objectName)) {
                    mbs.invoke(objectName, "reload", null, null);
                }
            } catch (MBeanException | MalformedObjectNameException | InstanceNotFoundException | ReflectionException e) {                 app.log(getClass().getSimpleName() + ": Failed to reload application ", e);
            }
        }

... with the helper functions:


    private String hostname = "localhost"; // This is an init-param

    public String getHostname() {
        return hostname;
    }

    private static MBeanServer getServer() {
        ArrayList<MBeanServer> mbservers = MBeanServerFactory.findMBeanServer(null);

        if (mbservers.size() > 0) {
            MBeanServer mbserver = mbservers.get(0);

            if (mbserver != null)
                return mbserver;
        }

        return null;
    }

Just wire this into your application somewhere. You don't need to install anything else, even the Manager application.

-chris


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to