Example usage for java.util.concurrent Executors newCachedThreadPool

List of usage examples for java.util.concurrent Executors newCachedThreadPool

Introduction

In this page you can find the example usage for java.util.concurrent Executors newCachedThreadPool.

Prototype

public static ExecutorService newCachedThreadPool() 

Source Link

Document

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.

Usage

From source file:fr.inria.edelweiss.kgdqp.core.CentralizedInferrencingNoSpin.java

public static void main(String args[])
        throws ParseException, EngineException, InterruptedException, IOException, LoadException {

    List<String> endpoints = new ArrayList<String>();
    String queryPath = null;//from   w ww.  j a v  a 2 s .  com
    boolean rulesSelection = false;
    File rulesDir = null;
    File ontDir = null;

    /////////////////
    Graph graph = Graph.create();
    QueryProcess exec = QueryProcess.create(graph);

    Options options = new Options();
    Option helpOpt = new Option("h", "help", false, "print this message");
    //        Option queryOpt = new Option("q", "query", true, "specify the sparql query file");
    //        Option endpointOpt = new Option("e", "endpoint", true, "a federated sparql endpoint URL");
    Option versionOpt = new Option("v", "version", false, "print the version information and exit");
    Option rulesOpt = new Option("r", "rulesDir", true, "directory containing the inference rules");
    Option ontOpt = new Option("o", "ontologiesDir", true,
            "directory containing the ontologies for rules selection");
    //        Option locOpt = new Option("c", "centralized", false, "performs centralized inferences");
    Option dataOpt = new Option("l", "load", true, "data file or directory to be loaded");
    //        Option selOpt = new Option("s", "rulesSelection", false, "if set to true, only the applicable rules are run");
    //        options.addOption(queryOpt);
    //        options.addOption(endpointOpt);
    options.addOption(helpOpt);
    options.addOption(versionOpt);
    options.addOption(rulesOpt);
    options.addOption(ontOpt);
    //        options.addOption(selOpt);
    //        options.addOption(locOpt);
    options.addOption(dataOpt);

    String header = "Corese/KGRAM rule engine experiment command line interface";
    String footer = "\nPlease report any issue to alban.gaignard@cnrs.fr, olivier.corby@inria.fr";

    CommandLineParser parser = new BasicParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("kgdqp", header, options, footer, true);
        System.exit(0);
    }
    if (cmd.hasOption("o")) {
        rulesSelection = true;
        String ontDirPath = cmd.getOptionValue("o");
        ontDir = new File(ontDirPath);
        if (!ontDir.isDirectory()) {
            logger.warn(ontDirPath + " is not a valid directory path.");
            System.exit(0);
        }
    }
    if (!cmd.hasOption("r")) {
        logger.info("You must specify a path for inference rules directory !");
        System.exit(0);
    }

    if (cmd.hasOption("l")) {
        String[] dataPaths = cmd.getOptionValues("l");
        for (String path : dataPaths) {
            Load ld = Load.create(graph);
            ld.load(path);
            logger.info("Loaded " + path);
        }
    }

    if (cmd.hasOption("v")) {
        logger.info("version 3.0.4-SNAPSHOT");
        System.exit(0);
    }

    String rulesDirPath = cmd.getOptionValue("r");
    rulesDir = new File(rulesDirPath);
    if (!rulesDir.isDirectory()) {
        logger.warn(rulesDirPath + " is not a valid directory path.");
        System.exit(0);
    }

    // Local rules graph initialization
    Graph rulesG = Graph.create();
    Load ld = Load.create(rulesG);

    if (rulesSelection) {
        // Ontology loading
        if (ontDir.isDirectory()) {
            for (File o : ontDir.listFiles()) {
                logger.info("Loading " + o.getAbsolutePath());
                ld.load(o.getAbsolutePath());
            }
        }
    }

    // Rules loading
    if (rulesDir.isDirectory()) {
        for (File r : rulesDir.listFiles()) {
            if (r.getAbsolutePath().endsWith(".rq")) {
                logger.info("Loading " + r.getAbsolutePath());
                //                ld.load(r.getAbsolutePath());

                //                    byte[] encoded = Files.readAllBytes(Paths.get(r.getAbsolutePath()));
                //                    String construct = new String(encoded, "UTF-8"); //StandardCharsets.UTF_8);

                FileInputStream f = new FileInputStream(r);
                QueryLoad ql = QueryLoad.create();
                String construct = ql.read(f);
                f.close();

                SPINProcess sp = SPINProcess.create();
                String spinConstruct = sp.toSpin(construct);

                ld.load(new ByteArrayInputStream(spinConstruct.getBytes()), Load.TURTLE_FORMAT);
                logger.info("Rules graph size : " + rulesG.size());

            }
        }
    }

    // Rule engine initialization
    RuleEngine ruleEngine = RuleEngine.create(graph);
    ruleEngine.set(exec);
    ruleEngine.setOptimize(true);
    ruleEngine.setConstructResult(true);
    ruleEngine.setTrace(true);

    StopWatch sw = new StopWatch();
    logger.info("Federated graph size : " + graph.size());
    logger.info("Rules graph size : " + rulesG.size());

    // Rule selection
    logger.info("Rules selection");
    QueryProcess localKgram = QueryProcess.create(rulesG);
    ArrayList<String> applicableRules = new ArrayList<String>();
    sw.start();
    String rulesSelQuery = "";
    if (rulesSelection) {
        rulesSelQuery = pertinentRulesQuery;
    } else {
        rulesSelQuery = allRulesQuery;
    }
    Mappings maps = localKgram.query(rulesSelQuery);
    logger.info("Rules selected in " + sw.getTime() + " ms");
    logger.info("Applicable rules : " + maps.size());

    // Selected rule loading
    for (Mapping map : maps) {
        IDatatype dt = (IDatatype) map.getValue("?res");
        String rule = dt.getLabel();
        //loading rule in the rule engine
        //            logger.info("Adding rule : ");
        //            System.out.println("-------");
        //            System.out.println(rule);
        //            System.out.println("");
        //            if (! rule.toLowerCase().contains("sameas")) {
        applicableRules.add(rule);
        ruleEngine.addRule(rule);
        //            }
    }

    // Rules application on distributed sparql endpoints
    logger.info("Rules application (" + applicableRules.size() + " rules)");
    ExecutorService threadPool = Executors.newCachedThreadPool();
    RuleEngineThread ruleThread = new RuleEngineThread(ruleEngine);
    sw.reset();
    sw.start();

    //        ruleEngine.process();
    threadPool.execute(ruleThread);
    threadPool.shutdown();

    //monitoring loop
    while (!threadPool.isTerminated()) {
        //            System.out.println("******************************");
        //            System.out.println(Util.jsonDqpCost(QueryProcessDQP.queryCounter, QueryProcessDQP.queryVolumeCounter, QueryProcessDQP.sourceCounter, QueryProcessDQP.sourceVolumeCounter));
        //            System.out.println("Rule engine running for " + sw.getTime() + " ms");
        //            System.out.println("Federated graph size : " + graph.size());
        System.out.println(sw.getTime() + " , " + graph.size());
        Thread.sleep(5000);
    }

    logger.info("Federated graph size : " + graph.size());
    //        logger.info(Util.jsonDqpCost(QueryProcessDQP.queryCounter, QueryProcessDQP.queryVolumeCounter, QueryProcessDQP.sourceCounter, QueryProcessDQP.sourceVolumeCounter));

    //        TripleFormat f = TripleFormat.create(graph, true);
    //        f.write("/tmp/gAll.ttl");
}

