com.project.atm.model.async.ProcessExecutor.java Source code

Java tutorial

Introduction

Here is the source code for com.project.atm.model.async.ProcessExecutor.java

Source

/**
 * Innovez-One, Proprietary Software Cloud Communications
 *  Copyright (c) 2015, Innovez-One and individual contributors
 *  by the @authors tag.
 *
 *  This program is Proprietary Software: you can not redistribute it and/or modify
 *  without license from Innovez-One.
 *
 *  Website : http://www.innovez-one.com/
 *  Report bugs to <techsupport@innovez-one.com>.
 *  Copyright (C) 2015 PT. Innovez-One. All rights reserved.
 */
package com.project.atm.model.async;

import com.project.atm.common.util.ResourceHelper;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * Author andry on 21/01/16.
 */
public class ProcessExecutor implements ApplicationListener<ContextClosedEvent> {
    private static final Logger logger = Logger.getLogger(ProcessExecutor.class);

    private ThreadPoolTaskExecutor taskExecutor;

    public ProcessExecutor(ThreadPoolTaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;

        String processThread = ResourceHelper.getAppResource("processThread");
        if (processThread.isEmpty() || processThread.equals("")) {
            taskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors() / 2);
        } else {
            try {
                taskExecutor.setCorePoolSize(Integer.parseInt(processThread));
            } catch (NumberFormatException nfe) {
                logger.error(nfe.getMessage() + ", set to default configuration");
                taskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors() / 2);
            }
        }

    }

    public void execute(Runnable task) {
        taskExecutor.execute(task);
    }

    public ThreadPoolTaskExecutor getTaskExecutor() {
        return taskExecutor;
    }

    public void setTaskExecutor(ThreadPoolTaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }

    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        logger.info("process executor shutdown");
        taskExecutor.shutdown();
    }
}