com.github.ltsopensource.spring.TaskTrackerAnnotationFactoryBean Maven / Gradle / Ivy
The newest version!
package com.github.ltsopensource.spring;
import com.github.ltsopensource.autoconfigure.PropertiesConfigurationFactory;
import com.github.ltsopensource.core.commons.utils.CollectionUtils;
import com.github.ltsopensource.core.commons.utils.StringUtils;
import com.github.ltsopensource.core.constant.Level;
import com.github.ltsopensource.core.listener.MasterChangeListener;
import com.github.ltsopensource.spring.tasktracker.JobDispatcher;
import com.github.ltsopensource.tasktracker.TaskTracker;
import com.github.ltsopensource.tasktracker.TaskTrackerBuilder;
import com.github.ltsopensource.core.properties.TaskTrackerProperties;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.github.ltsopensource.tasktracker.runner.RunnerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.Map;
import java.util.Properties;
/**
* TaskTracker Spring Bean 工厂类
* 如果用这个工厂类,那么JobRunner中引用SpringBean的话,只有通过注解的方式注入
*
* @author Robert HG ([email protected]) on 8/4/15.
*/
@SuppressWarnings("rawtypes")
public class TaskTrackerAnnotationFactoryBean implements FactoryBean, ApplicationContextAware,
InitializingBean, DisposableBean {
private ApplicationContext applicationContext;
private TaskTracker taskTracker;
private boolean started;
/**
* 集群名称
*/
private String clusterName;
/**
* 节点组名称
*/
private String nodeGroup;
/**
* zookeeper地址
*/
private String registryAddress;
/**
* 提交失败任务存储路径 , 默认用户木邻居
*/
private String dataPath;
/**
* 工作线程个数
*/
private int workThreads;
/**
* 任务执行类
*/
private Class jobRunnerClass;
/**
* 业务日志级别
*/
private Level bizLoggerLevel;
/**
* spring中jobRunner的bean name
*/
private String jobRunnerBeanName;
/**
* master节点变化监听器
*/
private MasterChangeListener[] masterChangeListeners;
/**
* 只有当使用 JobDispatcher 的时候才有效果
*/
private String shardField;
private String identity;
private String bindIp;
/**
* 额外参数配置
*/
private Properties configs = new Properties();
private String[] locations;
@Override
public TaskTracker getObject() throws Exception {
return taskTracker;
}
@Override
public Class> getObjectType() {
return TaskTracker.class;
}
@Override
public boolean isSingleton() {
return true;
}
@SuppressWarnings("unchecked")
@Override
public void afterPropertiesSet() throws Exception {
TaskTrackerProperties properties = null;
if (locations == null || locations.length == 0) {
properties = new TaskTrackerProperties();
properties.setClusterName(clusterName);
properties.setDataPath(dataPath);
properties.setNodeGroup(nodeGroup);
properties.setRegistryAddress(registryAddress);
properties.setBindIp(bindIp);
properties.setIdentity(identity);
properties.setWorkThreads(workThreads);
properties.setConfigs(CollectionUtils.toMap(configs));
properties.setBizLoggerLevel(bizLoggerLevel);
} else {
properties = PropertiesConfigurationFactory
.createPropertiesConfiguration(TaskTrackerProperties.class, locations);
}
taskTracker = TaskTrackerBuilder.buildByProperties(properties);
registerRunnerBeanDefinition();
// 设置config
for (Map.Entry