Skip to content

Instantly share code, notes, and snippets.

@Xorlev
Created September 2, 2013 21:17
Show Gist options
  • Save Xorlev/6417352 to your computer and use it in GitHub Desktop.
Save Xorlev/6417352 to your computer and use it in GitHub Desktop.
Storm embedded webserver
public class MetricsWeb extends BaseTaskHook {
private static final Logger log = LoggerFactory.getLogger(MetricsWeb.class);
private static Server server = null;
private static CuratorFramework zk = null;
private static ServiceDiscovery<Void> dsc = null;
@Override
public void prepare(Map conf, TopologyContext context) {
if ("production".equals(conf.get("environment"))) {
initWebConsole(9090);
}
}
@Override
public void cleanup() {
destroyWebConsole();
}
private synchronized int initWebConsole(int port) {
if (server == null) {
log.info("Attempting to start server on {}...", port);
server = new Server(port);
Context context = new Context(server, "/");
context.addServlet(HystrixMetricsStreamServlet.class, "/hystrix.stream");
context.addServlet(DebugServlet.class, "/debug");
try {
server.start();
log.info("Started server on {}...", port);
return port;
} catch (BindException e) {
server = null;
return initWebConsole(port+1);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
return -1;
}
private synchronized void destroyWebConsole() {
if (server == null) return;
try {
server.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
stormConfig.put(Config.TOPOLOGY_AUTO_TASK_HOOKS, Lists.newArrayList(MetricsWeb.class.getName()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment