xxl_job的分片功能

需求:我们现在实现这样的需求,在指定节假日,需要给平台的所有用户去发送祝福的短信.

初始化数据

集成Druid&MyBatis

<!--MyBatis驱动-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>
<!--mysql驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok依赖-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>

添加配置

spring.datasource.url=jdbc:mysql://localhost:3306/xxl_job_demo?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.username=root
spring.datasource.password=WolfCode_2017

添加实体类

@Setter@Getter
public class UserMobilePlan {
    private Long id;//主键
    private String username;//用户名
    private String nickname;//昵称
    private String phone;//手机号码
    private String info;//备注
}

添加Mapper处理类

@Mapper
public interface UserMobilePlanMapper {
    @Select("select * from t_user_mobile_plan")
    List<UserMobilePlan> selectAll();
}

业务功能实现

任务处理方法实现

@XxlJob("sendMsgHandler")
public void sendMsgHandler() throws Exception{
    List<UserMobilePlan> userMobilePlans = userMobilePlanMapper.selectAll();
    System.out.println("任务开始时间:"+new Date()+",处理任务数量:"+userMobilePlans.size());
    Long startTime = System.currentTimeMillis();
    userMobilePlans.forEach(item->{
        try {
            //模拟发送短信动作
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    System.out.println("任务结束时间:"+new Date());
    System.out.println("任务耗时:"+(System.currentTimeMillis()-startTime)+"毫秒");
}

任务配置信息

分片概念讲解

比如我们的案例中有2000+条数据,如果不采取分片形式的话,任务只会在一台机器上执行,这样的话需要20+秒才能执行完任务.

如果采取分片广播的形式的话,一次任务调度将会广播触发对应集群中所有执行器执行一次任务,同时系统自动传递分片参数;可根据分片参数开发分片任务;

获取分片参数方式:

// 可参考Sample示例执行器中的示例任务"ShardingJobHandler"了解试用 
int shardIndex = XxlJobHelper.getShardIndex();
int shardTotal = XxlJobHelper.getShardTotal();

通过这两个参数,我们可以通过求模取余的方式,分别查询,分别执行,这样的话就可以提高处理的速度.

之前2000+条数据只在一台机器上执行需要20+秒才能完成任务,分片后,有两台机器可以共同完成2000+条数据,每台机器处理1000+条数据,这样的话只需要10+秒就能完成任务

案例改造成任务分片

Mapper增加查询方法

@Mapper
public interface UserMobilePlanMapper {
    @Select("select * from t_user_mobile_plan where mod(id,#{shardingTotal})=#{shardingIndex}")
    List<UserMobilePlan> selectByMod(@Param("shardingIndex") Integer shardingIndex,@Param("shardingTotal")Integer shardingTotal);
    @Select("select * from t_user_mobile_plan")
    List<UserMobilePlan> selectAll();
}

 任务类方法

@XxlJob("sendMsgShardingHandler")
public void sendMsgShardingHandler() throws Exception{
    System.out.println("任务开始时间:"+new Date());
    int shardTotal = XxlJobHelper.getShardTotal();
    int shardIndex = XxlJobHelper.getShardIndex();
    List<UserMobilePlan> userMobilePlans = null;
    if(shardTotal==1){
        //如果没有分片就直接查询所有数据
        userMobilePlans = userMobilePlanMapper.selectAll();
    }else{
        userMobilePlans = userMobilePlanMapper.selectByMod(shardIndex,shardTotal);
    }
    System.out.println("处理任务数量:"+userMobilePlans.size());
    Long startTime = System.currentTimeMillis();
    userMobilePlans.forEach(item->{
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    System.out.println("任务结束时间:"+new Date());
    System.out.println("任务耗时:"+(System.currentTimeMillis()-startTime)+"毫秒");
}

任务设置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
xxl-job 提供了分片广播任务的能力,可以将一个任务分成多个子任务并行执行,从而提高任务执行效率。 要在 Spring Boot 中集成 xxl-job 分片广播任务,需要进行以下几个步骤: 1. 在 xxl-job 后台管理中创建一个分片广播任务,并配置好任务参数和调度策略。 2. 在 Spring Boot 项目中引入 xxl-job 的依赖: ```xml <dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.3.0</version> </dependency> ``` 3. 在 Spring Boot 项目的 application.properties 或 application.yml 文件中配置 xxl-job 的相关参数,例如: ```yaml xxl.job.admin.addresses=http://localhost:8080/xxl-job-admin xxl.job.executor.appname=my-job xxl.job.executor.ip= xxl.job.executor.port=9999 xxl.job.accessToken= ``` 其中 `xxl.job.admin.addresses` 是 xxl-job 后台管理地址,`xxl.job.executor.appname` 是执行器名称,`xxl.job.executor.ip` 是执行器 IP 地址,`xxl.job.executor.port` 是执行器端口号,`xxl.job.accessToken` 是访问令牌,可以在 xxl-job 后台管理中进行配置。 4. 在 Spring Boot 项目中定义一个分片广播任务的执行器(Executor),例如: ```java @XxlJob("shardingJobHandler") @Component public class ShardingJobHandler { @Autowired private XxlJobExecutor xxlJobExecutor; @XxlJob("shardingJobHandler") public ReturnT<String> execute(String param) throws Exception { // 获取当前任务的分片信息 ShardingUtil.ShardingVO sharding = ShardingUtil.getShardingVo(); int index = sharding.getIndex(); int total = sharding.getTotal(); // 根据分片信息执行任务 for (int i = index; i < 100; i += total) { // 执行任务逻辑 xxlJobExecutor.log("shardingJobHandler executing: " + i); } return ReturnT.SUCCESS; } } ``` 在这个示例中,我们定义了一个名为 `shardingJobHandler` 的分片广播任务执行器,并使用 `@XxlJob` 注解将其标记为 xxl-job 的任务处理器,同时使用 `@Component` 注解将其声明为 Spring Boot 的组件。在 `execute` 方法中,我们通过 `ShardingUtil.getShardingVo()` 方法获取当前任务的分片信息,并根据分片信息执行任务逻辑。 5. 在 xxl-job 后台管理中启动分片广播任务,并观察任务执行情况。 通过以上步骤,我们就可以在 Spring Boot 中集成 xxl-job 分片广播任务,并实现分片执行逻辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值