package com.bocloud.ims.service.thread;

import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.ims.interfaces.resource.CloudServerService;

public class CloudServerCreateTask implements Runnable {
	private static Logger logger = LoggerFactory.getLogger(CloudServerCreateTask.class);

	private LinkedBlockingQueue<CloudServerParam> queue;
	private CloudServerService cloudServerService;

	public CloudServerCreateTask(LinkedBlockingQueue<CloudServerParam> queue, CloudServerService cloudServerService) {
		super();
		this.queue = queue;
		this.cloudServerService = cloudServerService;
	}

	@Override
	public void run() {
		if (null == queue) {
			queue = new LinkedBlockingQueue<>();
		}
		boolean flag = true;
		while (flag) {
			CloudServerParam param;
			try {
				param = queue.take();
				if ("create".equalsIgnoreCase(param.getType())) {
					cloudServerService.create(param.getParam(), param.getContext());
				} else {
//					cloudServerService.apply(param.getParam(), param.getContext());
				}
			} catch (InterruptedException e) {
				logger.error("CloudServerCreateTask InterruptedException!", e);
				flag = false;
				Thread.currentThread().interrupt();
			}
		}
	}
}
