Authored by qinchao

百度云ocr开关消费

1 package com.yohoufo.user.component; 1 package com.yohoufo.user.component;
2 2
  3 +import com.google.common.cache.Cache;
  4 +import com.google.common.cache.CacheBuilder;
3 import com.yoho.core.cache.LocalCache; 5 import com.yoho.core.cache.LocalCache;
4 import com.yoho.core.config.ConfigReader; 6 import com.yoho.core.config.ConfigReader;
  7 +import com.yoho.core.config.ZKConfigWriter;
5 import org.slf4j.Logger; 8 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 9 import org.slf4j.LoggerFactory;
7 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Component; 11 import org.springframework.stereotype.Component;
9 12
10 import javax.annotation.PostConstruct; 13 import javax.annotation.PostConstruct;
  14 +import java.util.concurrent.Callable;
  15 +import java.util.concurrent.ExecutionException;
11 import java.util.concurrent.TimeUnit; 16 import java.util.concurrent.TimeUnit;
12 17
13 /** 18 /**
@@ -20,25 +25,32 @@ public class CertPhotoSwitchComponent { @@ -20,25 +25,32 @@ public class CertPhotoSwitchComponent {
20 @Autowired 25 @Autowired
21 private ConfigReader configReader; 26 private ConfigReader configReader;
22 27
  28 + @Autowired
  29 + private ZKConfigWriter zkConfigWriter;
  30 +
23 //缓存 31 //缓存
24 - private LocalCache localCache_switch = new LocalCache();  
25 - private static final String SWITCH_CACHE_KEY = "certPhotoSwitchCacheKey"; 32 + private Cache<String,Boolean> localCache = CacheBuilder.newBuilder().maximumSize(10).expireAfterWrite(2, TimeUnit.MINUTES).build();
  33 + private final String SWITCH_CACHE_KEY = "certPhotoSwitchCacheKey";
  34 + private final String zkValue = "ufo.order.certPhotoSwitchCacheKey";
26 private Boolean defaultSwitch = Boolean.TRUE; 35 private Boolean defaultSwitch = Boolean.TRUE;
27 36
28 -  
29 - @PostConstruct  
30 - private void init() {  
31 - localCache_switch.init(SWITCH_CACHE_KEY, 1, TimeUnit.MINUTES, (String s, Object o) -> {  
32 - logger.info("StoreSellerServiceImpl init certPhotoSwitchCacheKey s = {}, o = {}", s, o);  
33 - return configReader.getBoolean("ufo.order.certPhotoSwitchCacheKey",defaultSwitch);  
34 - });  
35 - }  
36 -  
37 public boolean getCertPhotoSwitch(){ 37 public boolean getCertPhotoSwitch(){
38 - Object value=localCache_switch.get(SWITCH_CACHE_KEY);  
39 - if(null == value) { 38 + try{
  39 + return localCache.get(SWITCH_CACHE_KEY, new Callable<Boolean>() {
  40 + @Override
  41 + public Boolean call() throws Exception {
  42 + return configReader.getBoolean(zkValue,defaultSwitch);
  43 + }
  44 + });
  45 + }catch (ExecutionException e){
  46 + logger.warn("StoreSellerServiceImpl getCertPhotoSwitch error ", e);
40 return defaultSwitch; 47 return defaultSwitch;
41 } 48 }
42 - return (Boolean) value; 49 + }
  50 +
  51 + public void writeZkValueToFalse(){
  52 + logger.info("StoreSellerServiceImpl writeZkValueToFalse");
  53 + zkConfigWriter.setConfig(zkValue,"false");
  54 + localCache.invalidate(SWITCH_CACHE_KEY);
43 } 55 }
44 } 56 }
  1 +
  2 +package com.yohoufo.user.consumer;
  3 +
  4 +import com.yoho.core.rabbitmq.YhConsumer;
  5 +import com.yohoufo.user.component.CertPhotoSwitchComponent;
  6 +import org.slf4j.Logger;
  7 +import org.slf4j.LoggerFactory;
  8 +import org.springframework.beans.factory.annotation.Autowired;
  9 +import org.springframework.stereotype.Component;
  10 +
  11 +
  12 +@Component
  13 +public class OrcSwitchUpdateConsumer implements YhConsumer {
  14 + private static final Logger LOGGER = LoggerFactory.getLogger(CertPhotoSwitchComponent.class);
  15 + private final String topic = "reviewed.ocr.limit";
  16 + @Autowired
  17 + private CertPhotoSwitchComponent certPhotoSwitchComponent;
  18 +
  19 + public String getMessageTopic() {
  20 + return topic;
  21 + }
  22 +
  23 + @Override
  24 + public void handleMessage(Object message) {
  25 + try {
  26 + LOGGER.info("begin handle express info update message, message is {}. topic {}", message,getMessageTopic());
  27 + certPhotoSwitchComponent.writeZkValueToFalse();
  28 + LOGGER.info("handle express info update message success, message is {}.", message);
  29 + } catch (Exception e) {
  30 + LOGGER.warn("handle express info update message fail, message is {}.", message);
  31 + LOGGER.error("handleMessage error = ", e);
  32 + }
  33 + }
  34 +}
@@ -116,7 +116,7 @@ order.seller.tip.recoverTip=\u5145\u503C \uFFE5{} \u6062\u590D\u8D85\u7EA7\u5546 @@ -116,7 +116,7 @@ order.seller.tip.recoverTip=\u5145\u503C \uFFE5{} \u6062\u590D\u8D85\u7EA7\u5546
116 116
117 uic.url=http://java-yoho-uic.test3.ingress.dev.yohocorp.com/uic 117 uic.url=http://java-yoho-uic.test3.ingress.dev.yohocorp.com/uic
118 yoho.message.controller.url=http://message-controller.yohoops.org/yoho-message-controller 118 yoho.message.controller.url=http://message-controller.yohoops.org/yoho-message-controller
119 -yoho.reviewed.controller.url=http://localhost:8063/reviewed 119 +yoho.reviewed.controller.url=http://172.16.6.54:8063/reviewed
120 120
121 #rabbit address for transaction compensate 121 #rabbit address for transaction compensate
122 rabbit_host=192.168.104.199:30005 122 rabbit_host=192.168.104.199:30005
@@ -68,6 +68,14 @@ consumer: @@ -68,6 +68,14 @@ consumer:
68 - class: com.yohoufo.order.mq.consumer.ExpressInfoUpdateConsumer 68 - class: com.yohoufo.order.mq.consumer.ExpressInfoUpdateConsumer
69 topic: ufo.order.updateExpressInfo 69 topic: ufo.order.updateExpressInfo
70 70
  71 + #ocr百度云开关
  72 + - address: 192.168.102.45:5672
  73 + username: yoho
  74 + password: yoho
  75 + consumers:
  76 + - class: com.yohoufo.user.consumer.OrcSwitchUpdateConsumer
  77 + topic: reviewed.ocr.limit
  78 +
71 - address: 192.168.102.45:5672 79 - address: 192.168.102.45:5672
72 username: yoho 80 username: yoho
73 password: yoho 81 password: yoho
@@ -72,6 +72,14 @@ consumer: @@ -72,6 +72,14 @@ consumer:
72 - class: com.yohoufo.order.mq.consumer.ExpressInfoUpdateConsumer 72 - class: com.yohoufo.order.mq.consumer.ExpressInfoUpdateConsumer
73 topic: ufo.order.updateExpressInfo 73 topic: ufo.order.updateExpressInfo
74 74
  75 + #ocr百度云开关
  76 + - address: ${rabbit_common}
  77 + username: ${rabbit_common_user}
  78 + password: ${rabbit_common_password}
  79 + consumers:
  80 + - class: com.yohoufo.user.consumer.OrcSwitchUpdateConsumer
  81 + topic: reviewed.ocr.limit
  82 +
75 - address: ${rabbit_ufo} 83 - address: ${rabbit_ufo}
76 username: ${rabbit_ufo_user} 84 username: ${rabbit_ufo_user}
77 password: ${rabbit_ufo_password} 85 password: ${rabbit_ufo_password}