/// <summary> /// 处理数据传给DB /// 将消息一拆为n个消息 /// </summary> /// <param name="model"></param> private void HandleMsgToDB(object model) { //var _logger = _loggerFactory.CreateLogger<MsgService>(); using (var scope = SherlockEngine.Current.CreateScope()) { try { string serialNO = Guid.NewGuid().ToString();//同一条消息进行拆解 SendMsgRequest sendMsgRequest = (SendMsgRequest)model; //将推送消息推送消息中心 PushMessageToMessageCenter(sendMsgRequest); //拆分数据 //获取设备和通道 IPushTokenBrandService pushTokenBrandService = scope.ServiceProvider.GetService(typeof(IPushTokenBrandService)) as IPushTokenBrandService; List <DeviceChannelDomainModel> deviceChannelDomainModelList = pushTokenBrandService.GetDeviceChannelListAsync(sendMsgRequest.Token, sendMsgRequest.AppId).Result; List <DeviceChannelDto> deviceChannelList = _mapper.Map <List <DeviceChannelDto> >(deviceChannelDomainModelList); //未获取到,接收的消息直接进历史表,标记为账号登出 if (deviceChannelList == null || deviceChannelList.Count == 0) { //直接入历史表,标记错误 SendProcessDto sendProcessDto = SendMsgToSendProcess(sendMsgRequest, serialNO); sendProcessDto.Token = sendMsgRequest.Token; ISendProcessLogic sendProcessLogic = scope.ServiceProvider.GetService(typeof(ISendProcessLogic)) as ISendProcessLogic; sendProcessLogic.RecordErrorProcess(sendProcessDto, ErrorTypeEnum.Logout, string.Empty); return; } //获取到,根据设备Token拆分数据 List <SendProcessDto> sendProcessDtoList = new List <SendProcessDto>(); foreach (var item in deviceChannelList) { SendProcessDto sendProcessDto = SendMsgToSendProcess(sendMsgRequest, serialNO); sendProcessDto.Token = sendMsgRequest.Token; //将获取到的TokenBrandId(核心),BrandId,ChannelId,DeviceToken 赋给对象,可做到新老接口兼容 //老接口发送数据时,可根据TokenBrandId直接找到需要发送的ChannelId和DeviceToken //新接口发送数据时,通过ChannelId,DeviceToken 发送数据 sendProcessDto.TokenBrandId = item.Id; sendProcessDto.BrandId = item.BrandId; sendProcessDto.ChannelId = item.ChannelId; sendProcessDto.DeviceToken = item.DeviceToken; sendProcessDtoList.Add(sendProcessDto); } IMapper mapper = scope.ServiceProvider.GetService(typeof(IMapper)) as IMapper; List <PushSendProcessDomainModel> domainList = mapper.Map <List <PushSendProcessDomainModel> >(sendProcessDtoList); IPushSendProcessService pushSendProcessService = scope.ServiceProvider.GetService(typeof(IPushSendProcessService)) as IPushSendProcessService; int rows = pushSendProcessService.InsertProcessListAsync(domainList).Result; } catch (Exception ex) { //_logger.LogError(ex.Message.ToString()); //LogHelper.Error.Write("HandleMsgToDB", ex); } finally { _semaphore.Release(); //LogHelper.Info.Write("HandleMsgToDB", "信号量释放之前使用的数量:" + count); } } }
public SendHandler(IMapper mapper , IPushTokenBrandService pushTokenBrandService , ISendProcessLogic sendProcessLogic , IPushSendProcessService pushSendProcessService ) { _mapper = mapper; _pushTokenBrandService = pushTokenBrandService; _sendProcessLogic = sendProcessLogic; _pushSendProcessService = pushSendProcessService; }
/// <summary> /// 将消息发送给所有用户 /// </summary> /// <param name="o"></param> private async void SendAllAsync(object o) { using (var scope = SherlockEngine.Current.CreateScope()) { ILoggerFactory loggerFactory = scope.ServiceProvider.GetService(typeof(ILoggerFactory)) as ILoggerFactory; var logger = loggerFactory.CreateLogger <AcceptMsgController>(); SendMsgDto dto = o as SendMsgDto; //获取App下所有的用户 IPushTokenBrandService pushTokenBrandService = scope.ServiceProvider.GetService(typeof(IPushTokenBrandService)) as IPushTokenBrandService; //IPushTokenBrandService pushTokenBrandService = SherlockEngine.Current.GetService<IPushTokenBrandService>();//这样写不会Dispose资源 var allTokenList = await pushTokenBrandService.GetAllTokenBrand(dto.AppId); if (allTokenList == null || allTokenList.Count == 0) { return; } IMapper mapper = scope.ServiceProvider.GetService(typeof(IMapper)) as IMapper; var req = mapper.Map <SendMsgListRequest>(dto); req.TokenList = allTokenList; string retMsg = string.Empty; try { //将消息拆解存到消息队列中 IMsgService msgService = scope.ServiceProvider.GetService(typeof(IMsgService)) as IMsgService; //IMsgService msgService = SherlockEngine.Current.GetService<IMsgService>(); var ret = msgService.AcceptAllMsgList(req, out retMsg); if (ret == ErrCode.Sucess) { logger.LogInformation("推送全体成功"); } else { logger.LogInformation("推送全体失败:" + retMsg); } } catch (Exception ex) { logger.LogInformation("推送全体失败:" + ex.Message.ToString()); } } }
//private ILoggerFactory _loggerFactory; public MsgService(IConfigLogic configLogic , IMapper mapper , ITokenBrandLogic tokenBrandLogic , IPushTokenBrandService pushTokenBrandService , ISendProcessLogic sendProcessLogic , IPushSendProcessService pushSendProcessService , IEventNotification eventNotification //, ILoggerFactory loggerFactory ) { _configLogic = configLogic; _mapper = mapper; _tokenBrandLogic = tokenBrandLogic; _pushTokenBrandService = pushTokenBrandService; _sendProcessLogic = sendProcessLogic; _pushSendProcessService = pushSendProcessService; _eventNotification = eventNotification; //_loggerFactory = loggerFactory; //初始化信号量 InitSemaphore(); //初始化队列 InitQueue(); }
public RegisterService(ITokenBrandLogic tokenBrandLogic, IPushTokenBrandService pushTokenBrandService, IMapper mapper) { _tokenBrandLogic = tokenBrandLogic; _pushTokenBrandService = pushTokenBrandService; _mapper = mapper; }
public TokenBrandLogic(IPushTokenBrandService pushTokenBrandService, IInfraDicService infraDicService, IMapper mapper) { _pushTokenBrandService = pushTokenBrandService; _infraDicService = infraDicService; _mapper = mapper; }