croatoan
croatoan

Reputation: 13

Is there a way to restart a webapp from inside the application using the Tomcat API?

Basically I created some servlets on startup during the contextInitialized phase when my webapp starts up. But I need to be able to add servlets dynamically while the webapp is still running.

I'm starting to realize this may not be possible, so my next solution was to attempt to redeploy the webapp or restart tomcat. Is it possible to redeploy the app from inside the application? Or even add a servlet after the context has already been initialized?

At this point just being able to redeploy the .war would be enough.

Upvotes: 1

Views: 1242

Answers (2)

Christopher Schultz
Christopher Schultz

Reputation: 20862

As @JasonArmstrong says, you can trigger a reload using JMX Beans, which are available within the same JVM to any application. You can reload yourself or another application. I'll post code later on to show how to do that.

On the other hand, if all you want to do is deploy an additional servlet, you can do that using standard Java Servlet APIs.

The Java Servlet API ServletContext interface contains a method, addServlet, which allows you to deploy a new servlet. That, in combination with the return value from that method should allow you to deploy a new servlet after the application has been initialized.

For example (in a Servlet)

Servlet servlet = ...; // Make your servlet, here

ServletContext application = getServletContext();

ServletRegistration.Dynamic reg = application.addServlet("MyNewServlet", servlet);

reg.addMapping("/nyNewServlet");

UPDATE 2018-12-07

Apologies for the above text which has now been retracted. Everything about it is true except that you can't deploy new servlets (and, presumably, Filters and other things like that) after the context has been initialized.

== Back to original answer==

Or if you want to reload the context, you can still do it using JMX. The code looks something like this:

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

// Get the JVM's local "server" (there should be only one)
MBeanServer mbs = MBeanServerFactory.findMBeanServer(null).get(0);

// Create an "object name" that points to the application's
// context in Tomcat's MXBean tree. You might want to use
// jvisualvm or a similar MBean explorer to locate your
// application to get the exact right syntax for your context
// in the tree. Tomcat version may affect this value.
ObjectName objectName = new ObjectName("Catalina:j2eeType=WebModule,name=//localhost/contextname,J2EEApplication=none,J2EEServer=none");

// Invoke the "reload" method, which will 
mbs.invoke(objectName, "reload", null, null);

Upvotes: 1

Jason Armstrong
Jason Armstrong

Reputation: 1260

The Manager app will allow you to update the application. You may also be able to do something with JMX MBeans.

The Manager app allows you to deploy and redeploy via update=true parameter. You can also start, stop, restart an app from there.

It’s not quite what you were looking for, but it’s pretty easy to set up.

Upvotes: 0

Related Questions