Skip to content

Instantly share code, notes, and snippets.

@oc
Created January 14, 2011 12:09
Show Gist options
  • Save oc/779532 to your computer and use it in GitHub Desktop.
Save oc/779532 to your computer and use it in GitHub Desktop.
Plugin-definition for maven-shade-plugin
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>bekkopen.jetty.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>bekkopen.jetty.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
public class BigIPNodeHandler extends AbstractHandler {
// The location the BigIP load balancer will check.
// GET /node.txt /HTTP 1.0
// Assuming the BigIP is configured to
// * report the node down if it doesn't get a response from the path.
// * report the node as turning off gracefully if it sees 'offline' (stop new sessions)
// * report the node as healthy if it sees 'online' (accept new sessions)
public static final String NODE_HANDLER_PATH = "/node.txt";
// Respond with 226 if offline. See RFC 3229#10.4.1 226 IM Used.
// Our BigIP load-balancer requires a 2xx service code, and this is the most applicable to let the server know something is changed
private static final int IM_USED = 226;
private final String secretToken;
private boolean available = true;
public BigIPNodeHandler(String secretToken) {
this.secretToken = secretToken;
}
public void handle(String target, Request serverRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (!NODE_HANDLER_PATH.equals(target)) return;
if("POST".equals(request.getMethod()) && secretToken.equals(request.getParameter("secret"))) {
available = !("offline".equals(request.getParameter("state")));
}
response.setContentType("text/plain");
response.setStatus(available ? SC_OK : IM_USED);
PrintWriter out = response.getWriter();
out.println(available ? "online" : "offline");
out.close();
HttpConnection.getCurrentConnection().getRequest().setHandled(true);
}
public static String check(int port) {
return GET("http://localhost:" + port + NODE_HANDLER_PATH).body;
}
public static String online(int port, String secretToken) {
return POST("http://localhost:" + port + NODE_HANDLER_PATH + "?secret=" + secretToken + "&state=online").body;
}
public static String offline(int port, String secretToken) {
return POST("http://localhost:" + port + NODE_HANDLER_PATH + "?secret=" + secretToken + "&state=offline").body;
}
}
private void start() {
// Start a Jetty server with some sensible(?) defaults
try {
Server srv = new Server();
srv.setStopAtShutdown(true);
// Allow 5 seconds to complete.
// Adjust this to fit with your own webapp needs.
// Remove this if you wish to shut down immediately (i.e. kill <pid> or Ctrl+C).
srv.setGracefulShutdown(5000);
// Increase thread pool
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setMaxThreads(100);
srv.setThreadPool(threadPool);
// Ensure using the non-blocking connector (NIO)
Connector connector = new SelectChannelConnector();
connector.setPort(port);
connector.setMaxIdleTime(30000);
srv.setConnectors(new Connector[]{connector});
// Get the war-file
ProtectionDomain protectionDomain = Main.class.getProtectionDomain();
String warFile = protectionDomain.getCodeSource().getLocation().toExternalForm();
String currentDir = new File(protectionDomain.getCodeSource().getLocation().getPath()).getParent();
// Handle signout/signin in BigIP-cluster
// Add the warFile (this jar)
WebAppContext context = new WebAppContext(warFile, contextPath);
context.setServer(srv);
resetTempDirectory(context, currentDir);
// Add the handlers
HandlerList handlers = new HandlerList();
handlers.addHandler(context);
handlers.addHandler(new ShutdownHandler(srv, context, secret));
handlers.addHandler(new BigIPNodeHandler(secret));
srv.setHandler(handlers);
srv.start();
srv.join();
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment