Search in sources :

Example 1 with TimeoutTask

use of org.codelibs.core.timer.TimeoutTask in project fess-crawler by codelibs.

the class FtpClient method processRequest.

protected ResponseData processRequest(final String uri, final boolean includeContent) {
    if (ftpAuthenticationHolder == null) {
        init();
    }
    // start
    AccessTimeoutTarget accessTimeoutTarget = null;
    TimeoutTask accessTimeoutTask = null;
    if (accessTimeout != null) {
        accessTimeoutTarget = new AccessTimeoutTarget(Thread.currentThread());
        accessTimeoutTask = TimeoutManager.getInstance().addTimeoutTarget(accessTimeoutTarget, accessTimeout.intValue(), false);
    }
    try {
        return getResponseData(uri, includeContent);
    } finally {
        if (accessTimeout != null) {
            accessTimeoutTarget.stop();
            if (!accessTimeoutTask.isCanceled()) {
                accessTimeoutTask.cancel();
            }
        }
    }
}
Also used : AccessTimeoutTarget(org.codelibs.fess.crawler.client.AccessTimeoutTarget) TimeoutTask(org.codelibs.core.timer.TimeoutTask)

Example 2 with TimeoutTask

use of org.codelibs.core.timer.TimeoutTask in project fess-crawler by codelibs.

the class ApiExtractor method getText.

@Override
public ExtractData getText(final InputStream in, final Map<String, String> params) {
    if (logger.isDebugEnabled()) {
        logger.debug("Accessing " + url);
    }
    // start
    AccessTimeoutTarget accessTimeoutTarget = null;
    TimeoutTask accessTimeoutTask = null;
    if (accessTimeout != null) {
        accessTimeoutTarget = new AccessTimeoutTarget(Thread.currentThread());
        accessTimeoutTask = TimeoutManager.getInstance().addTimeoutTarget(accessTimeoutTarget, accessTimeout.intValue(), false);
    }
    final ExtractData data = new ExtractData();
    final HttpPost httpPost = new HttpPost(url);
    final HttpEntity postEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).setCharset(Charset.forName("UTF-8")).addBinaryBody("filedata", in).build();
    httpPost.setEntity(postEntity);
    try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
        if (response.getStatusLine().getStatusCode() != Constants.OK_STATUS_CODE) {
            logger.error("Failed to access " + url + ", code: " + response.getStatusLine().getStatusCode() + ".");
            return null;
        }
        data.setContent(EntityUtils.toString(response.getEntity(), Charsets.UTF_8));
        final Header[] headers = response.getAllHeaders();
        for (final Header header : headers) {
            data.putValue(header.getName(), header.getValue());
        }
    } catch (final IOException e) {
        throw new ExtractException(e);
    } finally {
        if (accessTimeout != null) {
            accessTimeoutTarget.stop();
            if (!accessTimeoutTask.isCanceled()) {
                accessTimeoutTask.cancel();
            }
        }
    }
    return data;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ExtractException(org.codelibs.fess.crawler.exception.ExtractException) AccessTimeoutTarget(org.codelibs.fess.crawler.client.AccessTimeoutTarget) ExtractData(org.codelibs.fess.crawler.entity.ExtractData) HttpEntity(org.apache.http.HttpEntity) RequestHeader(org.codelibs.fess.crawler.client.http.RequestHeader) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) TimeoutTask(org.codelibs.core.timer.TimeoutTask)

Example 3 with TimeoutTask

use of org.codelibs.core.timer.TimeoutTask in project fess by codelibs.

the class ThumbnailGenerator method main.

public static void main(final String[] args) {
    final Options options = new Options();
    final CmdLineParser parser = new CmdLineParser(options);
    try {
        parser.parseArgument(args);
    } catch (final CmdLineException e) {
        System.err.println(e.getMessage());
        System.err.println("java " + ThumbnailGenerator.class.getCanonicalName() + " [options...] arguments...");
        parser.printUsage(System.err);
        return;
    }
    if (logger.isDebugEnabled()) {
        try {
            ManagementFactory.getRuntimeMXBean().getInputArguments().stream().forEach(s -> logger.debug("Parameter: {}", s));
            System.getProperties().entrySet().stream().forEach(e -> logger.debug("Property: {}={}", e.getKey(), e.getValue()));
            System.getenv().entrySet().forEach(e -> logger.debug("Env: {}={}", e.getKey(), e.getValue()));
            logger.debug("Option: {}", options);
        } catch (final Exception e) {
        // ignore
        }
    }
    final String httpAddress = System.getProperty(Constants.FESS_ES_HTTP_ADDRESS);
    if (StringUtil.isNotBlank(httpAddress)) {
        System.setProperty(FesenClient.HTTP_ADDRESS, httpAddress);
    }
    TimeoutTask systemMonitorTask = null;
    int exitCode;
    try {
        SingletonLaContainerFactory.setConfigPath("app.xml");
        SingletonLaContainerFactory.setExternalContext(new GenericExternalContext());
        SingletonLaContainerFactory.setExternalContextComponentDefRegister(new GenericExternalContextComponentDefRegister());
        SingletonLaContainerFactory.init();
        final Thread shutdownCallback = new Thread("ShutdownHook") {

            @Override
            public void run() {
                if (logger.isDebugEnabled()) {
                    logger.debug("Destroying LaContainer..");
                }
                destroyContainer();
            }
        };
        Runtime.getRuntime().addShutdownHook(shutdownCallback);
        systemMonitorTask = TimeoutManager.getInstance().addTimeoutTarget(new SystemMonitorTarget(), ComponentUtil.getFessConfig().getThumbnailSystemMonitorIntervalAsInteger(), true);
        final int totalCount = process(options);
        if (totalCount != 0) {
            logger.info("Created {} thumbnail files.", totalCount);
        } else {
            logger.info("No new thumbnails found.");
        }
        exitCode = 0;
    } catch (final ContainerNotAvailableException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("ThumbnailGenerator is stopped.", e);
        } else if (logger.isInfoEnabled()) {
            logger.info("ThumbnailGenerator is stopped.");
        }
        exitCode = Constants.EXIT_FAIL;
    } catch (final Throwable t) {
        logger.error("ThumbnailGenerator does not work correctly.", t);
        exitCode = Constants.EXIT_FAIL;
    } finally {
        if (systemMonitorTask != null) {
            systemMonitorTask.cancel();
        }
        destroyContainer();
    }
    System.exit(exitCode);
}
Also used : ContainerNotAvailableException(org.codelibs.fess.exception.ContainerNotAvailableException) CmdLineParser(org.kohsuke.args4j.CmdLineParser) SystemMonitorTarget(org.codelibs.fess.timer.SystemMonitorTarget) ContainerNotAvailableException(org.codelibs.fess.exception.ContainerNotAvailableException) CmdLineException(org.kohsuke.args4j.CmdLineException) TimeoutTask(org.codelibs.core.timer.TimeoutTask) GenericExternalContext(org.lastaflute.di.core.external.GenericExternalContext) CmdLineException(org.kohsuke.args4j.CmdLineException) GenericExternalContextComponentDefRegister(org.lastaflute.di.core.external.GenericExternalContextComponentDefRegister)

Example 4 with TimeoutTask

use of org.codelibs.core.timer.TimeoutTask in project fess by codelibs.

the class SuggestCreator method main.

