All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.schedulerx.worker.pull.ConsumerThread Maven / Gradle / Ivy

There is a newer version: 1.12.2
Show newest version
package com.alibaba.schedulerx.worker.pull;

import java.io.IOException;

import com.alibaba.schedulerx.protocol.Worker.MasterStartContainerRequest;
import com.alibaba.schedulerx.worker.SchedulerxWorker;
import com.alibaba.schedulerx.worker.container.ContainerPool;
import com.alibaba.schedulerx.worker.container.ThreadContainer;
import com.alibaba.schedulerx.worker.domain.JobContext;
import com.alibaba.schedulerx.worker.log.LogFactory;
import com.alibaba.schedulerx.worker.log.Logger;
import com.alibaba.schedulerx.worker.processor.JobProcessor;
import com.alibaba.schedulerx.worker.processor.JobProcessorEx;
import com.alibaba.schedulerx.worker.util.ContanerUtil;

/**
 *
 * @author xiaomeng.hxm
 */
public class ConsumerThread extends ThreadContainer {

    private volatile boolean running = true;
    private BlockingContainerQueue queue;
    private static Logger LOGGER = LogFactory.getLogger(ConsumerThread.class);

    private Thread currentThread;

    public ConsumerThread(JobContext context, ContainerPool containerPool) throws Exception {
        super(context, containerPool);
    }

    public ConsumerThread(BlockingContainerQueue queue, ContainerPool containerPool, String taskMasterAkkaPath) throws Exception {
        this.queue = queue;
        this.containerPool = containerPool;
        this.masterActorSelection = SchedulerxWorker.actorSystem.actorSelection(taskMasterAkkaPath);
        if (masterActorSelection == null) {
            String errMsg = "get taskMaster akka path error, path=" + taskMasterAkkaPath;
            LOGGER.error(errMsg);
            throw new IOException(errMsg);
        }
    }


    @Override
    public void run() {
        currentThread = Thread.currentThread();
        while (running) {
            try {
                MasterStartContainerRequest request = queue.get();
                if (request != null) {
                    JobContext context = ContanerUtil.convert2JobContext(request);
                    setContext(context);
                    containerPool.put(context.getUniqueId(), this);
                    super.start();
                }
            } catch (Exception e) {
                LOGGER.error("ConsumerThread run failed.", e);
            }
        }
    }

    public void stopRunning() {
        running = false;
    }

    @Override
    public void kill(boolean mayInterruptIfRunning) {
        JobContext context = this.getContext();
        JobProcessor jobProcessor = this.getJobProcessor();
        LOGGER.info("kill container, jobInstanceId={}", context.getJobInstanceId());
        if (jobProcessor != null && (jobProcessor instanceof JobProcessorEx)) {
            ((JobProcessorEx) jobProcessor).kill(context);
        }
        this.currentThread.interrupt();
        containerPool.remove(context.getUniqueId());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy