Created
September 2, 2013 21:17
-
-
Save Xorlev/6417352 to your computer and use it in GitHub Desktop.
Storm embedded webserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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