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

    上节我们使用最少的代码启动的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;
        }
    }
}

 

转载于:https://my.oschina.net/wednesday/blog/1591560

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值