Manual web-app restart
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Servlets and Filters
Portlets
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Servlet
JMX Consoles
Instrumenting
Tutorials
Cookbook

Restart JMX
Cookbook
Cookbook
CMP

Start and stop web-apps when called by an admin servlet.

  1. resin.conf
  2. Admin web.xml
  3. Restart Servlet

resin.conf

Starts any .war files in the webapps directory automatically, but only restarts on a JMX call.

resin.conf
...
<host id="">
  <web-app-deploy path="webapps">
    <startup-mode>automatic<startup-mode>
    <redeploy-mode>manual<redeploy-mode>

    ...
  </web-app-deploy>

Admin web.xml

The /admin web-app configures the restart servlet and protects itself with basic authentication, using the XmlAuthenticator.

See the digest document for information on generating the password.

web.xml
<web-app xmlns="http://caucho.com/ns/resin">
  <servlet servlet-name="restart"
           servlet-class="example.RestartServlet">
  </servlet>

  <servlet-mapping url-pattern="/restart"  servlet-name="restart"/>

  <!-- protect with basic auth. -->
  <authenticator>
    <type>com.caucho.server.security.XmlAuthenticator</type>
    <init user="aladdin:Xmasdf80N3m:user"/>
  </authenticator>

  <security-constraint url-pattern="/*" role-name="user"/>

  <login-config auth-method='basic'/>
</web-app>

Restart Servlet

example/RestartServlet.java
package example;

import java.io.PrintWriter;
import java.io.IOException;

import java.util.logging.Logger;
import java.util.logging.Level;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ObjectInstance;
import javax.management.MBeanServerInvocationHandler;

import javax.naming.InitialContext;
import javax.naming.Context;

import javax.servlet.ServletException;

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

import com.caucho.server.webapp.mbean.WebAppMBean;

public class RestartServlet extends HttpServlet {
  private static final Logger log
    = Logger.getLogger(RestartServlet.class.getName());

  private MBeanServer _mbeanServer;

  public void setMBeanServer(MBeanServer mbeanServer)
  {
    _mbeanServer = mbeanServer;
  }

  public void init()
    throws ServletException
  {
    try {
      if (_mbeanServer == null) {
        Context ic = new InitialContext();

	String jndiName = "java:comp/env/jmx/GlobalMBeanServer";

        _mbeanServer = (MBeanServer) ic.lookup(jndiName);
      }
    } catch (Exception e) {
      throw new ServletException(e);
    }
  }

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
    throws IOException, ServletException
  {
    PrintWriter out = response.getWriter();

    try {
      String webAppName = request.getParameter("webapp");

      String action = request.getParameter("action");

      WebAppMBean webApp = findWebApp(webAppName);

      if (webApp == null) {
        out.println("Can't find web-app " + webAppName);
        return;
      }

     if ("start".equals(action)) {
        webApp.start();
        out.println("started " + webAppName);
      }
      else if ("stop".equals(action)) {
        webApp.stop();
        out.println("stopped " + webAppName);
      }
      else if ("update".equals(action)) {
        webApp.update();
        out.println("updated " + webAppName);
      }
      else
        out.println(action + " is an unknown action.");
    } catch (Exception e) {
      throw new ServletException(e);
    }
  }

  private WebAppMBean findWebApp(String webAppName)
  {
    try {
      String name = ("resin:" +
                     "Server=default," +
                     "Host=default," +
                     "type=WebApp," +
                     "name=" + webAppName);

      ObjectName objectName = new ObjectName(name);

      ObjectInstance object = _mbeanServer.getObjectInstance(objectName);

      if (object == null)
	return null;

      WebAppMBean webApp;

      webApp = (WebAppMBean)
	MBeanServerInvocationHandler.newProxyInstance(_mbeanServer,
                                                      objectName,
                                                      WebAppMBean.class,
                                                      false);

      return webApp;
    } catch (Throwable e) {
      log.log(Level.WARNING, e.toString(), e);

      return null;
    }
  }
}


Cookbook
Cookbook
CMP
Copyright © 1998-2005 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.