From source file:my.adam.smo.server.HTTPServer.java

@Inject
public HTTPServer(@Value("${server_worker_threads:10}") int workerCount) {
    bootstrap.setFactory(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool(), workerCount));

    ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() {
        @Override//from w ww  .  j av  a 2 s  .c  o m
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline p = Channels.pipeline();

            if (enableTrafficLogging) {
                InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
                p.addLast("logger", new LoggingHandler(InternalLogLevel.DEBUG));
            }

            p.addLast("codec", new HttpServerCodec());
            p.addLast("chunkAggregator", new HttpChunkAggregator(MAX_CONTENT_LENGTH));
            p.addLast("chunkedWriter", new ChunkedWriteHandler());
            p.addLast("compressor", new HttpContentCompressor());

            p.addLast("handler", new SimpleChannelUpstreamHandler() {
                @Override
                public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
                    StopWatch stopWatch = new StopWatch("messageReceived");
                    stopWatch.start();

                    final DefaultHttpRequest httpRequest = (DefaultHttpRequest) e.getMessage();
                    ChannelBuffer cb = Base64.decode(httpRequest.getContent(), Base64Dialect.STANDARD);

                    RPCommunication.Request request = RPCommunication.Request
                            .parseFrom(cb.copy(0, cb.readableBytes()).array());
                    logger.trace("received request:" + request.toString());

                    if (enableAsymmetricEncryption) {
                        request = getAsymDecryptedRequest(request);
                        logger.trace("asymmetric encryption enabled, decrypted request: " + request.toString());
                    }

                    if (enableSymmetricEncryption) {
                        request = getDecryptedRequest(request);
                        logger.trace("symmetric encryption enabled, decrypted request: " + request.toString());
                    }

                    final RPCommunication.Request protoRequest = request;

                    RpcController dummyController = new DummyRpcController();
                    Service service = serviceMap.get(request.getServiceName());

                    logger.trace("got service: " + service + " for name " + request.getServiceName());

                    Descriptors.MethodDescriptor methodToCall = service.getDescriptorForType()
                            .findMethodByName(request.getMethodName());

                    logger.trace("got method: " + methodToCall + " for name " + request.getMethodName());

                    Message methodArguments = service.getRequestPrototype(methodToCall).newBuilderForType()
                            .mergeFrom(request.getMethodArgument()).build();

                    logger.trace("get method arguments from request " + methodArguments.toString());

                    RpcCallback<Message> callback = new RpcCallback<Message>() {
                        @Override
                        public void run(Message parameter) {
                            HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
                                    HttpResponseStatus.OK);

                            ByteArrayOutputStream paramOutputStream = new ByteArrayOutputStream();
                            CodedOutputStream paramCodedOutputStream = CodedOutputStream
                                    .newInstance(paramOutputStream);
                            try {
                                parameter.writeTo(paramCodedOutputStream);
                                paramCodedOutputStream.flush();
                            } catch (IOException e1) {
                                logger.error("failed to write to output stream");
                            }

                            RPCommunication.Response response = RPCommunication.Response.newBuilder()
                                    .setResponse(ByteString.copyFrom(paramOutputStream.toByteArray()))
                                    .setRequestId(protoRequest.getRequestId()).build();

                            if (enableSymmetricEncryption) {
                                response = getEncryptedResponse(response);
                                logger.trace("symmetric encryption enabled, encrypted response: "
                                        + response.toString());
                            }

                            if (enableAsymmetricEncryption) {
                                response = getAsymEncryptedResponse(response);
                                logger.trace("asymmetric encryption enabled, encrypted response: "
                                        + response.toString());
                            }

                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                            CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputStream);
                            try {
                                response.writeTo(codedOutputStream);
                                codedOutputStream.flush();
                            } catch (IOException e1) {
                                logger.error("unable to write to output stream", e1);
                            }

                            byte[] arr = outputStream.toByteArray();

                            ChannelBuffer resp = Base64.encode(ChannelBuffers.copiedBuffer(arr),
                                    Base64Dialect.STANDARD);

                            httpResponse.setContent(resp);
                            httpResponse.addHeader(HttpHeaders.Names.CONTENT_LENGTH, resp.readableBytes());
                            httpResponse.addHeader(HttpHeaders.Names.CONTENT_TYPE,
                                    HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);

                            e.getChannel().write(httpResponse);
                            logger.trace("finishing call, httpResponse sent");
                        }
                    };
                    logger.trace("calling " + methodToCall.getFullName());
                    service.callMethod(methodToCall, dummyController, methodArguments, callback);
                    stopWatch.stop();
                    logger.trace(stopWatch.shortSummary());
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
                    if (!standardExceptionHandling(ctx, e)) {
                        super.exceptionCaught(ctx, e);
                    }
                }
            });
            return p;
        }
    };
    bootstrap.setPipelineFactory(pipelineFactory);
}

