blob: 3db854a6888e57d00eb4d1f3e58bb9e56190a233 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.logging;
import com.yahoo.concurrent.DaemonThreadFactory;
import java.io.IOException;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
/**
* Abstract class that deals with storing event entries on disk and making sure all stored
* entries are eventually sent
*
* @author hmusum
*/
abstract class AbstractSpoolingLogger extends AbstractThreadedLogger implements Runnable {
protected static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(Spooler.class.getName());
private static final ScheduledExecutorService executorService =
new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory("AbstractSpoolingLogger-send-"));
protected final Spooler spooler;
public AbstractSpoolingLogger() {
this(new Spooler());
}
public AbstractSpoolingLogger(Spooler spooler) {
this.spooler = spooler;
executorService.scheduleWithFixedDelay(this, 0, 10L, TimeUnit.MILLISECONDS);
}
public void run() {
try {
spooler.processFiles(this::transport);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean send(LoggerEntry entry) {
log.log(Level.INFO, "Sending");
try {
executor.execute(() -> spooler.write(entry));
} catch (RejectedExecutionException e) {
return false;
}
return true;
}
// TODO Call from a component or make this class a component
public void shutdown() {
executorService.shutdown();
try {
if ( ! executorService.awaitTermination(10, TimeUnit.SECONDS))
log.log(Level.WARNING, "Timeout elapsed waiting for termination");
} catch (InterruptedException e) {
log.log(Level.WARNING, "Failure when waiting for termination: " + e.getMessage());
}
}
}
|