Authored by qinchao

百度云ocr开关消费

package com.yohoufo.user.component;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.yoho.core.cache.LocalCache;
import com.yoho.core.config.ConfigReader;
import com.yoho.core.config.ZKConfigWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
... ... @@ -20,25 +25,32 @@ public class CertPhotoSwitchComponent {
@Autowired
private ConfigReader configReader;
@Autowired
private ZKConfigWriter zkConfigWriter;
//缓存
private LocalCache localCache_switch = new LocalCache();
private static final String SWITCH_CACHE_KEY = "certPhotoSwitchCacheKey";
private Cache<String,Boolean> localCache = CacheBuilder.newBuilder().maximumSize(10).expireAfterWrite(2, TimeUnit.MINUTES).build();
private final String SWITCH_CACHE_KEY = "certPhotoSwitchCacheKey";
private final String zkValue = "ufo.order.certPhotoSwitchCacheKey";
private Boolean defaultSwitch = Boolean.TRUE;
@PostConstruct
private void init() {
localCache_switch.init(SWITCH_CACHE_KEY, 1, TimeUnit.MINUTES, (String s, Object o) -> {
logger.info("StoreSellerServiceImpl init certPhotoSwitchCacheKey s = {}, o = {}", s, o);
return configReader.getBoolean("ufo.order.certPhotoSwitchCacheKey",defaultSwitch);
});
}
public boolean getCertPhotoSwitch(){
Object value=localCache_switch.get(SWITCH_CACHE_KEY);
if(null == value) {
try{
return localCache.get(SWITCH_CACHE_KEY, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return configReader.getBoolean(zkValue,defaultSwitch);
}
});
}catch (ExecutionException e){
logger.warn("StoreSellerServiceImpl getCertPhotoSwitch error ", e);
return defaultSwitch;
}
return (Boolean) value;
}
public void writeZkValueToFalse(){
logger.info("StoreSellerServiceImpl writeZkValueToFalse");
zkConfigWriter.setConfig(zkValue,"false");
localCache.invalidate(SWITCH_CACHE_KEY);
}
}
... ...
package com.yohoufo.user.consumer;
import com.yoho.core.rabbitmq.YhConsumer;
import com.yohoufo.user.component.CertPhotoSwitchComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class OrcSwitchUpdateConsumer implements YhConsumer {
private static final Logger LOGGER = LoggerFactory.getLogger(CertPhotoSwitchComponent.class);
private final String topic = "reviewed.ocr.limit";
@Autowired
private CertPhotoSwitchComponent certPhotoSwitchComponent;
public String getMessageTopic() {
return topic;
}
@Override
public void handleMessage(Object message) {
try {
LOGGER.info("begin handle express info update message, message is {}. topic {}", message,getMessageTopic());
certPhotoSwitchComponent.writeZkValueToFalse();
LOGGER.info("handle express info update message success, message is {}.", message);
} catch (Exception e) {
LOGGER.warn("handle express info update message fail, message is {}.", message);
LOGGER.error("handleMessage error = ", e);
}
}
}
\ No newline at end of file
... ...
... ... @@ -116,7 +116,7 @@ order.seller.tip.recoverTip=\u5145\u503C \uFFE5{} \u6062\u590D\u8D85\u7EA7\u5546
uic.url=http://java-yoho-uic.test3.ingress.dev.yohocorp.com/uic
yoho.message.controller.url=http://message-controller.yohoops.org/yoho-message-controller
yoho.reviewed.controller.url=http://localhost:8063/reviewed
yoho.reviewed.controller.url=http://172.16.6.54:8063/reviewed
#rabbit address for transaction compensate
rabbit_host=192.168.104.199:30005
... ...
... ... @@ -68,6 +68,14 @@ consumer:
- class: com.yohoufo.order.mq.consumer.ExpressInfoUpdateConsumer
topic: ufo.order.updateExpressInfo
#ocr百度云开关
- address: 192.168.102.45:5672
username: yoho
password: yoho
consumers:
- class: com.yohoufo.user.consumer.OrcSwitchUpdateConsumer
topic: reviewed.ocr.limit
- address: 192.168.102.45:5672
username: yoho
password: yoho
... ...
... ... @@ -72,6 +72,14 @@ consumer:
- class: com.yohoufo.order.mq.consumer.ExpressInfoUpdateConsumer
topic: ufo.order.updateExpressInfo
#ocr百度云开关
- address: ${rabbit_common}
username: ${rabbit_common_user}
password: ${rabbit_common_password}
consumers:
- class: com.yohoufo.user.consumer.OrcSwitchUpdateConsumer
topic: reviewed.ocr.limit
- address: ${rabbit_ufo}
username: ${rabbit_ufo_user}
password: ${rabbit_ufo_password}
... ...