com.taotao.boot.sms.common.handler.AbstractSendHandler Maven / Gradle / Ivy
Show all versions of taotao-boot-starter-sms-common Show documentation
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.taotao.boot.sms.common.handler;
import com.taotao.boot.sms.common.event.SmsSendFailEvent;
import com.taotao.boot.sms.common.event.SmsSendFinallyEvent;
import com.taotao.boot.sms.common.event.SmsSendSuccessEvent;
import com.taotao.boot.sms.common.model.NoticeData;
import com.taotao.boot.sms.common.properties.AbstractHandlerProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.lang.Nullable;
import java.util.Collection;
/**
* 抽象发送处理
*
* @author shuigedeng
* @version 2022.04
* @since 2022-04-27 17:48:15
*/
public abstract class AbstractSendHandler> implements SendHandler {
protected final P properties;
private final ApplicationEventPublisher eventPublisher;
public AbstractSendHandler(P properties, @Nullable ApplicationEventPublisher eventPublisher) {
this.properties = properties;
this.eventPublisher = eventPublisher;
}
@Override
public boolean acceptSend(@Nullable String type) {
return properties.getTemplates().containsKey(type);
}
/**
* 获取通道名称
*
* @return {@link String }
* @since 2022-04-27 17:48:18
*/
public abstract String getChannelName();
/**
* 发布发送成功事件
*
* @param noticeData 通知内容
* @param phones 手机号列表
* @since 2022-04-27 17:48:21
*/
protected final void publishSendSuccessEvent(NoticeData noticeData, Collection phones, Object response) {
if (eventPublisher == null) {
return;
}
eventPublisher.publishEvent(new SmsSendSuccessEvent(
this, getChannelName(), phones, noticeData.getType(), noticeData.getParams(), response));
publishSendFinallyEvent(noticeData, phones, response);
}
/**
* 发布发送失败事件
*
* @param noticeData 通知内容
* @param phones 手机号列表
* @param cause 源异常
* @since 2022-04-27 17:48:23
*/
protected final void publishSendFailEvent(
NoticeData noticeData, Collection phones, Throwable cause, Object response) {
if (eventPublisher == null) {
return;
}
eventPublisher.publishEvent(new SmsSendFailEvent(
this, getChannelName(), phones, noticeData.getType(), noticeData.getParams(), cause, response));
publishSendFinallyEvent(noticeData, phones, response);
}
/**
* 发布发送结束事件
*
* @param noticeData 通知内容
* @param phones 手机号列表
* @since 2022-04-27 17:48:26
*/
private void publishSendFinallyEvent(NoticeData noticeData, Collection phones, Object response) {
if (eventPublisher == null) {
return;
}
eventPublisher.publishEvent(new SmsSendFinallyEvent(
this, getChannelName(), phones, noticeData.getType(), noticeData.getParams(), response));
}
}