From source file:com.synconset.ImageFetcher.java

public ImageFetcher(Context ct) {
    context = ct;//from   w ww.j ava2s . c  o m
    executor = Executors.newCachedThreadPool();

    dir_thumb = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "thumb");
    if (!dir_thumb.isDirectory() && !dir_thumb.mkdir()) {
        // failed
    }

    try {
        FileUtils.cleanDirectory(dir_thumb);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:de.huberlin.wbi.hiway.am.cuneiform.CuneiformApplicationMaster.java

public CuneiformApplicationMaster() {
    super();/*from ww  w. jav  a  2  s . com*/
    ExecutorService executor = Executors.newCachedThreadPool();

    creActor = new HiWayCreActor(this);
    executor.submit(creActor);

    ticketSrc = new TicketSrcActor(creActor);
    executor.submit(ticketSrc);
    executor.shutdown();
}

From source file:dpfmanager.shell.modules.server.core.ServerService.java

@PostConstruct
public void init() {
    // No context yet
    bundle = DPFManagerProperties.getBundle();
    executor = Executors.newCachedThreadPool();
    // Random port
    port = randInt(49152, 65535);//from w w  w  .  j a  v a  2  s  .  co m
}

From source file:com.ottogroup.bi.asap.pipeline.MicroPipelineManager.java

/**
 * Initializes the manager using the provided input
 * @param componentRepository/*from   w w  w  .  j a  v a 2s. com*/
 */
public MicroPipelineManager(final ComponentRepository componentRepository) {
    this.factory = new MicroPipelineFactory(componentRepository);
    this.pipelineThreadPool = Executors.newCachedThreadPool();
}

From source file:co.cask.tigon.sql.ioserver.OutputServerSocketTest.java

@BeforeClass
public static void init() {
    clientFactory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool());
    serverFacotry = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool());
    clientBootstrap = new ClientBootstrap(clientFactory);
}

