springboot整合极光推送与xxl-job


一、springboot整合xxl-job

首先去git上拉取大佬的代码https://gitee.com/xuxueli0323/xxl-job

拉取好后找到其中的sql文件并执行
在这里插入图片描述
然后按照自己喜好可以修改访问的端口号

注意xxl-job-admin与xxl-job-executor-samples中的xxl.job.accessToken一定要一致否则注册会失败

其中代码中已经编写好了许多Handler类 如果自己想增加一些任务自行修改编写便可
在这里插入图片描述
通过访问http://localhost:8080/xxl-job-admin/
在里面配置执行器与任务 便可正常使用

二、springboot整合极光推送

1.添加依赖

<!--极光推送-->
<dependency>
	<groupId>cn.jpush.api</groupId>
	<artifactId>jpush-client</artifactId>
	<version>3.2.9</version>
</dependency>

2.添加配置类
JPushConfig

import cn.jpush.api.JPushClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
 
@Configuration
@ConfigurationProperties
public class JPushConfig {
 
    // 极光账号上的 AppKey
    @Value("7fd04ce01c973d1ad703bcb4")
    private String appkey;
    // 极光账号上的 Master Secret
    @Value("a8414209858d8037654258d0")
    private String secret;
 
 
    private JPushClient jPushClient;
 
    /**
     * 推送客户端
     * @return
     */
    @PostConstruct
    public void initJPushClient() {
        jPushClient = new JPushClient(secret, appkey);
    }
 
    /**
     * 获取推送客户端
     * @return
     */
    public JPushClient getJPushClient() {
        return jPushClient;
    }
}

3.推送实体

import lombok.Data;
import java.util.Map;
 
@Data
public class PushBean {
 
    // 必填, 通知内容, 内容可以为空字符串,则表示不展示到通知栏。
    private String alert;
    // 可选, 附加信息, 供业务使用。
    private Map<String, String> extras;
    //android专用 可选, 通知标题	如果指定了,则通知里原来展示 App名称的地方,将展示成这个字段。
    private String title;
}

4.封装第三方API

import cn.jpush.api.push.model.PushPayload;
 
/**
 * 极光推送
 *
 * 封装第三方API
 */
public interface IJPushService {
 
    boolean pushAll(PushBean pushBean);
 
    boolean pushIos(PushBean pushBean);
 
    boolean pushIos(PushBean pushBean, String... registids);
 
    boolean pushAndroid(PushBean pushBean);
 
    boolean pushAndroid(PushBean pushBean, String... registids);
 
    boolean sendPush(PushPayload pushPayload);
}
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
/**
 * 极光推送
 *
 * 封装第三方API
 */
@Service
@Slf4j
public class JPushServiceImpl implements IJPushService {
 
    @Autowired
    private JPushConfig jPushConfig;
 
    /**
     * 广播 (所有平台,所有设备, 不支持附加信息)
     * @param pushBean 推送内容
     * @return
     */
    @Override
    public boolean pushAll(PushBean pushBean){
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.alert(pushBean.getAlert()))
                .build());
    }
 
    /**
     * ios广播
     * @param pushBean 推送内容
     * @return
     */
    @Override
    public boolean pushIos(PushBean pushBean){
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras()))
                .build());
    }
 
    /**
     * ios通过registid推送 (一次推送最多 1000 个)
     * @param pushBean 推送内容
     * @param registids 推送id
     * @return
     */
    @Override
    public boolean pushIos(PushBean pushBean, String... registids){
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.registrationId(registids))
                .setNotification(Notification.ios(pushBean.getAlert(), pushBean.getExtras()))
                .build());
    }
 
    /**
     * android广播
     * @param pushBean 推送内容
     * @return
     */
    @Override
    public boolean pushAndroid(PushBean pushBean){
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
                .build());
    }
 
    /**
     * android通过registid推送 (一次推送最多 1000 个)
     * @param pushBean 推送内容
     * @param registids 推送id
     * @return
     */
    @Override
    public boolean pushAndroid(PushBean pushBean, String ... registids){
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(registids))
                .setNotification(Notification.android(pushBean.getAlert(), pushBean.getTitle(), pushBean.getExtras()))
                .build());
    }
 
    /**
     * 调用api推送
     * @param pushPayload 推送实体
     * @return
     */
    @Override
    public boolean sendPush(PushPayload pushPayload){
        log.info("发送极光推送请求: {}", pushPayload);
        PushResult result = null;
        try{
            result = jPushConfig.getJPushClient().sendPush(pushPayload);
        } catch (APIConnectionException e) {
            log.error("极光推送连接异常: ", e);
        } catch (APIRequestException e) {
            log.error("极光推送请求异常: ", e);
        }
        if (result!=null && result.isResultOK()) {
            log.info("极光推送请求成功: {}", result);
            return true;
        }else {
            log.info("极光推送请求失败: {}", result);
            return false;
        }
    }
}
/**
 * 推送服务
 *
 * 封装业务功能
 */
public interface IMyJPushService {
 
    boolean pushAll(PushBean pushBean);
 
    boolean pushIos(PushBean pushBean);
 
    boolean pushIos(PushBean pushBean, String... registids);
 
    boolean pushAndroid(PushBean pushBean);
 
    boolean pushAndroid(PushBean pushBean, String... registids);
 
    String[] checkRegistids(String[] registids);
}

5 .测试

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
 
@Controller
public class JPushController {
 
    @Autowired
    private IMyJPushService service;
 
    /**
     * 广播
     * @param title
     * @param content
     * @param registids  指定id
     * @return
     */
    @PostMapping("/jpush")
    @ResponseBody
    public boolean test(String title, String content, String registids){
        PushBean pushBean = new PushBean();
        pushBean.setTitle(title);
        pushBean.setAlert(content);
        return service.pushIos(pushBean);
    }
}

6.编写xxl-job定时任务
在这里插入图片描述
7.在平台中添加任务
在这里插入图片描述
8.启动定时任务
最后完成整合

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值