【极光系列】springBoot集成xxl-job调度器

【springboot集成xxl-job】

一.gitee地址

直接下载可用 https://gitee.com/shawsongyue/aurora.git

模块:aurora_xxl_job

二.mysql安装教程

参考我的另一篇文章:https://blog.csdn.net/weixin_40736233/article/details/135582926?spm=1001.2014.3001.5501

三.集成xxl-job-admin管理页面步骤

1.下载xxl-job-admin源码

#1.下载源码,解压后用idea打开xxl-job项目,如下图
GitHub地址:http://github.com/xuxueli/xxl-job
Gitee地址:https://gitee.com/xuxueli0323/xxl-job

在这里插入图片描述

2.创建数据库以及相关表

执行源码ddl路径: /doc/db/tables_xxl_job.sql

3.修改xxl-job-admin相关配置

源码配置路径: /xxl-job/xxl-job-admin/src/main/resources/application.properties

#修改为你自己的数据库以及账号密码
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=xiaosongyue.1997

4.直接启动服务

xxl-job-admin模块下的主启动类:XxlJobAdminApplication.class

5.访问管理页面

地址:http://127.0.0.1:8088/xxl-job-admin

tips:默认账号admin,密码123456

在这里插入图片描述

四.项目集成xxl-job任务步骤

1.引入pom.xml依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xsy</groupId>
    <artifactId>aurora_xxl_job</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--基础SpringBoot依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
    </parent>

    <!--属性设置-->
    <properties>
        <!--java_JDK版本-->
        <java.version>1.8</java.version>
        <!--maven打包插件-->
        <maven.plugin.version>3.8.1</maven.plugin.version>
        <!--编译编码UTF-8-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--输出报告编码UTF-8-->
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!--json数据格式处理工具-->
        <fastjson.version>1.2.75</fastjson.version>
        <!--json数据格式处理工具-->
        <xxljob.version>2.3.0</xxljob.version>
    </properties>

    <!--通用依赖-->
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-job-core</artifactId>
            <version>${xxljob.version}</version>
        </dependency>

    </dependencies>

    <!--编译打包-->
    <build>
        <finalName>${project.name}</finalName>
        <!--资源文件打包-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>

        <!--插件统一管理-->
        <pluginManagement>
            <plugins>
                <!--maven打包插件-->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring.boot.version}</version>
                    <configuration>
                        <fork>true</fork>
                        <finalName>${project.build.finalName}</finalName>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <!--编译打包插件-->
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven.plugin.version}</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>UTF-8</encoding>
                        <compilerArgs>
                            <arg>-parameters</arg>
                        </compilerArgs>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <!--配置Maven项目中需要使用的远程仓库-->
    <repositories>
        <repository>
            <id>aliyun-repos</id>
            <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <!--用来配置maven插件的远程仓库-->
    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-plugin</id>
            <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

2.修改配置application.yml

#服务配置
server:
  #端口
  port: 7003

#spring配置
spring:
  #应用配置
  application:
    #应用名
    name: aurora_xxl_job

#xxl-job
xxl:
  job:
    admin:
      #管理页面地址,需要鱼xxl-job-admin管理页面访问地址一样
      addresses: http://127.0.0.1:8088/xxl-job-admin
    #执行器
    executor:
      #需要平台页面配置一样
      appname: aurora-job-executor
      ip:
      port: 9003
      logpath: /data/applogs/xxl-job/aurora-job-executor
      logretentiondays: -1
    #token,必须配置,否则当前项目无法连接服务端
    accessToken: default_token

3.包结构如下

在这里插入图片描述

4.创建主启动类

package com.aurora;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author 浅夏的猫
 * @description 主启动类
 * @date 22:46 2024/1/13
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

5.创建xxl-job配置类

package com.aurora.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class XxlJobConfig {

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.executor.appname}")
    private String appName;

    @Value("${xxl.job.executor.ip}")
    private String ip;

    @Value("${xxl.job.executor.port}")
    private int port;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;

    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppname(appName);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        return xxlJobSpringExecutor;
    }

}

6.创建一个任务执行器验证

package com.aurora.jobHandler;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AuroraJobHandler {

    private static Logger logger = LoggerFactory.getLogger(AuroraJobHandler.class);

    @XxlJob("auroraJobHandler")
    public ReturnT<String> lessExpireSchoolFileJob(String param) {
        logger.info("正在执行定时任务!!!");
        String jobParam = XxlJobHelper.getJobParam();
        return ReturnT.SUCCESS;
    }
}

7.启动当前项目,验证是否正常注册到服务端

(1)新增对应的执行器,执行器名称与你项目配置的appname需要一样

(2)新增以后启动本地服务,观察是否可以正常注册

在这里插入图片描述

8.验证调度任务

tips:任务管理器–选择上面创建的极光任务管理器–新增调度任务,创建以后,点击右边的操作,执行一次,观察项目服务器日志

在这里插入图片描述

在这里插入图片描述

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是Spring Boot集成极光推送的步骤: 1. 在pom.xml文件中添加极光推送的依赖: ```xml <dependency> <groupId>cn.jiguang</groupId> <artifactId>jpush-spring-boot-starter</artifactId> <version>3.4.0</version> </dependency> ``` 2. 在application.yml或application.properties文件中配置极光推送的相关信息: ```yaml jpush: app-key: f54*******************92 master-secret: d6*****************055 production: true ``` 请注意将上述代码中的app-key和master-secret替换为你自己的极光推送的AppKey和AppSecret。 3. 创建一个推送服务类,用于发送推送消息: ```java import cn.jpush.api.JPushClient; import cn.jpush.api.push.PushResult; import cn.jpush.api.push.model.Message; import cn.jpush.api.push.model.Platform; import cn.jpush.api.push.model.PushPayload; import cn.jpush.api.push.model.audience.Audience; import cn.jpush.api.push.model.notification.Notification; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service public class JPushService { @Value("${jpush.app-key}") private String appKey; @Value("${jpush.master-secret}") private String masterSecret; @Value("${jpush.production}") private boolean production; public void sendPush(String message) { JPushClient jpushClient = new JPushClient(masterSecret, appKey); PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.all()) .setAudience(Audience.all()) .setMessage(Message.content(message)) .build(); try { PushResult result = jpushClient.sendPush(payload); System.out.println("推送结果:" + result); } catch (Exception e) { e.printStackTrace(); } } } ``` 4. 在需要发送推送消息的地方调用推送服务类的sendPush方法: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class PushController { @Autowired private JPushService jPushService; @GetMapping("/push") public String pushMessage() { jPushService.sendPush("这是一条测试推送消息"); return "推送成功"; } } ``` 以上代码演示了如何在Spring Boot集成极光推送,并发送一条推送消息。你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏之以寒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值