use of org.jumpmind.symmetric.service.impl.DataLoaderService.DataLoaderWorker in project symmetric-ds by JumpMind.
the class PushUriHandler method handle.
public void handle(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
String nodeId = ServletUtils.getParameter(req, WebConstants.NODE_ID);
String channelId = getChannelId(req);
log.info("About to service push request for {}", nodeId);
IStagingManager stagingManager = engine.getStagingManager();
IDataLoaderService dataLoaderService = engine.getDataLoaderService();
INodeService nodeService = engine.getNodeService();
IStatisticManager statisticManager = engine.getStatisticManager();
String identityNodeId = nodeService.findIdentityNodeId();
ProcessInfo processInfo = statisticManager.newProcessInfo(new ProcessInfoKey(nodeId, identityNodeId, ProcessType.TRANSFER_FROM, channelId));
BufferedReader reader = null;
BufferedWriter writer = null;
DataLoaderWorker worker = null;
try {
Node sourceNode = engine.getNodeService().findNode(nodeId);
processInfo.setStatus(ProcessInfo.Status.TRANSFERRING);
reader = new BufferedReader(new InputStreamReader(createInputStream(req)));
long streamToFileThreshold = parameterService.getLong(ParameterConstants.STREAM_TO_FILE_THRESHOLD);
String line = reader.readLine();
StringBuilder batchPrefix = new StringBuilder();
Long batchId = null;
while (line != null) {
if (line.startsWith(CsvConstants.BATCH)) {
batchId = getBatchId(line);
IStagedResource resource = stagingManager.create(streamToFileThreshold, Constants.STAGING_CATEGORY_INCOMING, nodeId, batchId);
writer = resource.getWriter();
writer.write(batchPrefix.toString());
} else if (line.startsWith(CsvConstants.COMMIT)) {
writer.write(line);
writer.close();
writer = null;
if (worker == null) {
worker = dataLoaderService.createDataLoaderWorker(ProcessType.LOAD_FROM_PUSH, channelId, sourceNode);
}
worker.queueUpLoad(new IncomingBatch(batchId, nodeId));
batchId = null;
}
if (batchId == null) {
batchPrefix.append(line).append("\n");
} else if (writer != null) {
writer.write(line);
writer.write("\n");
}
line = reader.readLine();
}
processInfo.setStatus(ProcessInfo.Status.OK);
} catch (RuntimeException ex) {
processInfo.setStatus(ProcessInfo.Status.ERROR);
throw ex;
} finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(writer);
}
PrintWriter resWriter = res.getWriter();
if (worker != null) {
worker.queueUpLoad(new DataLoaderService.EOM());
while (!worker.isComplete()) {
String status = "done";
IncomingBatch batch = worker.waitForNextBatchToComplete();
if (batch == null) {
status = "in progress";
batch = worker.getCurrentlyLoading();
}
if (batch != null && !(batch instanceof DataLoaderService.EOM)) {
ArrayList<IncomingBatch> list = new ArrayList<IncomingBatch>(1);
list.add(batch);
log.info("sending {} ack ... for {}", status, batch);
// TODO 13 support
resWriter.write(engine.getTransportManager().getAcknowledgementData(false, identityNodeId, list));
resWriter.write("\n");
resWriter.flush();
}
}
}
res.flushBuffer();
log.debug("Done servicing push request for {}", nodeId);
}
Aggregations