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

uw.task.TaskScheduler Maven / Gradle / Ivy

Go to download

uw-task包是一个分布式任务框架,通过uw-task可以快速构建基于docker的分布式任务体系,同时支持基于influx的任务运维监控。

There is a newer version: 1.1.17
Show newest version
package uw.task;

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.postprocessor.GUnzipPostProcessor;
import org.springframework.amqp.support.postprocessor.GZipPostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import uw.task.util.GlobalSequenceManager;

/**
 * 任务调度器,通过调用此类,把任务发送到队列中执行。
 * 
 * @author axeon
 *
 */
@Component
public class TaskScheduler {

	private static final Logger log = LoggerFactory.getLogger(TaskScheduler.class);

	@Autowired
	private AmqpTemplate rabbitTemplate;

	@Autowired
	private GlobalSequenceManager sequence;

	/**
	 * 线程池。
	 */
	private ExecutorService executorService = Executors
			.newCachedThreadPool(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("TaskScheduler-%d").build());

	/**
	 * smsTemplate 转换器用Jackson2JsonMessageConverter,用于json转换实体类对象
	 * 
	 * @param connectionFactory
	 * @return
	 */
	@Bean
	RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
		RabbitTemplate template = new RabbitTemplate(connectionFactory);
		template.setMessageConverter(new Jackson2JsonMessageConverter());
		template.setBeforePublishPostProcessors(new GZipPostProcessor());
		template.setAfterReceivePostProcessors(new GUnzipPostProcessor());
		template.setReplyTimeout(60000);
		template.afterPropertiesSet();
		return template;
	}

	/**
	 * 把任务发送到队列中
	 * 
	 * @param taskdata
	 *            任务数据
	 */
	public void sendToQueue(TaskData taskdata) {
		sendToQueue("", taskdata);
	}

	/**
	 * 把任务发送到队列中。
	 * 
	 * @param target
	 *            目标主机配置名,如果没有,则为空
	 * @param taskdata
	 */
	public void sendToQueue(String target, TaskData taskdata) {
		taskdata.setId(sequence.nextId("task_runner_log"));
		taskdata.setQueueDate(new Date());
		taskdata.setRunType(TaskData.RUN_TYPE_GLOBAL);
		String queue = taskdata.getTaskClass() + "$" + (target==null?"":target);
		rabbitTemplate.convertAndSend(queue + "@exchange", queue + "@route", taskdata);
	}

	/**
	 * 远程运行任务,并返回future>。 如果需要获得数据,可以使用futrue.get()来获得。
	 * 
	 * @param taskdata
	 *            任务数据
	 * @param timeoutTimeMillis
	 *            超时毫秒数,超过这个时间没有得到结果,直接返回null
	 * @param firstWaitTimeMillis
	 *            第一次等待时间毫秒数
	 * @param retryIntervalTimeMillis
	 *            重试间隔毫秒数
	 * @return
	 */
	public  Future> runTask(TaskData taskdata) {
		return runTask("", taskdata);
	}

	/**
	 * 远程运行任务,并返回future>。 如果需要获得数据,可以使用futrue.get()来获得。
	 * 
	 * @param target
	 *            目标主机配置名,如果没有,则为空
	 * @param taskdata
	 *            任务数据
	 * @return
	 */
	public  Future> runTask(String target, TaskData taskdata) {
		taskdata.setId(sequence.nextId("task_runner_log"));
		taskdata.setQueueDate(new Date());
		taskdata.setRunType(TaskData.RUN_TYPE_GLOBAL_RPC);
		Future> future = executorService.submit(new Callable>() {
			public TaskData call() {
				// 返回数据
				String queue = taskdata.getTaskClass() + "$" + (target==null?"":target);
				TaskData retdata = (TaskData) rabbitTemplate.convertSendAndReceive(queue + "@exchange",
						queue + "@route", taskdata);
				return retdata;
			}
		});
		return future;
	}

	/**
	 * 本地直接运行任务。 在此执行任务,可能会导致阻塞。
	 * 
	 * @param taskdata
	 */
	public Future> runTaskLocal(TaskData taskdata) {
		Future> future = executorService.submit(new Callable>() {
			public TaskData call() {
				taskdata.setId(sequence.nextId("task_runner_log"));
				taskdata.setQueueDate(new Date());
				taskdata.setRunType(TaskData.RUN_TYPE_LOCAL);
				return taskdata;
			}
		});
		return future;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy