• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

snowplow / snowplow-android-tracker / #1051

pending completion
#1051

push

github-actions

matus-tomlein
Prepare for 4.1.2 release

3486 of 4579 relevant lines covered (76.13%)

0.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.73
/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/internal/emitter/Executor.java
1
/*
2
 * Copyright (c) 2015-2022 Snowplow Analytics Ltd. All rights reserved.
3
 *
4
 * This program is licensed to you under the Apache License Version 2.0,
5
 * and you may not use this file except in compliance with the Apache License Version 2.0.
6
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7
 *
8
 * Unless required by applicable law or agreed to in writing,
9
 * software distributed under the Apache License Version 2.0 is distributed on an
10
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12
 */
13

14
package com.snowplowanalytics.snowplow.internal.emitter;
15

16
import androidx.annotation.NonNull;
17
import androidx.annotation.Nullable;
18
import androidx.annotation.RestrictTo;
19

20
import com.snowplowanalytics.snowplow.internal.tracker.Logger;
21

22
import java.util.concurrent.Callable;
23
import java.util.concurrent.ExecutorService;
24
import java.util.concurrent.Executors;
25
import java.util.concurrent.Future;
26

27
/**
28
 * Static Class which holds the logic for controlling
29
 * the Thread Pool for the Classic Tracker.
30
 */
31
@RestrictTo(RestrictTo.Scope.LIBRARY)
32
public class Executor {
1✔
33

34
    private static ExecutorService executor;
35
    private static int threadCount = 2; // Minimum amount of threads.
1✔
36

37
    /**
38
     * If the executor is null creates a
39
     * new executor.
40
     *
41
     * @return the executor
42
     */
43
    private synchronized static ExecutorService getExecutor() {
44
        if (executor == null) {
1✔
45
            executor = Executors.newScheduledThreadPool(threadCount);
1✔
46
        }
47
        return executor;
1✔
48
    }
49

50
    /**
51
     * Sends a runnable to the executor service.
52
     * Errors are logged but not tracked with the diagnostic feature.
53
     *
54
     * @param tag string indicating the source of the runnable for logging purposes in case of
55
     *            exceptions raised by the runnable
56
     * @param runnable the runnable to be queued
57
     */
58
    public static void execute(@Nullable String tag, @Nullable Runnable runnable) {
59
        execute(false, tag, runnable);
1✔
60
    }
1✔
61

62
    /**
63
     * Sends a runnable to the executor service.
64
     *
65
     * @param reportsOnDiagnostic weather or not the error has to be tracked with diagnostic feature
66
     * @param tag string indicating the source of the runnable for logging purposes in case of
67
     *            exceptions raised by the runnable
68
     * @param runnable the runnable to be queued
69
     */
70
    public static void execute(boolean reportsOnDiagnostic, @Nullable String tag, @Nullable Runnable runnable) {
71
        final String loggerTag;
72
        if (tag == null) {
1✔
73
            loggerTag = "Source not provided";
×
74
        } else {
75
            loggerTag = tag;
1✔
76
        }
77
        execute(runnable, t -> {
1✔
78
            String message = t.getLocalizedMessage();
×
79
            if (message == null) {
×
80
                message = "No message provided.";
×
81
            }
82
            if (reportsOnDiagnostic) {
×
83
                Logger.track(loggerTag, message, t);
×
84
            } else {
85
                Logger.e(loggerTag, message, t);
×
86
            }
87
        });
×
88
    }
1✔
89

90
    /**
91
     * Sends a runnable to the executor service.
92
     *
93
     * @param runnable the runnable to be queued
94
     * @param exceptionHandler the handler of exception raised by the runnable
95
     */
96
    public static void execute(@Nullable Runnable runnable, @Nullable ExceptionHandler exceptionHandler) {
97
        ExecutorService executor = getExecutor();
1✔
98
        try {
99
            executor.execute(() -> {
1✔
100
                try {
101
                    if (runnable != null) {
1✔
102
                        runnable.run();
1✔
103
                    }
104
                } catch (Throwable t) {
1✔
105
                    if (exceptionHandler != null) {
1✔
106
                        exceptionHandler.handle(t);
1✔
107
                    }
108
                }
1✔
109
            });
1✔
110
        } catch (Exception e) {
×
111
            if (exceptionHandler != null) {
×
112
                exceptionHandler.handle(e);
×
113
            }
114
        }
1✔
115
    }
1✔
116

117
    /**
118
     * Sends a callable to the executor service and
119
     * returns a Future.
120
     *
121
     * @param callable the callable to be queued
122
     * @return the future object to be queried
123
     */
124
    @NonNull
125
    public static Future<?> futureCallable(@NonNull Callable<?> callable) {
126
        return getExecutor().submit(callable);
1✔
127
    }
128

129
    /**
130
     * Shuts the executor service down and resets
131
     * the executor to a null state.
132
     */
133
    @Nullable
134
    public static ExecutorService shutdown() {
135
        if (executor != null) {
1✔
136
            executor.shutdown();
1✔
137
            ExecutorService es = executor;
1✔
138
            executor = null;
1✔
139
            return es;
1✔
140
        }
141
        return null;
1✔
142
    }
143

144
    /**
145
     * Changes the amount of threads the
146
     * scheduler will be able to use.
147
     *
148
     * NOTE: This can only be set before the
149
     * scheduler is first accessed, after this
150
     * point the function will not effect anything.
151
     *
152
     * @param count the thread count
153
     */
154
    public static void setThreadCount(final int count) {
155
        if (count >= 2) {
1✔
156
            threadCount = count;
1✔
157
        }
158
    }
1✔
159

160
    public static int getThreadCount() {
161
        return threadCount;
×
162
    }
163

164
    /**
165
     * Handle exceptions raised by a Runnable
166
     */
167
    @FunctionalInterface
168
    public interface ExceptionHandler {
169
        void handle(@Nullable Throwable t);
170
    }
171
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc