Search in sources :

Example 1 with InstrumentedHandler

use of com.codahale.metrics.jetty9.InstrumentedHandler in project metrics by dropwizard.

the class ExampleServer method main.

public static void main(String[] args) throws Exception {
    COUNTER_1.inc();
    COUNTER_2.inc();
    final ThreadPool threadPool = new InstrumentedQueuedThreadPool(REGISTRY);
    final Server server = new Server(threadPool);
    final Connector connector = new ServerConnector(server, new InstrumentedConnectionFactory(new HttpConnectionFactory(), REGISTRY.timer("http.connection")));
    server.addConnector(connector);
    final ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/initial");
    context.setAttribute(MetricsServlet.METRICS_REGISTRY, REGISTRY);
    context.setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, new HealthCheckRegistry());
    final ServletHolder holder = new ServletHolder(new AdminServlet());
    context.addServlet(holder, "/dingo/*");
    final InstrumentedHandler handler = new InstrumentedHandler(REGISTRY);
    handler.setHandler(context);
    server.setHandler(handler);
    server.start();
    server.join();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) InstrumentedHandler(com.codahale.metrics.jetty9.InstrumentedHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) InstrumentedQueuedThreadPool(com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool) AdminServlet(com.codahale.metrics.servlets.AdminServlet) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) InstrumentedQueuedThreadPool(com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) InstrumentedConnectionFactory(com.codahale.metrics.jetty9.InstrumentedConnectionFactory)

Example 2 with InstrumentedHandler

use of com.codahale.metrics.jetty9.InstrumentedHandler in project chassis by Kixeye.

the class WebSocketTransportConfiguration method webSocketServer.

@Bean(initMethod = "start", destroyMethod = "stop")
@Order(0)
public Server webSocketServer(@Value("${websocket.enabled:false}") boolean websocketEnabled, @Value("${websocket.hostname:}") String websocketHostname, @Value("${websocket.port:-1}") int websocketPort, @Value("${secureWebsocket.enabled:false}") boolean secureWebsocketEnabled, @Value("${secureWebsocket.hostname:}") String secureWebsocketHostname, @Value("${secureWebsocket.port:-1}") int secureWebsocketPort, @Value("${secureWebsocket.selfSigned:false}") boolean selfSigned, @Value("${secureWebsocket.mutualSsl:false}") boolean mutualSsl, @Value("${secureWebsocket.keyStorePath:}") String keyStorePath, @Value("${secureWebsocket.keyStoreData:}") String keyStoreData, @Value("${secureWebsocket.keyStorePassword:}") String keyStorePassword, @Value("${secureWebsocket.keyManagerPassword:}") String keyManagerPassword, @Value("${secureWebsocket.trustStorePath:}") String trustStorePath, @Value("${secureWebsocket.trustStoreData:}") String trustStoreData, @Value("${secureWebsocket.trustStorePassword:}") String trustStorePassword, @Value("${securewebsocket.excludedCipherSuites:}") String[] excludedCipherSuites) throws Exception {
    // set up servlets
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS | ServletContextHandler.NO_SECURITY);
    context.setErrorHandler(null);
    context.setWelcomeFiles(new String[] { "/" });
    for (final MessageSerDe serDe : serDes) {
        // create the websocket creator
        final WebSocketCreator webSocketCreator = new WebSocketCreator() {

            public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
                // this will have spring construct a new one for every session
                ActionInvokingWebSocket webSocket = forwardingWebSocket();
                webSocket.setSerDe(serDe);
                webSocket.setUpgradeRequest(req);
                webSocket.setUpgradeResponse(resp);
                return webSocket;
            }
        };
        // configure the websocket servlet
        ServletHolder webSocketServlet = new ServletHolder(new WebSocketServlet() {

            private static final long serialVersionUID = -3022799271546369505L;

            @Override
            public void configure(WebSocketServletFactory factory) {
                factory.setCreator(webSocketCreator);
            }
        });
        Map<String, String> webSocketProperties = new HashMap<>();
        AbstractConfiguration config = ConfigurationManager.getConfigInstance();
        Iterator<String> webSocketPropertyKeys = config.getKeys("websocket");
        while (webSocketPropertyKeys.hasNext()) {
            String key = webSocketPropertyKeys.next();
            webSocketProperties.put(key.replaceFirst(Pattern.quote("websocket."), ""), config.getString(key));
        }
        webSocketServlet.setInitParameters(webSocketProperties);
        context.addServlet(webSocketServlet, "/" + serDe.getMessageFormatName() + "/*");
    }
    // create the server
    Server server;
    if (metricRegistry == null || !monitorThreadpool) {
        server = new Server();
        server.setHandler(context);
    } else {
        server = new Server(new InstrumentedQueuedThreadPool(metricRegistry));
        InstrumentedHandler instrumented = new InstrumentedHandler(metricRegistry);
        instrumented.setHandler(context);
        server.setHandler(instrumented);
    }
    // set up connectors
    if (websocketEnabled) {
        InetSocketAddress address = StringUtils.isBlank(websocketHostname) ? new InetSocketAddress(websocketPort) : new InetSocketAddress(websocketHostname, websocketPort);
        JettyConnectorRegistry.registerHttpConnector(server, address);
    }
    if (secureWebsocketEnabled) {
        InetSocketAddress address = StringUtils.isBlank(secureWebsocketHostname) ? new InetSocketAddress(secureWebsocketPort) : new InetSocketAddress(secureWebsocketHostname, secureWebsocketPort);
        JettyConnectorRegistry.registerHttpsConnector(server, address, selfSigned, mutualSsl, keyStorePath, keyStoreData, keyStorePassword, keyManagerPassword, trustStorePath, trustStoreData, trustStorePassword, excludedCipherSuites);
    }
    return server;
}
Also used : InstrumentedHandler(com.kixeye.chassis.transport.http.InstrumentedHandler) WebSocketServletFactory(org.eclipse.jetty.websocket.servlet.WebSocketServletFactory) Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) InetSocketAddress(java.net.InetSocketAddress) MessageSerDe(com.kixeye.chassis.transport.serde.MessageSerDe) WebSocketServlet(org.eclipse.jetty.websocket.servlet.WebSocketServlet) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) InstrumentedQueuedThreadPool(com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) Order(org.springframework.core.annotation.Order) Bean(org.springframework.context.annotation.Bean)