From source file:org.cryptomator.ui.MainModule.java

@Provides
@Singleton
ExecutorService getExec() {
    return closeLater(Executors.newCachedThreadPool(), ExecutorService::shutdown);
}

From source file:com.ottogroup.bi.spqr.pipeline.component.source.SourceRuntimeEnvironment.java

/**
 * Initializes the runtime environment using the provided input
 * @param processingNodeId// www  .  j a va2 s .c  o m
 * @param pipelineId
 * @param source
 * @param queueProducer
 * @throws RequiredInputMissingException
 */
public SourceRuntimeEnvironment(final String processingNodeId, final String pipelineId, final Source source,
        final StreamingMessageQueueProducer queueProducer) throws RequiredInputMissingException {
    this(processingNodeId, pipelineId, source, queueProducer, Executors.newCachedThreadPool());
    this.localExecutorService = true;
}

From source file:oz.hadoop.yarn.api.net.AbstractSocketHandler.java

/**
 * /* www. j  a va  2 s  .  c o  m*/
 * @param address
 * @param server
 */
public AbstractSocketHandler(InetSocketAddress address, boolean server, Runnable onDisconnectTask) {
    Assert.notNull(address);
    this.onDisconnectTask = onDisconnectTask;
    this.address = address;
    this.executor = Executors.newCachedThreadPool();
    this.listenerTask = new ListenerTask();
    this.readingBuffer = ByteBuffer.allocate(16384);
    this.bufferPoll = new ByteBufferPool();
    try {
        this.rootChannel = server ? ServerSocketChannel.open() : SocketChannel.open();
        if (logger.isDebugEnabled()) {
            logger.debug("Created instance of " + this.getClass().getSimpleName());
        }
    } catch (Exception e) {
        throw new IllegalStateException("Failed to create an instance of the ApplicationContainerClient", e);
    }
    this.thisClass = this.getClass();
    this.lifeCycleLatch = new CountDownLatch(1);
}