Skip to content

Commit

Permalink
Merge 30e3ba9 into 0078ecd
Browse files Browse the repository at this point in the history
  • Loading branch information
lfoppiano committed Jun 9, 2021
2 parents 0078ecd + 30e3ba9 commit d46499f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
Expand Up @@ -11,12 +11,12 @@
import org.slf4j.LoggerFactory;

public class GrobidPoolingFactory extends AbstractEngineFactory implements
PoolableObjectFactory {
PoolableObjectFactory<Engine> {

/**
* A pool which contains objects of type Engine for the conversion.
*/
private static volatile GenericObjectPool grobidEnginePool = null;
private static volatile GenericObjectPool<Engine> grobidEnginePool = null;
private static volatile Boolean grobidEnginePoolControl = false;
private static final Logger LOGGER = LoggerFactory
.getLogger(GrobidPoolingFactory.class);
Expand All @@ -37,13 +37,13 @@ protected GrobidPoolingFactory() {
*
* @return GenericObjectPool
*/
protected static GenericObjectPool newPoolInstance() {
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 = new GenericObjectPool<>(GrobidPoolingFactory.newInstance());
//grobidEnginePool.setFactory(GrobidPoolingFactory.newInstance());
grobidEnginePool
.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
Expand Down Expand Up @@ -72,7 +72,7 @@ public static synchronized Engine getEngineFromPool(boolean preloadModels) {
}
Engine engine = null;
try {
engine = (Engine) grobidEnginePool.borrowObject();
engine = grobidEnginePool.borrowObject();
} catch (NoSuchElementException nseExp) {
throw new NoSuchElementException();
} catch (Exception exp) {
Expand All @@ -92,7 +92,7 @@ public static void returnEngine(Engine engine) {
try {
//engine.close();
if (grobidEnginePool == null)
System.out.println("grobidEnginePool is null !");
LOGGER.error("grobidEnginePool is null !");
grobidEnginePool.returnObject(engine);
} catch (Exception exp) {
throw new GrobidException(
Expand All @@ -109,41 +109,28 @@ public static void returnEngine(Engine engine) {
protected static GrobidPoolingFactory newInstance() {
return new GrobidPoolingFactory();
}

/**
* {@inheritDoc}
*/

@Override
public void activateObject(Object arg0) throws Exception {
public void activateObject(Engine arg0) throws Exception {
}

/**
* {@inheritDoc}
*/
@Override
public void destroyObject(Object arg0) throws Exception {
public void destroyObject(Engine engine) throws Exception {
engine.close();
}

/**
* {@inheritDoc}
*/

@Override
public Object makeObject() throws Exception {
public Engine makeObject() throws Exception {
return (createEngine(this.preload));
}

/**
* {@inheritDoc}
*/

@Override
public void passivateObject(Object arg0) throws Exception {
public void passivateObject(Engine arg0) throws Exception {
}

/**
* {@inheritDoc}
*/

@Override
public boolean validateObject(Object arg0) {
public boolean validateObject(Engine arg0) {
return false;
}

Expand Down
Expand Up @@ -114,7 +114,7 @@ public Boolean call() {
}
}

return new Boolean(success);
return success;
}
}

Expand Down Expand Up @@ -183,8 +183,17 @@ public boolean accept(File dir, String name) {
long start = System.currentTimeMillis();
int fails = 0;

ExecutorService executor = Executors.newFixedThreadPool(GrobidProperties.getInstance().getNBThreads());
List<Future<Boolean>> results = new ArrayList<Future<Boolean>>();
int maxNumThreads = GrobidProperties.getInstance().getNBThreads();
int maxPoolConnections = GrobidProperties.getInstance().getMaxPoolConnections();

if (maxNumThreads >= maxPoolConnections) {
int newNumThreads = maxPoolConnections - 1;
System.out.println("Grobid nbThreads ("+maxNumThreads+") >= maxPoolConnections ("+maxPoolConnections+"). Lowering to " + newNumThreads);
maxNumThreads = maxPoolConnections - 1;
}

ExecutorService executor = Executors.newFixedThreadPool(maxNumThreads);
List<Future<Boolean>> results = new ArrayList<>();

if (refFiles.length > 0) {
// this will preload the models, so that the model loading messages don't mess with the progress bar
Expand Down Expand Up @@ -225,12 +234,10 @@ public boolean accept(File dir, String name) {
if (!success)
fails++;
pb.step();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}

executor.shutdown();
Expand Down

0 comments on commit d46499f

Please sign in to comment.