Example 3 with InstrumentedHandler

use of com.codahale.metrics.jetty9.InstrumentedHandler in project dropwizard by dropwizard.

the class AbstractServerFactory method createAppServlet.

protected Handler createAppServlet(Server server, JerseyEnvironment jersey, ObjectMapper objectMapper, Validator validator, MutableServletContextHandler handler, @Nullable Servlet jerseyContainer, MetricRegistry metricRegistry) {
    configureSessionsAndSecurity(handler, server);
    handler.addFilter(AllowedMethodsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)).setInitParameter(AllowedMethodsFilter.ALLOWED_METHODS_PARAM, Joiner.on(',').join(allowedMethods));
    handler.addFilter(ThreadNameFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    serverPush.addFilter(handler);
    if (jerseyContainer != null) {
        jerseyRootPath.ifPresent(jersey::setUrlPattern);
        jersey.register(new JacksonBinder(objectMapper));
        jersey.register(new HibernateValidationFeature(validator));
        if (registerDefaultExceptionMappers == null || registerDefaultExceptionMappers) {
            jersey.register(new ExceptionMapperBinder(detailedJsonProcessingExceptionMapper));
        }
        handler.addServlet(new NonblockingServletHolder(jerseyContainer), jersey.getUrlPattern());
    }
    final InstrumentedHandler instrumented = new InstrumentedHandler(metricRegistry);
    instrumented.setServer(server);
    instrumented.setHandler(handler);
    return instrumented;
}
Also used : NonblockingServletHolder(io.dropwizard.jetty.NonblockingServletHolder) InstrumentedHandler(com.codahale.metrics.jetty9.InstrumentedHandler) HibernateValidationFeature(io.dropwizard.jersey.validation.HibernateValidationFeature) ExceptionMapperBinder(io.dropwizard.setup.ExceptionMapperBinder) JacksonBinder(io.dropwizard.jersey.jackson.JacksonBinder) AllowedMethodsFilter(io.dropwizard.jersey.filter.AllowedMethodsFilter)

Example 4 with InstrumentedHandler

use of com.codahale.metrics.jetty9.InstrumentedHandler in project chassis by Kixeye.

the class HttpTransportConfiguration method httpServer.

