PropertyServiceKafka.java 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.java110.property.kafka;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.java110.property.smo.IPropertyServiceSMO;
  4. import com.java110.common.constant.KafkaConstant;
  5. import com.java110.common.constant.ResponseConstant;
  6. import com.java110.common.constant.StatusConstant;
  7. import com.java110.common.exception.InitConfigDataException;
  8. import com.java110.common.exception.InitDataFlowContextException;
  9. import com.java110.common.kafka.KafkaFactory;
  10. import com.java110.core.base.controller.BaseController;
  11. import com.java110.core.context.BusinessServiceDataFlow;
  12. import com.java110.core.factory.DataTransactionFactory;
  13. import org.apache.kafka.clients.consumer.ConsumerRecord;
  14. import org.slf4j.Logger;
  15. import org.slf4j.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 PropertyServiceKafka extends BaseController {
  25. private final static Logger logger = LoggerFactory.getLogger(PropertyServiceKafka.class);
  26. @Autowired
  27. private IPropertyServiceSMO propertyServiceSMOImpl;
  28. @KafkaListener(topics = {"propertyServiceTopic"})
  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 = propertyServiceSMOImpl.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 IPropertyServiceSMO getPropertyServiceSMOImpl() {
  76. return propertyServiceSMOImpl;
  77. }
  78. public void setPropertyServiceSMOImpl(IPropertyServiceSMO propertyServiceSMOImpl) {
  79. this.propertyServiceSMOImpl = propertyServiceSMOImpl;
  80. }
  81. }