TBSchedule应用实战(二)—— 使用Spring配置TBSchedule

原创
2017/12/18 17:28
阅读数 2.4K

    上节我们使用最少的代码启动的tbs调度器并且成功运行了一个job,那么反正tbs强依赖Spring,索性我们也就把二者整合在一起,整合步骤同官方文档一样,这里也就把配置文件贴上,并附上Spring启动版示例工程。

示例工程下载:点击下载

1、Spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <context:property-placeholder location="classpath*:*.properties"/>

    <context:component-scan base-package="com.oschina.wed"/>

    <context:annotation-config/>

    <bean id="scheduleManagerFactory" class="com.taobao.pamirs.schedule.strategy.TBScheduleManagerFactory"
          init-method="init">
        <property name="zkConfig">
            <map>
                <entry key="zkConnectString" value="172.16.60.12:2181,172.16.60.16:2182,172.16.60.33:2183"/>
                <entry key="rootPath" value="/tbschedule/wed/tasks"/>
                <entry key="zkSessionTimeout" value="60000"/>
                <entry key="userName" value=""/>
                <entry key="password" value=""/>
                <entry key="isCheckParentPath" value="true"/>
            </map>
        </property>
    </bean>
</beans>

2、修改入口类:

package com.oschina.wed;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    /**
     * 由于我们已经将调度器交给了Spring管理,
     * 那么此时我们需要做的只是启动容器就好。
     *
     * @param args
     */
    public static void main(String... args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");

        while (true) {

        }
    }
}

3、调度器工厂类已无用武之地,设为过期:

package com.oschina.wed;

import com.taobao.pamirs.schedule.strategy.TBScheduleManagerFactory;
import java.util.HashMap;
import java.util.Map;

/**
 * TBSchedule调度器工厂类
 */
@Deprecated
public class TBScheduleFactory {

    /**
     * 获取一个调度器实例
     * @param zkAddress 注册中心地址
     * @param taskPath 调度器注册路径
     * @param zkConnctionTimeout 注册中心连接超时时长
     * @param aclUserName 如需加密,此为用户名,tbs默认采用zookeeper digest加密方式
     * @param aclPwd 如需加密,此为密码,tbs默认采用zookeeper digest加密方式
     * @return
     */
    public static TBScheduleManagerFactory getTbsFactory(String zkAddress, String taskPath, String zkConnctionTimeout, String aclUserName, String aclPwd) {
        try {
            //tbs调度器的编程式注册
            TBScheduleManagerFactory tbScheduleManagerFactory = new TBScheduleManagerFactory();
            Map<String, String> zkConfig = new HashMap<String, String>();
            zkConfig.put("zkConnectString", zkAddress);
            zkConfig.put("rootPath", taskPath);
            zkConfig.put("zkSessionTimeout", zkConnctionTimeout);
            zkConfig.put("userName", aclUserName);
            zkConfig.put("password", aclPwd);
            tbScheduleManagerFactory.setZkConfig(zkConfig);
            //初始化zk链接,建立注册信息
            tbScheduleManagerFactory.init();
            return tbScheduleManagerFactory;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部