@Bean(initMethod = "start", destroyMethod = "stop")
@Order(0)
public Server httpServer(@Value("${http.enabled:false}") boolean httpEnabled, @Value("${http.hostname:}") String httpHostname, @Value("${http.port:-1}") int httpPort, @Value("${https.enabled:false}") boolean httpsEnabled, @Value("${https.hostname:}") String httpsHostname, @Value("${https.port:-1}") int httpsPort, @Value("${https.selfSigned:false}") boolean selfSigned, @Value("${https.mutualSsl:false}") boolean mutualSsl, @Value("${https.keyStorePath:}") String keyStorePath, @Value("${https.keyStoreData:}") String keyStoreData, @Value("${https.keyStorePassword:}") String keyStorePassword, @Value("${https.keyManagerPassword:}") String keyManagerPassword, @Value("${https.trustStorePath:}") String trustStorePath, @Value("${https.trustStoreData:}") String trustStoreData, @Value("${https.trustStorePassword:}") String trustStorePassword, @Value("${https.excludedCipherSuites:}") String[] excludedCipherSuites, ConfigurableWebApplicationContext webApplicationContext) throws Exception {
    // set up servlets
    ServletContextHandler context = servletContextHandler();
    // create a new child application context
    AnnotationConfigWebApplicationContext childApplicationContext = (AnnotationConfigWebApplicationContext) transportWebMvcContext(webApplicationContext, context).getContext();
    // register swagger
    childApplicationContext.getBean(SwaggerRegistry.class).registerSwagger(context, getObjectMappers(webApplicationContext));
    // configure the spring mvc dispatcher
    DispatcherServlet dispatcher = new DispatcherServlet(childApplicationContext);
    // enable gzip
    context.addFilter(GzipFilter.class, "/*", null);
    // map application servlets
    context.addServlet(new ServletHolder(dispatcher), "/");
    if (healthCheckRegistry != null) {
        context.addServlet(new ServletHolder(new HealthServlet(healthCheckRegistry)), "/healthcheck");
    }
    // create the server
    Server server;
    if (metricRegistry == null || !monitorThreadpool) {
        server = new Server();
        server.setHandler(context);
    } else {
        server = new Server(new InstrumentedQueuedThreadPool(metricRegistry));
        InstrumentedHandler instrumented = new InstrumentedHandler(metricRegistry);
        instrumented.setHandler(context);
        server.setHandler(instrumented);
    }
    // set up connectors
    if (httpEnabled) {
        InetSocketAddress address = StringUtils.isBlank(httpHostname) ? new InetSocketAddress(httpPort) : new InetSocketAddress(httpHostname, httpPort);
        JettyConnectorRegistry.registerHttpConnector(server, address);
    }
    if (httpsEnabled) {
        InetSocketAddress address = StringUtils.isBlank(httpsHostname) ? new InetSocketAddress(httpsPort) : new InetSocketAddress(httpsHostname, httpsPort);
        JettyConnectorRegistry.registerHttpsConnector(server, address, selfSigned, mutualSsl, keyStorePath, keyStoreData, keyStorePassword, keyManagerPassword, trustStorePath, trustStoreData, trustStorePassword, excludedCipherSuites);
    }
    return server;
}
Also used : InstrumentedQueuedThreadPool(com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool) Server(org.eclipse.jetty.server.Server) HealthServlet(com.kixeye.chassis.transport.shared.HealthServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) InetSocketAddress(java.net.InetSocketAddress) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) SwaggerRegistry(com.kixeye.chassis.transport.swagger.SwaggerRegistry) Order(org.springframework.core.annotation.Order) Bean(org.springframework.context.annotation.Bean)

Aggregations

InstrumentedQueuedThreadPool (com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool)3 Server (org.eclipse.jetty.server.Server)3 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)3 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)3 InstrumentedHandler (com.codahale.metrics.jetty9.InstrumentedHandler)2 InetSocketAddress (java.net.InetSocketAddress)2 Bean (org.springframework.context.annotation.Bean)2 Order (org.springframework.core.annotation.Order)2 HealthCheckRegistry (com.codahale.metrics.health.HealthCheckRegistry)1 InstrumentedConnectionFactory (com.codahale.metrics.jetty9.InstrumentedConnectionFactory)1 AdminServlet (com.codahale.metrics.servlets.AdminServlet)1 InstrumentedHandler (com.kixeye.chassis.transport.http.InstrumentedHandler)1 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)1 HealthServlet (com.kixeye.chassis.transport.shared.HealthServlet)1 SwaggerRegistry (com.kixeye.chassis.transport.swagger.SwaggerRegistry)1 AllowedMethodsFilter (io.dropwizard.jersey.filter.AllowedMethodsFilter)1 JacksonBinder (io.dropwizard.jersey.jackson.JacksonBinder)1 HibernateValidationFeature (io.dropwizard.jersey.validation.HibernateValidationFeature)1 NonblockingServletHolder (io.dropwizard.jetty.NonblockingServletHolder)1 ExceptionMapperBinder (io.dropwizard.setup.ExceptionMapperBinder)1