Commit 78f4e166 by 穆龙飞

modify:修改邮件发送逻辑

parent ddb652bc
Pipeline #44041 passed with stages
in 1 minute 35 seconds
package com.netease.mail.yanxuan.change.integration.client;
public interface Checker {
/**
* 校验结果是否成功
*
* @return
*/
boolean check();
}
package com.netease.mail.yanxuan.change.integration.client;
public class LabelLevel {
/**
* 分类信息,100:色情,110:性感,200:广告,210:二维码,300:暴恐,400:违禁,500:涉政
*/
private int label;
/**
* 分类级别,0:正常,1:不确定,2:确定
*/
private int level;
public LabelLevel() {}
public LabelLevel(int label, int level) {
this.label = label;
this.level = level;
}
public int getLabel() {
return label;
}
public void setLabel(int label) {
this.label = label;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
package com.netease.mail.yanxuan.change.integration.client;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netease.mail.yanxuan.change.integration.rpc.RpcResultException;
import java.io.IOException;
import java.util.List;
public class RpcListResult<T> implements RpcTemplate.CallHandler<List<T>> {
private final Class<T> tClass;
public RpcListResult(Class<T> tClass) {
this.tClass = tClass;
}
@Override
public List<T> handle(String resp) throws IOException {
JSONObject rpcResult = JSON.parseObject(resp);
int code = rpcResult.getIntValue("code");
if (code == 200) {
return JSON.parseArray(rpcResult.getString("result"), tClass);
}
throw new RpcResultException("response error, code = " + code);
}
}
package com.netease.mail.yanxuan.change.integration.client;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netease.mail.yanxuan.change.integration.rpc.RpcResultException;
import java.io.IOException;
public class RpcObjectData<T> implements RpcTemplate.CallHandler<T> {
private Class<T> tClass;
public RpcObjectData(Class<T> tClass) {
this.tClass = tClass;
}
@Override
public T handle(String resp) throws IOException {
JSONObject rpcResult = JSON.parseObject(resp);
int code = rpcResult.getIntValue("code");
if (code == 200) {
return JSON.parseObject(rpcResult.getString("data"), tClass);
}
throw new RpcResultException("response error, code = " + code);
}
}
package com.netease.mail.yanxuan.change.integration.client;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netease.mail.yanxuan.change.integration.rpc.RpcResultException;
import lombok.SneakyThrows;
import java.io.IOException;
public class RpcObjectDataMsg<T> implements RpcTemplate.CallHandler<T> {
private Class<T> tClass;
public RpcObjectDataMsg(Class<T> tClass) {
this.tClass = tClass;
}
@SneakyThrows
@Override
public T handle(String resp) throws IOException {
JSONObject rpcResult = JSON.parseObject(resp);
int code = rpcResult.getIntValue("code");
if (code == 200) {
return JSON.parseObject(rpcResult.getString("data"), tClass);
}
throw new RpcResultException(rpcResult.getString("msg"));
}
}
package com.netease.mail.yanxuan.change.integration.client;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netease.mail.yanxuan.change.integration.rpc.RpcResultException;
import java.io.IOException;
public class RpcObjectResult<T> implements RpcTemplate.CallHandler<T> {
private Class<T> tClass;
public RpcObjectResult(Class<T> tClass) {
this.tClass = tClass;
}
@Override
public T handle(String resp) throws IOException {
JSONObject rpcResult = JSON.parseObject(resp);
int code = rpcResult.getIntValue("code");
if (code == 200) {
return JSON.parseObject(rpcResult.getString("result"), tClass);
}
throw new RpcResultException("response error, code = " + code);
}
}
package com.netease.mail.yanxuan.change.integration.client;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.netease.mail.yanxuan.change.integration.email.exception.RpcException;
import com.netease.mail.yanxuan.change.integration.email.exception.RpcTimeoutException;
import com.netease.mail.yanxuan.change.integration.email.util.EncodeUtil;
import com.netease.mail.yanxuan.change.integration.email.util.HttpClientUtil;
import com.netease.yanxuan.missa.pressure.TracerClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@Component
public class RpcTemplate {
private static final Logger LOG = LoggerFactory.getLogger(RpcTemplate.class);
@Value("${act.rpc.get.retry:1}")
private int rpcGetRetry;
@Value("${act.rpc.post.retry:0}")
private int rpcPostRetry;
@Value("${act.rpc.collect:false}")
private boolean rpcCollect;
public RpcTemplate() {
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
}
/**
* Get请求
*
* @param url
* @param handler
* @param <T>
* @return
*/
public <T> T get(String url, CallHandler<T> handler) {
return get(url, null, null, -1, handler);
}
/**
* Get请求
*
* @param url
* @param params
* @param handler
* @param <T>
* @return
*/
public <T> T get(String url, Map<String, Object> params, CallHandler<T> handler) {
return get(url, params, null, -1, handler);
}
/**
* Get请求
*
* @param url
* @param params
* @param header
* @param timeout
* @param handler
* @param <T>
* @return
*/
public <T> T get(String url, Map<String, Object> params, Map<String, String> header,
int timeout, CallHandler<T> handler) {
CallTemplate template = () -> HttpClientUtil.get(url, params, header, timeout);
return this.execute(url, params, handler, template, rpcGetRetry, Method.GET);
}
/**
* Post请求
*
* @param url
* @param params
* @param handler
* @param <T>
* @return
*/
public <T> T post(String url, Map<String, Object> params, CallHandler<T> handler) {
return post(url, params, -1, handler);
}
/**
* Post请求
*
* @param url
* @param params
* @param timeout
* @param handler
* @param <T>
* @return
*/
public <T> T post(String url, Map<String, Object> params, int timeout, CallHandler<T> handler) {
CallTemplate template = () -> HttpClientUtil.post(url, params, timeout);
return this.execute(url, params, handler, template, rpcPostRetry, Method.POST);
}
/**
* Post Json请求
*
* @param url
* @param params
* @param handler
* @param <T>
* @return
*/
public <T> T postJson(String url, String params, CallHandler<T> handler) {
return postJson(url, params, -1, handler);
}
/**
* Post Json请求
*
* @param url
* @param params
* @param timeout
* @param handler
* @param <T>
* @return
*/
public <T> T postJson(String url, String params, int timeout, CallHandler<T> handler) {
CallTemplate template = () -> HttpClientUtil.postJson(url, params, timeout);
return this.execute(url, params, handler, template, rpcPostRetry, Method.BODY);
}
/**
* 执行模板调用
*
* @param url
* 请求地址
* @param params
* 请求报文
* @param handler
* 返回解析
* @param callTemplate
* 模板调用
* @param retryTime
* 最多重复次数,0默认(3次)
*/
private <T> T execute(String url, Object params, CallHandler<T> handler,
CallTemplate callTemplate, int retryTime, Method method) {
int tryTimes = 0;
RpcException rpcException = null;
String respBody = null;
RpcStatus status = RpcStatus.SUCCESS;
while (tryTimes <= retryTime) {
long startTime = System.currentTimeMillis();
try {
respBody = callTemplate.executeCall();
if (StringUtils.isEmpty(respBody)) {
throw new RpcException("response empty");
}
T t = handler.handle(respBody);
status = checkResp(handler, t);
return t;
} catch (RpcTimeoutException e) {
rpcException = e;
status = RpcStatus.TIMEOUT;
} catch (RpcException e) {
rpcException = e;
status = RpcStatus.EXCEPTION;
} catch (Exception e) {
rpcException = new RpcException(e);
status = RpcStatus.EXCEPTION;
} finally {
try {
if (LOG.isDebugEnabled() || rpcCollect) {
long costTime = System.currentTimeMillis() - startTime;
String reqParam = convertParam(params);
// 根据需要开启rpc日志
if (LOG.isDebugEnabled()) {
LOG.debug(
"process request, yx-tracer-flowType={}, url={}, method={}, params={}, response={}, cost={}ms",
TracerClient.getFlowType(), url, method.name(), reqParam, respBody,
costTime);
}
}
} catch (Exception e) {
LOG.error("collect error, msg={}", e.getMessage());
}
}
tryTimes++;
}
LOG.error("request error, url={}, params={}, respBody={}", url,
params instanceof String ? params : JSON.toJSONString(params), respBody, rpcException);
if (rpcException == null) {
rpcException = new RpcException("服务异常");
}
throw rpcException;
}
/**
* 校验resp
*
* @param t
* @param <T>
* @return
*/
private <T> RpcStatus checkResp(CallHandler<T> handler, T t) {
RpcStatus status = RpcStatus.SUCCESS;
try {
// 为空,或handlerCheck为false直接判断FAIL
if (t == null || !handler.check(t)) {
return RpcStatus.WARNING;
}
if (t instanceof Checker) {
status = ((Checker) t).check() ? RpcStatus.SUCCESS : RpcStatus.WARNING;
} else if (t instanceof List) {
// 为空认为不是理想结果
if (CollectionUtils.isEmpty((List) t)) {
status = RpcStatus.WARNING;
} else {
// 列表数据简单检测第一个元素
Object obj = ((List) t).get(0);
if (obj instanceof Checker) {
status = ((Checker) obj).check() ? RpcStatus.SUCCESS : RpcStatus.WARNING;
}
}
}
} catch (Exception e) {
LOG.error("collect error, msg={}", e.getMessage());
}
return status;
}
private String convertParam(Object param) {
if (param instanceof String) {
return (String) param;
}
String reqParam = "";
if (param instanceof Map) {
reqParam = EncodeUtil.encodeMap((Map) param);
} else if (param != null) {
reqParam = JSON.toJSONString(param);
}
return reqParam;
}
enum RpcStatus {
/**
* 成功
*/
SUCCESS(1),
/**
* 告警(请求成功,但非理想值)
*/
WARNING(2),
/**
* 异常
*/
EXCEPTION(3),
/**
* 超时
*/
TIMEOUT(4);
private final int value;
RpcStatus(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
enum Method {
/**
* Get请求
*/
GET,
/**
* Post表单请求
*/
POST,
/**
* 数据体(一般是post json)
*/
BODY
}
/**
* 模板调用
*/
@FunctionalInterface
public interface CallTemplate {
/**
* 模板调用类
*/
String executeCall();
}
/**
* 报文解析
*/
@FunctionalInterface
public interface CallHandler<T> {
/**
* 报文解析
*
* @param resp
* response body
*/
T handle(String resp) throws IOException;
/**
* 校验数据体
*
* @param t
* @return
*/
default boolean check(T t) {
return true;
}
}
}
package com.netease.mail.yanxuan.change.integration.client;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netease.mail.yanxuan.change.integration.rpc.RpcResultException;
import java.io.IOException;
public class SinoInfoRpcObjectData<T> implements RpcTemplate.CallHandler<T> {
private final Class<T> tClass;
public SinoInfoRpcObjectData(Class<T> tClass) {
this.tClass = tClass;
}
@Override
public T handle(String resp) throws IOException {
JSONObject rpcResult = JSON.parseObject(resp);
int code = rpcResult.getIntValue("code");
if (code == 200) {
return JSON.parseObject(rpcResult.getString("shareInfo"), tClass);
}
throw new RpcResultException("response error, code = " + code);
}
}
package com.netease.mail.yanxuan.change.integration.client;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
public class SinoRpcObjectData<T> implements RpcTemplate.CallHandler<T> {
private final Class<T> tClass;
public SinoRpcObjectData(Class<T> tClass) {
this.tClass = tClass;
}
@Override
public T handle(String resp) throws IOException {
JSONObject rpcResult = JSON.parseObject(resp);
return JSON.parseObject(JSON.toJSONString(rpcResult), tClass);
}
}
package com.netease.mail.yanxuan.change.integration.client;
import org.apache.http.conn.ssl.TrustStrategy;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* 不校验,统统认为是可信的
*
* Created by leikun on 2017/5/7.
*/
public class TrustAnyStrategy implements TrustStrategy {
private static final TrustAnyStrategy INSTANCE = new TrustAnyStrategy();
public static TrustAnyStrategy get() {
return INSTANCE;
}
private TrustAnyStrategy() {}
@Override
public boolean isTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
return true;
}
}
......@@ -2,10 +2,10 @@ package com.netease.mail.yanxuan.change.integration.email.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netease.mail.yanxuan.change.integration.client.RpcObjectData;
import com.netease.mail.yanxuan.change.integration.client.RpcTemplate;
import com.netease.mail.yanxuan.change.integration.email.service.UasClientService;
import com.netease.mail.yanxuan.change.integration.flow.email.EmailRpcService;
import com.netease.mail.yanxuan.change.integration.rpc.RpcResultException;
import com.netease.mail.yanxuan.change.integration.rpc.RpcTemplate;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -58,7 +58,14 @@ public class UasClientServiceImpl implements UasClientService {
params.put("cc", StringUtils.join(ccList, ","));
}
logger.info(JSONObject.toJSONString("邮箱接口入参 subject={},content={},toList={},ccList={}"), subject, content, StringUtils.join(toList, ","), params.get("cc"));
rpcTemplate.post(url, params, new RpcObjectData<>(String.class));
rpcTemplate.post(url, params, -1, resp -> {
JSONObject json = JSON.parseObject(resp);
int code = json.getInteger("code");
if (code != 200) {
throw new RpcResultException("检测异常");
}
return Boolean.TRUE;
});
//String res = service.sendEmail(subject, content, StringUtils.join(toList, ","), params.get("cc"));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment