PageRenderTime 157ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/linbox/im/server/router/handlers/PingHandler.java

https://gitlab.com/Mr.Tomato/linbox_server
Java | 59 lines | 45 code | 11 blank | 3 comment | 1 complexity | c8d0c01da2cbb493e85006c4dd000eed MD5 | raw file
  1. package com.linbox.im.server.router.handlers;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.linbox.im.exceptions.IMConsumerException;
  5. import com.linbox.im.message.MessageWrapper;
  6. import com.linbox.im.message.Ping;
  7. import com.linbox.im.message.Pong;
  8. import com.linbox.im.server.service.IOutboxService;
  9. import org.apache.commons.lang.StringUtils;
  10. import org.apache.kafka.clients.consumer.ConsumerRecord;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Qualifier;
  15. import org.springframework.stereotype.Service;
  16. /**
  17. * Created by lrsec on 7/7/15.
  18. */
  19. @Service
  20. @Qualifier("pingHandler")
  21. public class PingHandler implements Handler<String, String> {
  22. private static Logger logger = LoggerFactory.getLogger(PingHandler.class);
  23. @Autowired
  24. private IOutboxService outboxService;
  25. @Override
  26. public void handle(ConsumerRecord<String, String> record) {
  27. String json = record.value();
  28. try {
  29. logger.debug("Start handling Ping: {}", json);
  30. MessageWrapper wrapper = JSON.parseObject(json, MessageWrapper.class);
  31. Ping ping = JSON.parseObject(((JSONObject)wrapper.content).toJSONString(), Ping.class);
  32. wrapper.content = ping;
  33. String userId = ping.userId;
  34. if(StringUtils.isBlank(userId)) {
  35. logger.error("Can not find avaiable user id for PullOldMsgRequest {}", json);
  36. return;
  37. }
  38. sendPong(userId, wrapper);
  39. } catch (Exception e) {
  40. throw new IMConsumerException(e, json);
  41. }
  42. }
  43. private void sendPong(String userId, MessageWrapper wrapper) {
  44. Pong pong = new Pong();
  45. pong.rId = ((Ping)(wrapper.content)).rId;
  46. outboxService.put(userId, pong.toWrapperJson());
  47. }
  48. }