/
GrobidPoolingFactory.java
executable file
·136 lines (119 loc) · 3.83 KB
/
GrobidPoolingFactory.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package org.grobid.core.factory;
import java.util.NoSuchElementException;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.grobid.core.engines.Engine;
import org.grobid.core.exceptions.GrobidException;
import org.grobid.core.utilities.GrobidProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GrobidPoolingFactory extends AbstractEngineFactory implements
PoolableObjectFactory<Engine> {
/**
* A pool which contains objects of type Engine for the conversion.
*/
private static volatile GenericObjectPool<Engine> grobidEnginePool = null;
private static volatile Boolean grobidEnginePoolControl = false;
private static final Logger LOGGER = LoggerFactory
.getLogger(GrobidPoolingFactory.class);
private static volatile Boolean preload = false;
/**
* Constructor.
*/
protected GrobidPoolingFactory() {
//fullInit();
init();
}
/**
* Creates a pool for {@link Engine} objects. So a number of objects is
* always available and ready to start immediatly.
*
* @return GenericObjectPool
*/
protected static GenericObjectPool<Engine> newPoolInstance() {
if (grobidEnginePool == null) {
// initialize grobidEnginePool
LOGGER.debug("synchronized newPoolInstance");
synchronized (grobidEnginePoolControl) {
if (grobidEnginePool == null) {
grobidEnginePool = new GenericObjectPool<>(GrobidPoolingFactory.newInstance());
//grobidEnginePool.setFactory(GrobidPoolingFactory.newInstance());
grobidEnginePool
.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
grobidEnginePool.setMaxWait(GrobidProperties.getPoolMaxWait());
grobidEnginePool.setMaxActive(GrobidProperties.getMaxPoolConnections());
grobidEnginePool.setTestWhileIdle(false);
grobidEnginePool.setLifo(false);
grobidEnginePool.setTimeBetweenEvictionRunsMillis(2000);
grobidEnginePool.setMaxIdle(0);
}
}
}
return grobidEnginePool;
}
/**
* Obtains an instance from this pool.<br>
*
* By contract, clients must call {@link GrobidPoolingFactory#returnEngine}
* when they finish to use the engine.
*/
public static synchronized Engine getEngineFromPool(boolean preloadModels) {
preload = preloadModels;
if (grobidEnginePool == null) {
grobidEnginePool = newPoolInstance();
}
Engine engine = null;
try {
engine = grobidEnginePool.borrowObject();
} catch (NoSuchElementException nseExp) {
throw new NoSuchElementException();
} catch (Exception exp) {
throw new GrobidException("An error occurred while getting an engine from the engine pool", exp);
}
LOGGER.info("Number of Engines in pool active/max: "
+ grobidEnginePool.getNumActive() + "/"
+ grobidEnginePool.getMaxActive());
return engine;
}
/**
* By contract, engine must have been obtained using
* {@link GrobidPoolingFactory#getEngineFromPool}.<br>
*/
public static void returnEngine(Engine engine) {
try {
//engine.close();
if (grobidEnginePool == null)
LOGGER.error("grobidEnginePool is null !");
grobidEnginePool.returnObject(engine);
} catch (Exception exp) {
throw new GrobidException(
"An error occurred while returning an engine from the engine pool", exp);
}
}
/**
* Creates and returns an instance of GROBIDFactory. The init() method will
* be called.
*
* @return
*/
protected static GrobidPoolingFactory newInstance() {
return new GrobidPoolingFactory();
}
@Override
public void activateObject(Engine arg0) throws Exception {
}
@Override
public void destroyObject(Engine engine) throws Exception {
}
@Override
public Engine makeObject() throws Exception {
return (createEngine(this.preload));
}
@Override
public void passivateObject(Engine arg0) throws Exception {
}
@Override
public boolean validateObject(Engine arg0) {
return false;
}
}