Search in sources :

Example 1 with DaemonThreadFactory

use of org.apache.qpid.server.util.DaemonThreadFactory in project qpid-broker-j by apache.

the class HttpManagement method createServer.

private Server createServer(Collection<HttpPort<?>> ports) {
    LOGGER.debug("Starting up web server on {}", ports);
    _jettyServerExecutor = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory("Jetty-Server-Thread"));
    Server server = new Server(new ExecutorThreadPool(_jettyServerExecutor));
    int lastPort = -1;
    for (HttpPort<?> port : ports) {
        ServerConnector connector = createConnector(port, server);
        connector.addBean(new ConnectionTrackingListener());
        server.addConnector(connector);
        _portConnectorMap.put(port, connector);
        lastPort = port.getPort();
    }
    ServletContextHandler root = new ServletContextHandler(ServletContextHandler.SESSIONS);
    root.setContextPath("/");
    root.setCompactPath(true);
    server.setHandler(root);
    final ErrorHandler errorHandler = new ErrorHandler() {

        @Override
        protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException {
            String uri = request.getRequestURI();
            writeErrorPageMessage(request, writer, code, message, uri);
            for (int i = 0; i < 20; i++) writer.write("<br/>                                                \n");
        }
    };
    root.setErrorHandler(errorHandler);
    // set servlet context attributes for broker and configuration
    root.getServletContext().setAttribute(HttpManagementUtil.ATTR_BROKER, getBroker());
    root.getServletContext().setAttribute(HttpManagementUtil.ATTR_MANAGEMENT_CONFIGURATION, this);
    root.addFilter(new FilterHolder(new ExceptionHandlingFilter()), "/*", EnumSet.allOf(DispatcherType.class));
    FilterHolder corsFilter = new FilterHolder(new CrossOriginFilter());
    corsFilter.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, getCorsAllowOrigins());
    corsFilter.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, Joiner.on(",").join(getCorsAllowMethods()));
    corsFilter.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, getCorsAllowHeaders());
    corsFilter.setInitParameter(CrossOriginFilter.ALLOW_CREDENTIALS_PARAM, String.valueOf(getCorsAllowCredentials()));
    root.addFilter(corsFilter, "/*", EnumSet.of(DispatcherType.REQUEST));
    root.addFilter(new FilterHolder(new MethodFilter()), "/*", EnumSet.of(DispatcherType.REQUEST));
    addFiltersAndServletsForRest(root);
    if (!Boolean.TRUE.equals(getContextValue(Boolean.class, DISABLE_UI_CONTEXT_NAME))) {
        addFiltersAndServletsForUserInterfaces(root);
    }
    root.getSessionHandler().getSessionCookieConfig().setName(JSESSIONID_COOKIE_PREFIX + lastPort);
    root.getSessionHandler().getSessionCookieConfig().setHttpOnly(true);
    root.getSessionHandler().setMaxInactiveInterval(getSessionTimeout());
    return server;
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Server(org.eclipse.jetty.server.Server) MethodFilter(org.apache.qpid.server.management.plugin.filter.MethodFilter) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) DaemonThreadFactory(org.apache.qpid.server.util.DaemonThreadFactory) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ExecutorThreadPool(org.eclipse.jetty.util.thread.ExecutorThreadPool) ExceptionHandlingFilter(org.apache.qpid.server.management.plugin.filter.ExceptionHandlingFilter) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DispatcherType(javax.servlet.DispatcherType) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 2 with DaemonThreadFactory

use of org.apache.qpid.server.util.DaemonThreadFactory in project qpid-broker-j by apache.

the class ReplicatedEnvironmentFacade method connectToHelperNodeAndCheckPermittedHosts.

public static Collection<String> connectToHelperNodeAndCheckPermittedHosts(final String nodeName, final String hostPort, final String groupName, final String helperNodeName, final String helperHostPort, final int dbPingSocketTimeout) {
    ExecutorService executor = null;
    Future<Collection<String>> future = null;
    try {
        executor = Executors.newSingleThreadExecutor(new DaemonThreadFactory(String.format("PermittedHostsCheck-%s-%s", groupName, nodeName)));
        future = executor.submit(new Callable<Collection<String>>() {

            @Override
            public Collection<String> call() throws Exception {
                return getPermittedHostsFromHelper(nodeName, groupName, helperNodeName, helperHostPort, dbPingSocketTimeout);
            }
        });
        try {
            final long timeout = (long) (dbPingSocketTimeout * 1.25);
            final Collection<String> permittedNodes = dbPingSocketTimeout <= 0 ? future.get() : future.get(timeout, TimeUnit.MILLISECONDS);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(String.format("Node '%s' permits nodes: '%s'", helperNodeName, String.valueOf(permittedNodes)));
            }
            if (permittedNodes == null || !permittedNodes.contains(hostPort)) {
                throw new IllegalConfigurationException(String.format("Node using address '%s' is not permitted to join the group '%s'", hostPort, groupName));
            }
            return permittedNodes;
        } catch (ExecutionException e) {
            final Throwable cause = e.getCause();
            if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;
            } else {
                throw new RuntimeException(cause);
            }
        } catch (TimeoutException e) {
            future.cancel(true);
            throw new ExternalServiceTimeoutException(String.format("Task timed out trying to connect to existing node '%s' at '%s'", nodeName, hostPort));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new ExternalServiceException(String.format("Task failed to connect to existing node '%s' at '%s'", nodeName, hostPort));
        }
    } finally {
        executor.shutdown();
    }
}
Also used : ExternalServiceTimeoutException(org.apache.qpid.server.util.ExternalServiceTimeoutException) DaemonThreadFactory(org.apache.qpid.server.util.DaemonThreadFactory) ExternalServiceException(org.apache.qpid.server.util.ExternalServiceException) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) Callable(java.util.concurrent.Callable) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Collection(java.util.Collection) ExecutionException(java.util.concurrent.ExecutionException) SocketTimeoutException(java.net.SocketTimeoutException) TimeoutException(java.util.concurrent.TimeoutException) ExternalServiceTimeoutException(org.apache.qpid.server.util.ExternalServiceTimeoutException)

Aggregations

DaemonThreadFactory (org.apache.qpid.server.util.DaemonThreadFactory)2 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Collection (java.util.Collection)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 TimeoutException (java.util.concurrent.TimeoutException)1 DispatcherType (javax.servlet.DispatcherType)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)1 ExceptionHandlingFilter (org.apache.qpid.server.management.plugin.filter.ExceptionHandlingFilter)1 MethodFilter (org.apache.qpid.server.management.plugin.filter.MethodFilter)1 ConnectionScopedRuntimeException (org.apache.qpid.server.util.ConnectionScopedRuntimeException)1 ExternalServiceException (org.apache.qpid.server.util.ExternalServiceException)1 ExternalServiceTimeoutException (org.apache.qpid.server.util.ExternalServiceTimeoutException)1 ServerScopedRuntimeException (org.apache.qpid.server.util.ServerScopedRuntimeException)1