2018-03-15 15:40:50 +01:00
|
|
|
package org.gcube.nlphub.nlp;
|
|
|
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import org.gcube.nlphub.legacy.Constants;
|
|
|
|
import org.gcube.nlphub.legacy.JsonManager;
|
|
|
|
|
|
|
|
public class RunnerCommander extends Thread {
|
|
|
|
private HttpServletResponse response = null;
|
|
|
|
private String annotationList = "";
|
|
|
|
private String[] identifiers = null;
|
|
|
|
private String link = "";
|
|
|
|
private String token = "";
|
2018-03-20 14:45:49 +01:00
|
|
|
private ArrayList<String> outResultList = null;
|
2018-03-15 15:40:50 +01:00
|
|
|
private long sleepTime = 500l;
|
|
|
|
private long maxWaitingTime = 5l * 60l * 1000l;
|
|
|
|
private Logger logger = Logger.getLogger(RunnerCommander.class.getSimpleName());
|
|
|
|
private boolean complete = false;
|
|
|
|
|
|
|
|
public RunnerCommander(String[] identifiers, String link, String annotationList, String token) {
|
|
|
|
this.identifiers = identifiers;
|
|
|
|
this.annotationList = annotationList;
|
|
|
|
this.link = link;
|
|
|
|
this.token = token;
|
|
|
|
this.response = null;
|
|
|
|
this.sleepTime = 500l;
|
|
|
|
outResultList = new ArrayList<String>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public RunnerCommander(String[] identifiers, String link, String annotationList, String token,
|
|
|
|
HttpServletResponse response) {
|
|
|
|
this.identifiers = identifiers;
|
|
|
|
this.annotationList = annotationList;
|
|
|
|
this.link = link;
|
|
|
|
this.token = token;
|
|
|
|
this.response = response;
|
|
|
|
this.sleepTime = 500l;
|
|
|
|
outResultList = new ArrayList<String>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getSleepTime() {
|
|
|
|
return sleepTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSleepTime(long sleepTime) {
|
|
|
|
this.sleepTime = sleepTime;
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:45:49 +01:00
|
|
|
public synchronized void updateResultList(String res) {
|
2018-03-15 15:40:50 +01:00
|
|
|
outResultList.add(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getMaxWaitingtime() {
|
|
|
|
return maxWaitingTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMaxWaitingTime(long maxWaitingTime) {
|
|
|
|
this.maxWaitingTime = maxWaitingTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void startProcess() {
|
|
|
|
start();
|
2018-03-21 15:44:38 +01:00
|
|
|
// the following loop is needed in order to wait the end of all computations
|
|
|
|
// before return...
|
|
|
|
long counter = 0;
|
2018-03-15 15:40:50 +01:00
|
|
|
while(!complete) {
|
|
|
|
try {
|
|
|
|
sleep(sleepTime);
|
2018-03-21 15:44:38 +01:00
|
|
|
counter += sleepTime;
|
|
|
|
if(counter > 2*maxWaitingTime)
|
|
|
|
complete = true;
|
2018-03-15 15:40:50 +01:00
|
|
|
} catch (InterruptedException x) {
|
|
|
|
logger.info("Interrupted.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void runAlgorithms() {
|
|
|
|
for (String id : identifiers) {
|
2018-03-20 14:45:49 +01:00
|
|
|
NlpAsyncNerRunner n = new NlpAsyncNerRunner(id, token, link, annotationList, null, this);
|
2018-03-15 15:40:50 +01:00
|
|
|
n.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
runAlgorithms();
|
|
|
|
long counter = 0;
|
|
|
|
try {
|
|
|
|
while (counter <= maxWaitingTime) {
|
2018-03-20 17:03:42 +01:00
|
|
|
//logger.info("complete: " + outResultList.size() + "/" + identifiers.length);
|
2018-03-15 15:40:50 +01:00
|
|
|
if (outResultList.size() == identifiers.length) {
|
|
|
|
String[] links = new String[outResultList.size()];
|
|
|
|
links = outResultList.toArray(links);
|
|
|
|
writeResponse(links);
|
|
|
|
logger.info("Elapsed time: " + counter + " msec.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
counter += sleepTime;
|
|
|
|
sleep(sleepTime);
|
|
|
|
}
|
|
|
|
logger.error("Timeout error.");
|
|
|
|
timeoutHandler();
|
|
|
|
} catch (InterruptedException x) {
|
|
|
|
logger.info("Elapsed time: " + counter + " msec.");
|
|
|
|
logger.info("Thread interrupted.");
|
|
|
|
timeoutHandler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void timeoutHandler() {
|
|
|
|
boolean found = false;
|
|
|
|
for(int i=0; i<identifiers.length; i++, found=false) {
|
|
|
|
String id = identifiers[i];
|
|
|
|
id = id.substring(id.lastIndexOf(".") + 1);
|
|
|
|
for(String s : outResultList) {
|
|
|
|
if(s.startsWith(id)) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!found) {
|
|
|
|
outResultList.add(id + ":::" + Constants.ERROR_ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String[] links = new String[outResultList.size()];
|
|
|
|
links = outResultList.toArray(links);
|
|
|
|
writeResponse(links);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void writeResponse(String[] content) {
|
|
|
|
logger.info("RunnerCommander has run " + content.length + " tasks.");
|
|
|
|
if (response != null) {
|
|
|
|
response.setContentType("application/json;charset=UTF-8");
|
|
|
|
PrintWriter writer = null;
|
|
|
|
try {
|
|
|
|
writer = response.getWriter();
|
|
|
|
String msg = new JsonManager().getSuccessJsonResponse(content);
|
|
|
|
writer.println(msg);
|
|
|
|
if (writer.checkError()) {
|
|
|
|
logger.error("writeResponse method has some problem in writing result.");
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
logger.error(ex.getLocalizedMessage());
|
|
|
|
ex.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
complete = true;
|
|
|
|
if (writer != null)
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
public static void main(String[] args) {
|
|
|
|
String id1 = "org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.ENGLISH_NAMED_ENTITY_RECOGNIZER";
|
|
|
|
String id2 = "org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.ENGLISH_NER_CORENLP";
|
|
|
|
String tokken = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
|
|
|
|
String ann = "Organization,Location,Person";
|
|
|
|
String file = "http://data.d4science.org/TWhNTS9DdVdXaTZLSWsrWUNQdHk3OUdZSU93SXRFbjhHbWJQNStIS0N6Yz0";
|
|
|
|
file = "http://data.d4science.org/L0txb3o3Tk9GaW1LSWsrWUNQdHk3MG1ZWFdtWkJENU5HbWJQNStIS0N6Yz0";
|
|
|
|
String[] ids = new String[2];
|
|
|
|
ids[0] = id1;
|
|
|
|
ids[1] = id2;
|
|
|
|
|
|
|
|
RunnerCommander rc = new RunnerCommander(ids, file, ann, tokken);
|
|
|
|
rc.start();
|
|
|
|
} */
|
|
|
|
}
|