private void logTasks() { final StringBuilder stringBuilder = new StringBuilder(1024).append(String.format("%n%n")); for (Task task : tasks.getTasks()) { final String taskClassName = firstNonNull(task.getClass().getCanonicalName(), task.getClass().getName()); stringBuilder.append(String.format(" %-7s /tasks/%s (%s)%n", "POST", task.getName(), taskClassName)); } LOGGER.info("tasks = {}", stringBuilder.toString()); }
/** * Creates a new {@link AdminEnvironment}. * * @param handler a servlet context handler * @param healthChecks a health check registry */ public AdminEnvironment(MutableServletContextHandler handler, HealthCheckRegistry healthChecks, MetricRegistry metricRegistry) { super(handler); this.healthChecks = healthChecks; this.healthChecks.register("deadlocks", new ThreadDeadlockHealthCheck()); this.tasks = new TaskServlet(metricRegistry); tasks.add(new GarbageCollectionTask()); tasks.add(new LogConfigurationTask()); addServlet("tasks", tasks).addMapping("/tasks/*"); handler.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() { @Override public void lifeCycleStarting(LifeCycle event) { logTasks(); logHealthChecks(); } }); }
final String name = chooseName(annotation.name(), annotation.absolute(), task); final String name = chooseName(annotation.name(), annotation.absolute(), task); final String name = chooseName(annotation.name(), annotation.absolute(), task,
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { final Task task = tasks.get(req.getPathInfo()); if (task != null) { resp.setContentType(CONTENT_TYPE); final PrintWriter output = resp.getWriter(); try { final TaskExecutor taskExecutor = taskExecutors.get(task); requireNonNull(taskExecutor, "taskExecutor").executeTask(getParams(req), getBody(req), output); } catch (Exception e) { LOGGER.error("Error running {}", task.getName(), e); resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); output.println(); output.println(e.getMessage()); e.printStackTrace(output); } finally { output.close(); } } else { resp.sendError(HttpServletResponse.SC_NOT_FOUND); } }
/** * Adds the given task to the set of tasks exposed via the admin interface. * * @param task a task */ public void addTask(Task task) { tasks.add(requireNonNull(task)); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { final Task task = tasks.get(req.getPathInfo()); if (task != null) { resp.setContentType(MediaType.PLAIN_TEXT_UTF_8.toString()); final PrintWriter output = resp.getWriter(); try { final TaskExecutor taskExecutor = taskExecutors.get(task); requireNonNull(taskExecutor).executeTask(getParams(req), getBody(req), output); } catch (Exception e) { LOGGER.error("Error running {}", task.getName(), e); resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); output.println(); output.println(e.getMessage()); e.printStackTrace(output); } finally { output.close(); } } else { resp.sendError(HttpServletResponse.SC_NOT_FOUND); } }
/** * Adds the given task to the set of tasks exposed via the admin interface. * * @param task a task */ public void addTask(Task task) { tasks.add(requireNonNull(task)); }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (Strings.isNullOrEmpty(req.getPathInfo())) { try (final PrintWriter output = resp.getWriter()) { resp.setContentType(CONTENT_TYPE); getTasks().stream() .map(Task::getName) .sorted() .forEach(output::println); } } else if (tasks.containsKey(req.getPathInfo())) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); } else { resp.sendError(HttpServletResponse.SC_NOT_FOUND); } }
/** * Creates a new {@link AdminEnvironment}. * * @param handler a servlet context handler * @param healthChecks a health check registry */ public AdminEnvironment(MutableServletContextHandler handler, HealthCheckRegistry healthChecks, MetricRegistry metricRegistry) { super(handler); this.healthChecks = healthChecks; this.healthChecks.register("deadlocks", new ThreadDeadlockHealthCheck()); this.tasks = new TaskServlet(metricRegistry); tasks.add(new GarbageCollectionTask()); tasks.add(new LogConfigurationTask()); addServlet("tasks", tasks).addMapping("/tasks/*"); handler.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() { @Override public void lifeCycleStarting(LifeCycle event) { logTasks(); logHealthChecks(); } }); }
final String name = chooseName(annotation.name(), annotation.absolute(), task); final String name = chooseName(annotation.name(), annotation.absolute(), task); final String name = chooseName(annotation.name(), annotation.absolute(), task,
private void logTasks() { final StringBuilder stringBuilder = new StringBuilder(1024).append(String.format("%n%n")); for (Task task : tasks.getTasks()) { final String taskClassName = firstNonNull(task.getClass().getCanonicalName(), task.getClass().getName()); stringBuilder.append(String.format(" %-7s /tasks/%s (%s)%n", "POST", task.getName(), taskClassName)); } LOGGER.info("tasks = {}", stringBuilder.toString()); }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (Strings.isNullOrEmpty(req.getPathInfo())) { try (final PrintWriter output = resp.getWriter()) { resp.setContentType(MediaType.PLAIN_TEXT_UTF_8.toString()); getTasks().stream() .map(Task::getName) .sorted() .forEach(output::println); } } else if (tasks.containsKey(req.getPathInfo())) { resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); } else { resp.sendError(HttpServletResponse.SC_NOT_FOUND); } }