DaemonExecutor.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.exec;

  18. import java.nio.file.Path;
  19. import java.util.concurrent.ThreadFactory;

  20. /**
  21.  * Runs daemon processes asynchronously. Callers are expected to register a {@link ProcessDestroyer} before executing any processes.
  22.  *
  23.  * @since 1.3
  24.  */
  25. public class DaemonExecutor extends DefaultExecutor {

  26.     /**
  27.      * Constructs a new builder.
  28.      *
  29.      * @since 1.4.0
  30.      */
  31.     public static class Builder extends DefaultExecutor.Builder<Builder> {

  32.         /**
  33.          * Constructs a new instance.
  34.          */
  35.         public Builder() {
  36.             // empty
  37.         }

  38.         /**
  39.          * Creates a new configured DaemonExecutor.
  40.          *
  41.          * @return a new configured DaemonExecutor.
  42.          */
  43.         @Override
  44.         public DefaultExecutor get() {
  45.             return new DaemonExecutor(getThreadFactory(), getExecuteStreamHandler(), getWorkingDirectoryPath());
  46.         }

  47.     }

  48.     /**
  49.      * Creates a new builder.
  50.      *
  51.      * @return a new builder.
  52.      * @since 1.4.0
  53.      */
  54.     public static Builder builder() {
  55.         return new Builder();
  56.     }

  57.     /**
  58.      * Constructs a new instance.
  59.      *
  60.      * @deprecated Use {@link Builder#get()}.
  61.      */
  62.     @Deprecated
  63.     public DaemonExecutor() {
  64.         // super
  65.     }

  66.     private DaemonExecutor(final ThreadFactory threadFactory, final ExecuteStreamHandler executeStreamHandler, final Path workingDirectory) {
  67.         super(threadFactory, executeStreamHandler, workingDirectory);
  68.     }

  69.     /**
  70.      * Factory method to create a thread waiting for the result of an asynchronous execution.
  71.      *
  72.      * @param runnable the runnable passed to the thread.
  73.      * @param name     the name of the thread.
  74.      * @return the thread.
  75.      */
  76.     @Override
  77.     protected Thread createThread(final Runnable runnable, final String name) {
  78.         final Thread thread = super.createThread(runnable, name);
  79.         thread.setDaemon(true);
  80.         return thread;
  81.     }
  82. }