public static void main(final String[] args) {
    final Options options = new Options();
    final CmdLineParser parser = new CmdLineParser(options);
    try {
        parser.parseArgument(args);
    } catch (final CmdLineException e) {
        System.err.println(e.getMessage());
        System.err.println("java " + SuggestCreator.class.getCanonicalName() + " [options...] arguments...");
        parser.printUsage(System.err);
        return;
    }
    if (logger.isDebugEnabled()) {
        try {
            ManagementFactory.getRuntimeMXBean().getInputArguments().stream().forEach(s -> logger.debug("Parameter: {}", s));
            System.getProperties().entrySet().stream().forEach(e -> logger.debug("Property: {}={}", e.getKey(), e.getValue()));
            System.getenv().entrySet().forEach(e -> logger.debug("Env: {}={}", e.getKey(), e.getValue()));
            logger.debug("Option: {}", options);
        } catch (final Exception e) {
        // ignore
        }
    }
    final String httpAddress = System.getProperty(Constants.FESS_ES_HTTP_ADDRESS);
    if (StringUtil.isNotBlank(httpAddress)) {
        System.setProperty(FesenClient.HTTP_ADDRESS, httpAddress);
    }
    TimeoutTask systemMonitorTask = null;
    int exitCode;
    try {
        SingletonLaContainerFactory.setConfigPath("app.xml");
        SingletonLaContainerFactory.setExternalContext(new GenericExternalContext());
        SingletonLaContainerFactory.setExternalContextComponentDefRegister(new GenericExternalContextComponentDefRegister());
        SingletonLaContainerFactory.init();
        final Thread shutdownCallback = new Thread("ShutdownHook") {

            @Override
            public void run() {
                if (logger.isDebugEnabled()) {
                    logger.debug("Destroying LaContainer..");
                }
                destroyContainer();
            }
        };
        Runtime.getRuntime().addShutdownHook(shutdownCallback);
        systemMonitorTask = TimeoutManager.getInstance().addTimeoutTarget(new SystemMonitorTarget(), ComponentUtil.getFessConfig().getSuggestSystemMonitorIntervalAsInteger(), true);
        exitCode = process(options);
    } catch (final ContainerNotAvailableException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("SuggestCreator is stopped.", e);
        } else if (logger.isInfoEnabled()) {
            logger.info("SuggestCreator is stopped.");
        }
        exitCode = Constants.EXIT_FAIL;
    } catch (final Throwable t) {
        logger.error("Suggest creator does not work correctly.", t);
        exitCode = Constants.EXIT_FAIL;
    } finally {
        if (systemMonitorTask != null) {
            systemMonitorTask.cancel();
        }
        destroyContainer();
    }
    logger.info("Finished SuggestCreator.");
    System.exit(exitCode);
}
Also used : ContainerNotAvailableException(org.codelibs.fess.exception.ContainerNotAvailableException) CmdLineParser(org.kohsuke.args4j.CmdLineParser) SystemMonitorTarget(org.codelibs.fess.timer.SystemMonitorTarget) ContainerNotAvailableException(org.codelibs.fess.exception.ContainerNotAvailableException) CmdLineException(org.kohsuke.args4j.CmdLineException) TimeoutTask(org.codelibs.core.timer.TimeoutTask) GenericExternalContext(org.lastaflute.di.core.external.GenericExternalContext) CmdLineException(org.kohsuke.args4j.CmdLineException) GenericExternalContextComponentDefRegister(org.lastaflute.di.core.external.GenericExternalContextComponentDefRegister)

Example 5 with TimeoutTask

use of org.codelibs.core.timer.TimeoutTask in project fess by codelibs.

the class SuggestJob method execute.

@Override
public String execute() {
    final StringBuilder resultBuf = new StringBuilder();
    if (sessionId == null) {
        // create session id
        sessionId = RandomStringUtils.randomAlphabetic(15);
    }
    resultBuf.append("Session Id: ").append(sessionId).append("\n");
    if (jobExecutor != null) {
        jobExecutor.addShutdownListener(() -> ComponentUtil.getProcessHelper().destroyProcess(sessionId));
    }
    final TimeoutTask timeoutTask = createTimeoutTask();
    try {
        executeSuggestCreator();
    } catch (final Exception e) {
        logger.warn("Failed to create suggest data.", e);
        resultBuf.append(e.getMessage()).append("\n");
    } finally {
        if (timeoutTask != null && !timeoutTask.isCanceled()) {
            timeoutTask.cancel();
        }
    }
    return resultBuf.toString();
}
Also used : JobProcessingException(org.codelibs.fess.exception.JobProcessingException) TimeoutTask(org.codelibs.core.timer.TimeoutTask)

Aggregations

TimeoutTask (org.codelibs.core.timer.TimeoutTask)13 AccessTimeoutTarget (org.codelibs.fess.crawler.client.AccessTimeoutTarget)5 JobProcessingException (org.codelibs.fess.exception.JobProcessingException)4 ContainerNotAvailableException (org.codelibs.fess.exception.ContainerNotAvailableException)3 SystemMonitorTarget (org.codelibs.fess.timer.SystemMonitorTarget)3 CmdLineException (org.kohsuke.args4j.CmdLineException)3 CmdLineParser (org.kohsuke.args4j.CmdLineParser)3 GenericExternalContext (org.lastaflute.di.core.external.GenericExternalContext)3 GenericExternalContextComponentDefRegister (org.lastaflute.di.core.external.GenericExternalContextComponentDefRegister)3 IOException (java.io.IOException)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 BasicHeader (org.apache.http.message.BasicHeader)1 InterruptedRuntimeException (org.codelibs.core.exception.InterruptedRuntimeException)1