Skip to content

Commit

Permalink
Added clean shutdown hooks to jetty.
Browse files Browse the repository at this point in the history
  • Loading branch information
simbo1905 committed Jun 16, 2012
1 parent 50919fb commit 5e008ae
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ zktodo2test.dat.properties
/.settings
/.openshift
/zktodo2test.dat.script
/nohup.out
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: java $JAVA_OPTS -jar target/dependency/jetty-runner.jar --port $PORT target/*.war
web: java $JAVA_OPTS -jar target/jetty-runner-jmx.jar --port $PORT target/*.war

38 changes: 37 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<junit.version>4.7</junit.version>
<concurrent.version>1.3.4</concurrent.version>
<commons-lang.version>2.5</commons-lang.version>
<jetty.version>7.4.5.v20110725</jetty.version>
<jetty.version>7.6.4.v20120524</jetty.version>
<postgresql.version>8.4-702.jdbc3</postgresql.version>
<commons-logging.version>1.0.4</commons-logging.version>
<dom4j.version>1.6.1</dom4j.version>
Expand Down Expand Up @@ -133,6 +133,10 @@
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down Expand Up @@ -199,6 +203,10 @@
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<jettyConfig>src/etc/jetty-jmx.xml</jettyConfig>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -218,11 +226,39 @@
<version>${jetty.version}</version>
<destFileName>jetty-runner.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jmx</artifactId>
<version>${jetty.version}</version>
<destFileName>jetty-jmx.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
<tasks>
<jar destfile="${project.build.directory}/jetty-runner-jmx.jar">
<zipfileset src="${project.build.directory}/dependency/jetty-jmx.jar" />
<zipfileset src="${project.build.directory}/dependency/jetty-runner.jar" />
<manifest>
<attribute name="Main-Class" value="org.mortbay.jetty.runner.Runner" />
</manifest>
</jar>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
102 changes: 102 additions & 0 deletions src/etc/jetty-jmx.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<!-- ============================================================================ -->
<!-- To correctly start Jetty with JMX module enabled, this configuration -->
<!-- file must appear first in the list of the configuration files. -->
<!-- The simplest way to achieve this is to add etc/jetty-jmx.xml as the -->
<!-- first file in configuration file list at the end of start.ini file. -->
<!-- ============================================================================ -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

<!-- =========================================================== -->
<!-- Set the java.rmi.server.hostname property in case you've -->
<!-- got a misconfigured /etc/hosts entry or the like. -->
<!-- =========================================================== -->
<!--
<Call class="java.lang.System" name="setProperty">
<Arg>java.rmi.server.hostname</Arg>
<Arg>127.0.0.1</Arg>
</Call>
-->

<!-- =========================================================== -->
<!-- Initialize an mbean server -->
<!-- =========================================================== -->
<Call id="MBeanServer" class="java.lang.management.ManagementFactory"
name="getPlatformMBeanServer" />

<!-- =========================================================== -->
<!-- Initialize the Jetty MBean container -->
<!-- =========================================================== -->
<New id="MBeanContainer" class="org.eclipse.jetty.jmx.MBeanContainer">
<Arg><Ref id="MBeanServer" /></Arg>
<Call name="start"/>
</New>

<!-- Add to the Server to listen for object events -->
<Get id="Container" name="container">
<Call name="addEventListener">
<Arg><Ref id="MBeanContainer" /></Arg>
</Call>
</Get>

<!-- Add to the Server as a managed lifecycle -->
<Call name="addBean">
<Arg><Ref id="MBeanContainer"/></Arg>
<Arg type="boolean">true</Arg>
</Call>

<!-- Add the static log -->
<Ref id="MBeanContainer">
<Call name="addBean">
<Arg>
<New class="org.eclipse.jetty.util.log.Log"/>
</Arg>
</Call>
</Ref>

<!-- In order to connect to the JMX server remotely from a different
process, possibly running on a different host, Jetty JMX module
can create a remote JMX connector. It requires RMI registry to
be started prior to creating the connector server because the
JMX specification uses RMI to facilitate connections.
-->

<!-- Optionally start the RMI registry. Normally RMI registry runs on
port 1099. The argument below can be changed in order to comply
with the firewall requirements.
-->

<Call name="createRegistry" class="java.rmi.registry.LocateRegistry">
<Arg type="java.lang.Integer"><SystemProperty name="jetty.jmxrmiport" default="1099"/></Arg>
<Call name="sleep" class="java.lang.Thread">
<Arg type="java.lang.Integer">1000</Arg>
</Call>
</Call>


<!-- Optionally add a remote JMX connector. The parameters of the constructor
below specify the JMX service URL, and the object name string for the
connector server bean. The parameters of the JMXServiceURL constructor
specify the protocol that clients will use to connect to the remote JMX
connector (RMI), the hostname of the server (local hostname), port number
(automatically assigned), and the URL path. Note that URL path contains
the RMI registry hostname and port number, that may need to be modified
in order to comply with the firewall requirements.
-->

<New id="ConnectorServer" class="org.eclipse.jetty.jmx.ConnectorServer">
<Arg>
<New class="javax.management.remote.JMXServiceURL">
<Arg type="java.lang.String">rmi</Arg>
<Arg type="java.lang.String" />
<Arg type="java.lang.Integer"><SystemProperty name="jetty.jmxrmiport" default="1099"/></Arg>
<Arg type="java.lang.String">/jndi/rmi://<SystemProperty name="jetty.jmxrmihost" default="localhost"/>:<SystemProperty name="jetty.jmxrmiport" default="1099"/>/jmxrmi</Arg>
</New>
</Arg>
<Arg>org.eclipse.jetty.jmx:name=rmiconnectorserver</Arg>
<Call name="start" />
</New>
</Configure>

40 changes: 0 additions & 40 deletions src/main/java/forum/ControllerVM.java

This file was deleted.

90 changes: 90 additions & 0 deletions src/main/java/org/zkforge/zktodo2/jetty/StopJettyJmx.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.zkforge.zktodo2.jetty;

import static java.lang.System.out;

import java.io.IOException;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

/**
* A class to do a clean shutdown of a jetty server which has jmx management
* interfaces enabled.
*
* @author simbo
*/
public class StopJettyJmx {

enum Paramaeters {
NAME_JETTY_JMX("org.eclipse.jetty.jmx:name=rmiconnectorserver")
, NAME_JETTY_SERVER("org.eclipse.jetty.server:type=server,id=0")
, NAME_JETTY_MAVEN("org.mortbay.jetty.plugin:type=jettyserver,id=0");
final String val;
Paramaeters(String val){
this.val = val;
}
}
enum Commands {
COMMAND_STOP("stop");
final String val;
Commands(String val){
this.val = val;
}
}

public static void main(String[] args) {
if (args.length != 2) {
usage();
System.exit(-1);
}

final String type = args[0];
out.println("type: " + type);
final String jmxUrl = args[1];
out.println("jmxUrl: " + jmxUrl);

try {
JMXConnector jmxc = JMXConnectorFactory.connect(new JMXServiceURL(
jmxUrl), null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

if (type.equalsIgnoreCase(Paramaeters.NAME_JETTY_SERVER.name())) {
invoke(mbsc, Paramaeters.NAME_JETTY_SERVER, Commands.COMMAND_STOP);
invoke(mbsc, Paramaeters.NAME_JETTY_JMX, Commands.COMMAND_STOP);
} else if( type.equalsIgnoreCase(Paramaeters.NAME_JETTY_MAVEN.name())){
invoke(mbsc, Paramaeters.NAME_JETTY_MAVEN, Commands.COMMAND_STOP);
} else {
throw new IllegalArgumentException(String.format("I don't understand '%s' please use %s or %s", type, Paramaeters.NAME_JETTY_SERVER.name(), Paramaeters.NAME_JETTY_MAVEN.name()));
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}

protected static void invoke(MBeanServerConnection mbsc, Paramaeters object,
Commands command) throws MalformedObjectNameException,
InstanceNotFoundException, MBeanException, ReflectionException,
IOException {
ObjectName jettyServer = new ObjectName(object.val);
mbsc.invoke(jettyServer, command.val, null, null);
}

private static void usage() {
out.println("usage:");
out.println("\tjava " + StopJettyJmx.class.getCanonicalName()
+ " [NAME_JETTY_SERVER|NAME_JETTY_MAVEN] JMX_URL");
out.println("where JMX_URL is shown on the jetty startup log");
out.println("e.g. java "
+ StopJettyJmx.class.getCanonicalName()
+ " NAME_JETTY_SERVER service:jmx:rmi://Simon-Masseys-Mac-mini-2.local:1099/jndi/rmi://localhost:1099/jmxrmi");
out.println("N.B If you want to be able to make a remote connnection you need to start the server with -Dcom.sun.management.jmxremote.* options.");
}
}
12 changes: 6 additions & 6 deletions src/main/java/org/zkforge/zktodo2/ui/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public void init(@ContextParam(ContextType.VIEW) Component view){
Selectors.wireComponents(view, this, false);
reload();
}

protected void reload() {
List<Reminder> reminders = this.reminderService.findAll();
this.model.getReminders().clear();
this.model.getReminders().addAll(reminders);
}

/**
* @param e The event is unused but is here to indicated that the have access to the screen context
Expand All @@ -81,12 +87,6 @@ public void create(@ContextParam(ContextType.TRIGGER_EVENT) Event e) {
return;
}

protected void reload() {
List<Reminder> reminders = this.reminderService.findAll();
this.model.getReminders().clear();
this.model.getReminders().addAll(reminders);
}

/**
* @param e The event is unused but is here to indicated that the have access to the screen context
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/zkforge/zktodo2/ui/Presenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class Presenter extends SelectorComposer<Window> implements

private static final long serialVersionUID = -3486059156312322420L;

// service
// auto-wired property
@WireVariable ReminderService reminderService;

// components
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<listener-class>
org.zkoss.zk.ui.http.HttpSessionListener
</listener-class>
</listener>
</listener>
<servlet>
<servlet-name>zkLoader</servlet-name>
<servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
Expand Down

0 comments on commit 5e008ae

Please sign in to comment.