Example #1
0
        public System.Threading.Tasks.Task Execute(IJobExecutionContext context)
        {
            var customerInfoCaches = cache.HashGetAll <CustomerInfoCache>(ConstCacheKey.GetWxIdKey());

            foreach (var item in customerInfoCaches)
            {
                try
                {
                    if (LogoutWxIds.Contains(item.WxId))
                    {
                        continue;
                    }

                    var result = wechatHelper.SyncInit(item.WxId);

                    if (result.AddMsgs != null && result.AddMsgs.Count > 0)
                    {
                        SyncMessageResult syncMessageResult = new SyncMessageResult();
                        syncMessageResult.WxId = item.WxId;
                        syncMessageResult.Data = result;
                        var     resultJson = syncMessageResult.ToJson();
                        var     buffer     = Encoding.UTF8.GetBytes(resultJson);
                        Message message    = new Message(MqConst.SyncMessageTopic, buffer);
                        var     sendResult = producer.SendMessage(message);
                    }
                }
                catch (ExpiredException ex)
                {
                    lock (lockObj)
                    {
                        if (!LogoutWxIds.Contains(item.WxId))
                        {
                            LogoutWxIds.Add(item.WxId);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Util.Log.Logger.GetLog <SyncWechatMsessageJob>().Error(ex);
                }
            }

            return(System.Threading.Tasks.Task.CompletedTask);
        }
Example #2
0
        public ConsumeConcurrentlyStatus consumeMessage(List list, ConsumeConcurrentlyContext ccc)
        {
            Iterator iterator = list.iterator();

            while (iterator.hasNext())
            {
                string wxId   = null;
                byte[] buffer = null;
                try
                {
                    var messageClientExt = iterator.next() as org.apache.rocketmq.common.message.MessageClientExt;
                    buffer = messageClientExt.getBody();
                    wxId   = Encoding.UTF8.GetString(buffer);

                    var result = wechatHelper.SyncInit(wxId);

                    if (result.ModUserInfos?.Count > 0 || result.AddMsgs?.Count > 0 || result.DelContacts?.Count > 0 || result.AddMsgs?.Count > 0)
                    {
                        SyncMessageResult syncMessageResult = new SyncMessageResult();
                        syncMessageResult.WxId = wxId;
                        syncMessageResult.Data = result;
                        var     resultJson = syncMessageResult.ToJson();
                        var     dataBuffer = Encoding.UTF8.GetBytes(resultJson);
                        Message message    = new Message(MqConst.SyncMessageTopic, dataBuffer);
                        var     sendResult = producer.SendMessage(message);
                    }
                    OfflineStatus offlineStatus = new OfflineStatus()
                    {
                        WxId   = wxId,
                        Status = 0
                    };
                    offlineproducer.SendMessage(new Message(MqConst.UserOfflineStatusTopic, Encoding.UTF8.GetBytes(offlineStatus.ToJson())));

                    var userMessage = new Message(MqConst.UserSyncMessageTopic, buffer);
                    userMessage.setDelayTimeLevel(2);           //2表示5秒
                    userproducer.SendMessage(userMessage);
                    Util.Log.Logger.GetLog <SyncMessageListener>().Info(wxId);
                }
                catch (ExpiredException ex)
                {
                    if (!string.IsNullOrEmpty(wxId))
                    {
                        OfflineStatus offlineStatus = new OfflineStatus()
                        {
                            WxId   = wxId,
                            Status = 2
                        };
                        offlineproducer.SendMessage(new Message(MqConst.UserOfflineStatusTopic, Encoding.UTF8.GetBytes(offlineStatus.ToJson())));
                    }
                    if (Dic.ContainsKey(wxId))
                    {
                        if (Dic[wxId] < maxCount)
                        {
                            Dic[wxId]++;
                            userproducer.SendMessage(new Message(MqConst.UserSyncMessageTopic, buffer));
                        }
                        else
                        {
                            Dic[wxId] = 0;
                        }
                    }
                    else
                    {
                        Dic.TryAdd(wxId, 1);
                        userproducer.SendMessage(new Message(MqConst.UserSyncMessageTopic, buffer));
                    }

                    Util.Log.Logger.GetLog <SyncMessageListener>().Error($"{wxId}---重试次数:{Dic[wxId]}\r\n", ex);
                }
                catch (Exception ex)
                {
                    userproducer.SendMessage(new Message(MqConst.UserSyncMessageTopic, buffer));
                    Util.Log.Logger.GetLog <SyncMessageListener>().Error(wxId, ex);
                }
            }
            return(ConsumeConcurrentlyStatus.CONSUME_SUCCESS);
        }