blob: 6a2e33db3092711b133aae7e72e2f35b365f3112 (
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.component;
import com.yahoo.component.AbstractComponent;
import com.yahoo.concurrent.ThreadFactoryFactory;
import com.yahoo.container.di.ComponentDeconstructor;
import com.yahoo.container.di.componentgraph.Provider;
import com.yahoo.jdisc.SharedResource;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
/**
* @author tonyv
* @author gv
*/
public class Deconstructor implements ComponentDeconstructor {
private static final Logger log = Logger.getLogger(Deconstructor.class.getName());
private final ScheduledExecutorService executor =
Executors.newScheduledThreadPool(1, ThreadFactoryFactory.getDaemonThreadFactory("deconstructor"));
private final int delay;
public Deconstructor(boolean delayDeconstruction) {
delay = delayDeconstruction ? 60 : 0;
}
@Override
public void deconstruct(Object component) {
if (component instanceof AbstractComponent) {
AbstractComponent abstractComponent = (AbstractComponent) component;
if (abstractComponent.isDeconstructable()) {
log.info("Scheduling deconstruction of " + abstractComponent);
executor.schedule(new DestructComponentTask(abstractComponent), delay, TimeUnit.SECONDS);
}
} else if (component instanceof Provider) {
((Provider)component).deconstruct();
} else if (component instanceof SharedResource) {
// No need to delay release, as jdisc does ref-counting
((SharedResource)component).release();
}
}
private static class DestructComponentTask implements Runnable {
private final AbstractComponent component;
DestructComponentTask(AbstractComponent component) {
this.component = component;
}
public void run() {
log.info("Starting deconstruction of " + component);
component.deconstruct();
log.info("Finished deconstructing " + component);
}
}
}
|