CommunityServiceKafka.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.java110.community.kafka;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.java110.utils.constant.KafkaConstant;
  4. import com.java110.utils.constant.ResponseConstant;
  5. import com.java110.utils.constant.StatusConstant;
  6. import com.java110.utils.exception.InitConfigDataException;
  7. import com.java110.utils.exception.InitDataFlowContextException;
  8. import com.java110.utils.kafka.KafkaFactory;
  9. import com.java110.core.base.controller.BaseController;
  10. import com.java110.core.context.BusinessServiceDataFlow;
  11. import com.java110.core.factory.DataTransactionFactory;
  12. import com.java110.community.smo.ICommunityServiceSMO;
  13. import org.apache.kafka.clients.consumer.ConsumerRecord;
  14. import org.slf4j.Logger;
  15. import com.java110.core.log.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.kafka.annotation.KafkaListener;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. /**
  21. * kafka侦听
  22. * Created by wuxw on 2018/4/15.
  23. */
  24. public class CommunityServiceKafka extends BaseController {
  25. private final static Logger logger = LoggerFactory.getLogger(CommunityServiceKafka.class);
  26. @Autowired
  27. private ICommunityServiceSMO communityServiceSMOImpl;
  28. @KafkaListener(topics = {"communityServiceTopic"})
  29. public void listen(ConsumerRecord<?, ?> record) {
  30. logger.info("kafka的key: " + record.key());
  31. logger.info("kafka的value: " + record.value().toString());
  32. String orderInfo = record.value().toString();
  33. BusinessServiceDataFlow businessServiceDataFlow = null;
  34. JSONObject responseJson = null;
  35. try {
  36. Map<String, String> headers = new HashMap<String, String>();
  37. //预校验
  38. preValiateOrderInfo(orderInfo);
  39. businessServiceDataFlow = this.writeDataToDataFlowContext(orderInfo, headers);
  40. responseJson = communityServiceSMOImpl.service(businessServiceDataFlow);
  41. }catch (InitDataFlowContextException e){
  42. logger.error("请求报文错误,初始化 BusinessServiceDataFlow失败"+orderInfo,e);
  43. responseJson = DataTransactionFactory.createNoBusinessTypeBusinessResponseJson(orderInfo,ResponseConstant.RESULT_PARAM_ERROR,e.getMessage(),null);
  44. }catch (InitConfigDataException e){
  45. logger.error("请求报文错误,加载配置信息失败"+orderInfo,e);
  46. responseJson = DataTransactionFactory.createNoBusinessTypeBusinessResponseJson(orderInfo,ResponseConstant.RESULT_PARAM_ERROR,e.getMessage(),null);
  47. }catch (Exception e){
  48. logger.error("请求订单异常",e);
  49. responseJson = DataTransactionFactory.createBusinessResponseJson(businessServiceDataFlow,ResponseConstant.RESULT_CODE_ERROR,e.getMessage()+e,
  50. null);
  51. }finally {
  52. logger.debug("当前请求报文:" + orderInfo +", 当前返回报文:" +responseJson.toJSONString());
  53. //只有business 和 instance 过程才做通知消息
  54. if(!StatusConstant.REQUEST_BUSINESS_TYPE_BUSINESS.equals(responseJson.getString("businessType"))
  55. && !StatusConstant.REQUEST_BUSINESS_TYPE_INSTANCE.equals(responseJson.getString("businessType"))){
  56. return ;
  57. }
  58. try {
  59. KafkaFactory.sendKafkaMessage(KafkaConstant.TOPIC_NOTIFY_CENTER_SERVICE_NAME, "", responseJson.toJSONString());
  60. }catch (Exception e){
  61. logger.error("用户服务通知centerService失败"+responseJson,e);
  62. //这里保存异常信息
  63. }
  64. }
  65. }
  66. /**
  67. * 这里预校验,请求报文中不能有 dataFlowId
  68. * @param orderInfo
  69. */
  70. private void preValiateOrderInfo(String orderInfo) {
  71. /* if(JSONObject.parseObject(orderInfo).getJSONObject("orders").containsKey("dataFlowId")){
  72. throw new BusinessException(ResponseConstant.RESULT_CODE_ERROR,"报文中不能存在dataFlowId节点");
  73. }*/
  74. }
  75. public ICommunityServiceSMO getCommunityServiceSMOImpl() {
  76. return communityServiceSMOImpl;
  77. }
  78. public void setCommunityServiceSMOImpl(ICommunityServiceSMO communityServiceSMOImpl) {
  79. this.communityServiceSMOImpl = communityServiceSMOImpl;
  80. }
  81. }