fix: 移除不需要的代码
This commit is contained in:
parent
d0910837b6
commit
ba7c4f2a34
|
|
@ -1,34 +0,0 @@
|
||||||
package com.nflg.mobilebroken.admin.controller;
|
|
||||||
|
|
||||||
import com.nflg.mobilebroken.admin.annotation.ApiMark;
|
|
||||||
import com.nflg.mobilebroken.starter.service.impl.APPSSEManagerService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@Slf4j
|
|
||||||
@RequestMapping("/sse")
|
|
||||||
public class SSEController extends ControllerBase {
|
|
||||||
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private APPSSEManagerService sseManagerService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 建立sse连接
|
|
||||||
*
|
|
||||||
* @param userId 用户id
|
|
||||||
*/
|
|
||||||
@GetMapping(value = "connect", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
|
||||||
@ApiMark(moduleName = "连接", apiName = "SSE", isPublic = true)
|
|
||||||
public SseEmitter connect(@RequestParam String userId) {
|
|
||||||
return sseManagerService.connect(Integer.valueOf(userId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,43 +0,0 @@
|
||||||
package com.nflg.mobilebroken.admin.service.impl;
|
|
||||||
|
|
||||||
import com.nflg.mobilebroken.common.constant.Constant;
|
|
||||||
import com.nflg.mobilebroken.common.pojo.dto.UserDTO;
|
|
||||||
import com.nflg.mobilebroken.starter.service.INotifyPushService;
|
|
||||||
import com.nflg.mobilebroken.starter.service.SSEManagerService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
@Slf4j
|
|
||||||
public class SSEINotifyPushService implements INotifyPushService {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
@Qualifier("APPSSEManagerService")
|
|
||||||
private SSEManagerService sseManagerService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private RedisTemplate<String, Object> redisTemplate;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void push(UserDTO user, String subject, String content) {
|
|
||||||
// try {
|
|
||||||
// SSEMessageDTO message = new SSEMessageDTO()
|
|
||||||
// .setType(2)
|
|
||||||
// .setData(new NotifyDTO().setSubject(subject).setContent(content));
|
|
||||||
// sseManagerService.send(user.getId(), message);
|
|
||||||
// } catch (IOException e) {
|
|
||||||
// log.error("发送SSE失败", e);
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean check(UserDTO user) {
|
|
||||||
Object value = redisTemplate.opsForHash().get("message:config:" + user.getId(), Constant.REDIS_KEY_MESSAGECONFIG_APP);
|
|
||||||
return Objects.isNull(value) || (boolean) value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
package com.nflg.mobilebroken.cfs.controller;
|
|
||||||
|
|
||||||
import com.nflg.mobilebroken.starter.service.impl.AdminSSEManagerService;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 统计分析相关
|
|
||||||
*/
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/analysis")
|
|
||||||
public class AnalysisController extends ControllerBase{
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private AdminSSEManagerService adminSSEManagerService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取当前已连接SSE的管理端用户列表
|
|
||||||
* @return 当前已连接SSE的管理端用户列表
|
|
||||||
*/
|
|
||||||
@GetMapping("getSSEConnects")
|
|
||||||
public Collection<Integer> getSSEConnects(){
|
|
||||||
return adminSSEManagerService.getUserIds();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
package com.nflg.mobilebroken.cfs.controller;
|
|
||||||
|
|
||||||
import com.nflg.mobilebroken.starter.service.impl.AdminSSEManagerService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sse相关接口
|
|
||||||
* 曹鹏飞
|
|
||||||
*/
|
|
||||||
@RestController
|
|
||||||
@Slf4j
|
|
||||||
@RequestMapping("/sse")
|
|
||||||
//@SaUserCheckLogin
|
|
||||||
public class SSEController extends ControllerBase {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private AdminSSEManagerService adminSSEManagerService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 建立sse连接
|
|
||||||
*
|
|
||||||
* @param userId 用户id
|
|
||||||
*/
|
|
||||||
@GetMapping(value = "connect",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
|
||||||
public SseEmitter connect(@RequestParam Integer userId) {
|
|
||||||
return adminSSEManagerService.connect(userId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -5,7 +5,6 @@ import com.nflg.mobilebroken.common.pojo.ApiResult;
|
||||||
import com.nflg.mobilebroken.common.util.MultilingualUtil;
|
import com.nflg.mobilebroken.common.util.MultilingualUtil;
|
||||||
import com.nflg.mobilebroken.repository.entity.Ticket;
|
import com.nflg.mobilebroken.repository.entity.Ticket;
|
||||||
import com.nflg.mobilebroken.repository.service.ITicketService;
|
import com.nflg.mobilebroken.repository.service.ITicketService;
|
||||||
import com.nflg.mobilebroken.starter.service.impl.APPSSEManagerService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
@ -18,24 +17,12 @@ import javax.annotation.Resource;
|
||||||
@RequestMapping("/test")
|
@RequestMapping("/test")
|
||||||
public class TestController extends ControllerBase {
|
public class TestController extends ControllerBase {
|
||||||
|
|
||||||
@Resource
|
|
||||||
private APPSSEManagerService sseManagerService;
|
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private TicketEventPublisher ticketEventPublisher;
|
private TicketEventPublisher ticketEventPublisher;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private ITicketService ticketService;
|
private ITicketService ticketService;
|
||||||
|
|
||||||
// @GetMapping("sse/send")
|
|
||||||
// public ApiResult<Void> sendSse(@RequestParam String userId, @RequestParam String message) throws IOException {
|
|
||||||
// SSEMessageDTO messageDTO = new SSEMessageDTO()
|
|
||||||
// .setType(2)
|
|
||||||
// .setData(new NotifyDTO().setSubject("消息测试").setContent("消息内容"));
|
|
||||||
// sseManagerService.send(Integer.valueOf(userId), messageDTO);
|
|
||||||
// return ApiResult.success();
|
|
||||||
// }
|
|
||||||
|
|
||||||
@GetMapping("sss")
|
@GetMapping("sss")
|
||||||
public ApiResult sss(){
|
public ApiResult sss(){
|
||||||
Ticket ticket =ticketService.getById(8);
|
Ticket ticket =ticketService.getById(8);
|
||||||
|
|
|
||||||
|
|
@ -1,79 +0,0 @@
|
||||||
package com.nflg.mobilebroken.starter.service;
|
|
||||||
|
|
||||||
import com.nflg.mobilebroken.common.constant.STATE;
|
|
||||||
import com.nflg.mobilebroken.common.util.VUtils;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
public class SSEManagerBase {
|
|
||||||
|
|
||||||
private static boolean IS_SHUTDOWN = false;
|
|
||||||
|
|
||||||
protected void check(){
|
|
||||||
VUtils.trueThrow(IS_SHUTDOWN).throwMessage(STATE.ServiceConnectRefused,"SSE服务已关闭");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static void shutdown(Map<Integer, SseEmitter> emitters) {
|
|
||||||
IS_SHUTDOWN=true;
|
|
||||||
log.warn("准备关闭SSE服务");
|
|
||||||
emitters.forEach((k,v)->{
|
|
||||||
try {
|
|
||||||
v.send("因SSE服务关闭,连接即将断开");
|
|
||||||
}catch (Exception ex){
|
|
||||||
log.error("SSE发送消息失败:"+k,ex);
|
|
||||||
}
|
|
||||||
v.complete();
|
|
||||||
});
|
|
||||||
log.warn("SSE服务已关闭");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static void close(SseEmitter emitter){
|
|
||||||
emitter.complete();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected SseEmitter connect(Integer userId, Map<Integer, SseEmitter> emitters) {
|
|
||||||
SseEmitter emitter = new SseEmitter(Long.MAX_VALUE);
|
|
||||||
SseEmitter old=emitters.put(userId, emitter);
|
|
||||||
if (Objects.nonNull(old)){
|
|
||||||
log.warn("停止旧连接:"+userId);
|
|
||||||
try {
|
|
||||||
old.send(SseEmitter.event().name("被踢下线").data("你已在其他地方连接"));
|
|
||||||
old.complete();
|
|
||||||
} catch (Exception e) {
|
|
||||||
old.completeWithError(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
emitter.onError((ex) -> {
|
|
||||||
emitters.remove(userId);
|
|
||||||
emitter.complete();
|
|
||||||
log.error("SSE异常:"+userId, ex);
|
|
||||||
});
|
|
||||||
emitter.onTimeout(() -> {
|
|
||||||
emitters.remove(userId);
|
|
||||||
emitter.complete();
|
|
||||||
log.error("SSE超时:"+userId);
|
|
||||||
});
|
|
||||||
emitter.onCompletion(() -> {
|
|
||||||
emitters.remove(userId);
|
|
||||||
emitter.complete();
|
|
||||||
log.error("SSE完成:"+userId);
|
|
||||||
});
|
|
||||||
try {
|
|
||||||
emitter.send(SseEmitter.event().data("已连接").reconnectTime(5000));
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("sse发送数据出错", e);
|
|
||||||
}
|
|
||||||
return emitter;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void send(Object message, SseEmitter emitter) throws IOException {
|
|
||||||
VUtils.trueThrowBusinessError(Objects.isNull(emitter)).throwMessage("没有找到sse");
|
|
||||||
log.error("没有找到sse: ");
|
|
||||||
emitter.send(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
package com.nflg.mobilebroken.starter.service;
|
|
||||||
|
|
||||||
import com.nflg.mobilebroken.common.pojo.dto.SSEMessageDTO;
|
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
public interface SSEManagerService {
|
|
||||||
|
|
||||||
SseEmitter connect(Integer userId);
|
|
||||||
|
|
||||||
void send(Integer userId, SSEMessageDTO message) throws IOException;
|
|
||||||
|
|
||||||
void close(Integer userId);
|
|
||||||
|
|
||||||
void shutdown();
|
|
||||||
|
|
||||||
Collection<Integer> getUserIds();
|
|
||||||
}
|
|
||||||
|
|
@ -1,70 +0,0 @@
|
||||||
package com.nflg.mobilebroken.starter.service.impl;
|
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import com.nflg.mobilebroken.common.pojo.dto.SSEMessageDTO;
|
|
||||||
import com.nflg.mobilebroken.starter.service.SSEManagerBase;
|
|
||||||
import com.nflg.mobilebroken.starter.service.SSEManagerService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
||||||
|
|
||||||
import javax.annotation.PreDestroy;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
@Slf4j
|
|
||||||
public class APPSSEManagerService extends SSEManagerBase implements SSEManagerService {
|
|
||||||
|
|
||||||
public static final Map<Integer, SseEmitter> EMITTERS = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SseEmitter connect(Integer userId) {
|
|
||||||
check();
|
|
||||||
log.info("APP端SSE已连接:"+userId);
|
|
||||||
return connect(userId,EMITTERS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void send(Integer userId, SSEMessageDTO message) throws IOException {
|
|
||||||
log.info(StrUtil.format("APP端SSE发送消息,用户id: {},内容: {}", userId, message));
|
|
||||||
SseEmitter emitter = EMITTERS.get(userId);
|
|
||||||
if (Objects.isNull(emitter)) {
|
|
||||||
log.error("用户未连接SSE: " + userId);
|
|
||||||
} else {
|
|
||||||
send(message, emitter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close(Integer userId) {
|
|
||||||
log.info("APP端SSE连接主动关闭:"+userId);
|
|
||||||
close(EMITTERS.remove(userId));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void shutdown() {
|
|
||||||
shutdown(EMITTERS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Collection<Integer> getUserIds() {
|
|
||||||
return EMITTERS.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
@PreDestroy
|
|
||||||
public void cleanup() {
|
|
||||||
log.info("释放SSE连接");
|
|
||||||
for (SseEmitter emitter : EMITTERS.values()) {
|
|
||||||
try {
|
|
||||||
emitter.complete();
|
|
||||||
} catch (Exception e) {
|
|
||||||
emitter.completeWithError(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EMITTERS.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,70 +0,0 @@
|
||||||
package com.nflg.mobilebroken.starter.service.impl;
|
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import com.nflg.mobilebroken.common.pojo.dto.SSEMessageDTO;
|
|
||||||
import com.nflg.mobilebroken.starter.service.SSEManagerBase;
|
|
||||||
import com.nflg.mobilebroken.starter.service.SSEManagerService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
||||||
|
|
||||||
import javax.annotation.PreDestroy;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
@Slf4j
|
|
||||||
public class AdminSSEManagerService extends SSEManagerBase implements SSEManagerService {
|
|
||||||
|
|
||||||
protected static final Map<Integer, SseEmitter> EMITTERS = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SseEmitter connect(Integer userId) {
|
|
||||||
check();
|
|
||||||
log.info("管理端SSE已连接:"+userId);
|
|
||||||
return connect(userId,EMITTERS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void send(Integer userId, SSEMessageDTO message) throws IOException {
|
|
||||||
log.info(StrUtil.format("管理端SSE发送消息,用户id: {},内容: {}", userId, message));
|
|
||||||
SseEmitter emitter = EMITTERS.get(userId);
|
|
||||||
if (Objects.isNull(emitter)) {
|
|
||||||
log.error("用户未连接SSE: " + userId);
|
|
||||||
} else {
|
|
||||||
send(message, emitter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close(Integer userId) {
|
|
||||||
close(EMITTERS.remove(userId));
|
|
||||||
log.info("管理端SSE连接主动关闭:"+userId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void shutdown() {
|
|
||||||
shutdown(EMITTERS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Collection<Integer> getUserIds() {
|
|
||||||
return EMITTERS.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
@PreDestroy
|
|
||||||
public void cleanup() {
|
|
||||||
log.info("释放SSE连接");
|
|
||||||
for (SseEmitter emitter : EMITTERS.values()) {
|
|
||||||
try {
|
|
||||||
emitter.complete();
|
|
||||||
} catch (Exception e) {
|
|
||||||
emitter.completeWithError(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EMITTERS.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue