| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.impl; |
| 18 |
|
|
| 19 |
|
import org.apache.camel.Service; |
| 20 |
|
import org.apache.camel.util.ServiceHelper; |
| 21 |
|
|
| 22 |
|
import java.util.ArrayList; |
| 23 |
|
import java.util.Collection; |
| 24 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
| 25 |
|
|
| 26 |
|
import org.springframework.jmx.export.annotation.ManagedAttribute; |
| 27 |
|
import org.springframework.jmx.export.annotation.ManagedOperation; |
| 28 |
|
import org.springframework.jmx.export.annotation.ManagedResource; |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
3129 |
public abstract class ServiceSupport implements Service { |
| 37 |
|
private static int threadCounter; |
| 38 |
3129 |
private AtomicBoolean started = new AtomicBoolean(false); |
| 39 |
3129 |
private AtomicBoolean stopping = new AtomicBoolean(false); |
| 40 |
3129 |
private AtomicBoolean stopped = new AtomicBoolean(false); |
| 41 |
|
private Collection childServices; |
| 42 |
|
|
| 43 |
|
public void start() throws Exception { |
| 44 |
2052 |
if (started.compareAndSet(false, true)) { |
| 45 |
1815 |
if (childServices != null) { |
| 46 |
18 |
ServiceHelper.startServices(childServices); |
| 47 |
|
} |
| 48 |
1815 |
doStart(); |
| 49 |
|
} |
| 50 |
2052 |
} |
| 51 |
|
|
| 52 |
|
public void stop() throws Exception { |
| 53 |
2517 |
if (started.get() && stopping.compareAndSet(false, true)) { |
| 54 |
|
try { |
| 55 |
1593 |
doStop(); |
| 56 |
|
} |
| 57 |
|
finally { |
| 58 |
1593 |
if (childServices != null) { |
| 59 |
18 |
ServiceHelper.stopServices(childServices); |
| 60 |
|
} |
| 61 |
1593 |
stopped.set(true); |
| 62 |
1593 |
started.set(false); |
| 63 |
1593 |
stopping.set(false); |
| 64 |
1593 |
} |
| 65 |
|
} |
| 66 |
2517 |
} |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
public boolean isStarted() { |
| 72 |
654 |
return started.get(); |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
public boolean isStopping() { |
| 79 |
120 |
return stopping.get(); |
| 80 |
|
} |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
public boolean isStopped() { |
| 86 |
18 |
return stopped.get(); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
protected abstract void doStart() throws Exception; |
| 90 |
|
|
| 91 |
|
protected abstract void doStop() throws Exception; |
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
protected String getThreadName(String prefix) { |
| 97 |
24 |
return prefix + " thread:" + nextThreadCounter(); |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
protected static synchronized int nextThreadCounter() { |
| 101 |
24 |
return ++threadCounter; |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
protected void addChildService(Object childService) { |
| 105 |
24 |
if (childServices == null) { |
| 106 |
18 |
childServices = new ArrayList(); |
| 107 |
|
} |
| 108 |
24 |
childServices.add(childService); |
| 109 |
24 |
} |
| 110 |
|
|
| 111 |
|
protected boolean removeChildService(Object childService) { |
| 112 |
0 |
if (childServices != null) { |
| 113 |
0 |
return childServices.remove(childService); |
| 114 |
|
} |
| 115 |
|
else { |
| 116 |
0 |
return false; |
| 117 |
|
} |
| 118 |
|
} |
| 119 |
|
} |