Example usage for org.apache.solr.handler RequestHandlerUtils handleRollback

List of usage examples for org.apache.solr.handler RequestHandlerUtils handleRollback

Introduction

In this page you can find the example usage for org.apache.solr.handler RequestHandlerUtils handleRollback.

Prototype

public static boolean handleRollback(SolrQueryRequest req, UpdateRequestProcessor processor, SolrParams params,
        boolean force) throws IOException 

Source Link

Usage

From source file:org.codeexample.jeffery.solr.ThreadedUpdateRequestHandler.java

License:Apache License

private ExecutorService importStreamsMultiThreaded(final SolrQueryRequest req, final SolrQueryResponse rsp,
        List<ContentStream> streams) throws InterruptedException, IOException {
    ExecutorService executor = null;
    SolrParams params = req.getParams();

    final UpdateRequestProcessorChain processorChain = req.getCore()
            .getUpdateProcessingChain(params.get(UpdateParams.UPDATE_CHAIN));

    UpdateRequestProcessor processor = processorChain.createProcessor(req, rsp);
    try {//from   ww  w . ja  va 2s.c o m
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();

        resultMap.put("start_time", new Date());
        List<Map<String, Object>> details = new ArrayList<Map<String, Object>>();

        try {

            int threads = determineThreadsNumber(params, streams.size());
            ThreadFactory threadFactory = new ThreadFactory() {
                public Thread newThread(Runnable r) {
                    return new Thread(r, "threadedReqeustHandler-" + new Date());
                }
            };
            executor = Executors.newFixedThreadPool(threads, threadFactory);
            String contentType = params.get(CommonParams.STREAM_CONTENTTYPE);

            Iterator<ContentStream> iterator = streams.iterator();
            while (iterator.hasNext()) {
                ContentStream stream = iterator.next();
                iterator.remove();
                if (stream instanceof ContentStreamBase) {
                    ((ContentStreamBase) stream).setContentType(contentType);

                }
                submitTask(req, rsp, processorChain, executor, stream, details);
            }

            executor.shutdown();

            boolean terminated = executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
            if (!terminated) {
                throw new RuntimeException("Request takes too much time");
            }
            // Perhaps commit from the parameters
            RequestHandlerUtils.handleCommit(req, processor, params, false);
            RequestHandlerUtils.handleRollback(req, processor, params, false);
        } finally {
            resultMap.put("end_time", new Date());

            // check whether there is error in details
            for (Map<String, Object> map : details) {
                Exception ex = (Exception) map.get("exception");
                if (ex != null) {
                    rsp.setException(ex);
                    if (ex instanceof SolrException) {
                        rsp.add("status", ((SolrException) ex).code());
                    } else {
                        rsp.add("status", SolrException.ErrorCode.BAD_REQUEST);
                    }
                    break;
                }
            }
        }
        resultMap.put("details", details);
        rsp.add("result", resultMap);
        return executor;
    } finally {
        if (executor != null && !executor.isShutdown()) {
            executor.shutdownNow();
        }
        // finish the request
        processor.finish();
    }
}