1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.commons.exec;
21
22 /**
23 * Runs daemon processes asynchronously. Callers are expected to register a {@link ProcessDestroyer} before executing any processes.
24 *
25 * @since 1.3
26 */
27 public class DaemonExecutor extends DefaultExecutor {
28
29 /**
30 * Constructs a new builder.
31 *
32 * @since 1.4.0
33 */
34 public static class Builder extends DefaultExecutor.Builder<Builder> {
35
36 /**
37 * Constructs a new instance.
38 */
39 public Builder() {
40 // empty
41 }
42
43 /**
44 * Creates a new configured DaemonExecutor.
45 *
46 * @return a new configured DaemonExecutor.
47 */
48 @Override
49 public DefaultExecutor get() {
50 return new DaemonExecutor(this);
51 }
52
53 }
54
55 /**
56 * Creates a new builder.
57 *
58 * @return a new builder.
59 * @since 1.4.0
60 */
61 public static Builder builder() {
62 return new Builder();
63 }
64
65 /**
66 * Constructs a new instance.
67 *
68 * @deprecated Use {@link Builder#get()}.
69 */
70 @Deprecated
71 public DaemonExecutor() {
72 // super
73 }
74
75 private DaemonExecutor(final Builder builder) {
76 super(builder);
77 }
78
79 /**
80 * Factory method to create a thread waiting for the result of an asynchronous execution.
81 *
82 * @param runnable the runnable passed to the thread.
83 * @param name the name of the thread.
84 * @return the thread.
85 */
86 @Override
87 protected Thread createThread(final Runnable runnable, final String name) {
88 final Thread thread = super.createThread(runnable, name);
89 thread.setDaemon(true);
90 return thread;
91 }
92 }