Files
java-servers/servers/boe-server-all/src/main/java/com/xboe/stat/EventDataSender.java
2022-11-29 13:32:43 +08:00

86 lines
2.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.xboe.stat;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xboe.core.SysConstant;
import com.xboe.core.api.TokenProxy;
import com.xboe.core.event.IEventDataSender;
import com.xboe.core.utils.OkHttpUtil;
import com.xboe.standard.BaseConstant;
import lombok.extern.slf4j.Slf4j;
/**
* 事件数据发送实现,如果是云环境,只需要修改此类的实现就可以了
* @author seastar
*
*/
@Slf4j
@Component
public class EventDataSender implements IEventDataSender{
@Autowired
private OkHttpUtil okHttpUtil;
@Autowired
private HttpServletRequest request;
@Override
public void send(String title, String eventKey, String content, String objId, String objType, String objInfo,
String aid, String aname,String parameters) {
String statBaseUrl=SysConstant.getConfigValue("xboe.stat.base.url");
if(StringUtils.isBlank(statBaseUrl)) {
log.error("发送事件失败:未配置【xboe.stat.base.url】的值");
return;
}
String urlPre="/xboe/m/stat/event/send";
//案例同步不需要token
if(eventKey.equals("SyncCase")) {
urlPre ="/inner/stat/event/send";
}
final String url = statBaseUrl + urlPre;
Map<String, String> params = new HashMap<>();
params.put("title", title);
params.put("source", "all");
params.put("content", content);
params.put("objId", objId);
params.put("key", eventKey);
params.put("objType", objType);
params.put("objInfo", objInfo);
params.put("aid", aid);
params.put("aname", aname);
params.put("parameters",parameters);
String token = TokenProxy.getToken(request);
//最后采用异常发送,不影响当前进程
new Thread(()->{
try {
ObjectMapper mapper=new ObjectMapper();
String json =mapper.writeValueAsString(params);
//String[] headers=new String[] {"token",token};
String[] headers=new String[] {BaseConstant.HTTP_ACCESS_TOKEN,token};
String responseStr = okHttpUtil.doPostJson(url, json,headers);
if(responseStr.indexOf("\"status\":200")==-1) {
log.error("发送事件失败:"+responseStr);
log.info("【发送的token】"+headers[0]+" "+headers[1]);
}
}catch(Exception e) {
log.error("发送事件错误",e);
}
}).start();
}
}