springcloud中servcie层调用fegin异常以及异步方法的实现
近日在做业务上的短信推送和APP消息推送,通过调用别的模块的接口来实现,在springcloud中通过fegin进行调用。这里要说明的事情并不是如何开发推送功能,而是在调试过程中碰到的一些小问题。
我把消息推送之前的业务处理代码以及调用推送服务的代码都放在方法pushByAppAndShortMessage()中,然后把这个方法单独的放在crmservice里面。由于业务处理,pushByAppAndShortMessage中需要用到别的service,就不得不在crmservice中进行大量的autowired。代码如下:
package cn.appliedata.operate.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import cn.appliedata.common.ResponseWrapper;
import cn.appliedata.message.Message;
import cn.appliedata.message.Message.Type;
import cn.appliedata.model.account.UserAccount;
import cn.appliedata.operate.bean.Supplieres;
import cn.appliedata.operate.bean.crmFlow.SaveFeedbackDTO;
import cn.appliedata.operate.enums.SupplierSuggestionEnum;
import cn.appliedata.operate.feignClient.AccountFeignService;
import cn.appliedata.operate.feignClient.MessageFeignService;
import cn.appliedata.operate.feignClient.SmsCodeFeignService;
import cn.appliedata.operate.mapper.crm.PartsDao;
import cn.appliedata.operate.mapper.operate.OperateTaichiDao;
import cn.appliedata.operate.util.ConstantUtil;
import lombok.extern.slf4j.Slf4j;
/**
* @author :ayfei
*/
@Service
@Slf4j
public class CrmService {
@Autowired
private MessageFeignService messageFeignService;
@Autowired
private SmsCodeFeignService smsCodeFeignService;
@Autowired
private AccountFeignService accountFeignService;
@Autowired
private StocksiteService stocksiteService;
@Autowired
private PartsService partsService;
/*
* 消息推送[APP、短信]
*/
@Async
public void pushByAppAndShortMessage(Integer i,Integer senderFlag,SaveFeedbackDTO saveFeedbackDTO){
log.info("log推送1:索引index = "+i);
Message message = new Message(); // 创建APP推送消息体
message.setType(Type.ORDER); // 消息类型不能为空
if(senderFlag == ConstantUtil.INTNUM1){ // 1 服务人员 发送,推送给 供应商
log.info("log推送:推送给供应商");
//有新增、有修改,获取供应商的 accountcode 和 手机号
String type = "";
String feedBackName = "";//异常反馈单号
if(saveFeedbackDTO.getType() == ConstantUtil.INTNUM1){
type = "新增";
message.setContent("您有一条"+type+"异常反馈单待处理,请前往工具->运维反馈进行处理"); //内容不能为空
}else if(saveFeedbackDTO.getType() == ConstantUtil.INTNUM2){
type = "更新";
Map<String,String> serviceWorkerMap = partsService.getServiceWorkerInfo(saveFeedbackDTO.getFeedbackId());
feedBackName = serviceWorkerMap.get("feedBackName"); //当前的异常反馈单号
message.setContent("您有一条"+type+"异常反馈单待处理,异常反馈单号:"+feedBackName+",请前往工具->运维反馈进行处理"); //内容不能为空
}
//根据供应商id获取责任人id和电话
String ownerId = null;
String ownerPhone = null;
List<Supplieres> supplierSelect = stocksiteService.supplierSelect(null, null, saveFeedbackDTO.getSupplierId());
if(supplierSelect == null || supplierSelect.get(0) == null){
log.info("根据供应商id获取责任人信息为空");
return;
}else{
ownerId = supplierSelect.get(0).getOwnerId();
ownerPhone = supplierSelect.get(0).getNewSupplierCall();
}
if(ownerPhone != null){
try {
//短信推送供应商
String shortMessage = "您有一条"+type+"异常反馈单:"+feedBackName+"待处理,请登录APP前往工具->运维反馈进行处理。";
//ResponseWrapper sendArbitrarilyMsg = smsCodeFeignService.sendArbitrarilyMsg(ownerPhone, "", shortMessage);//给服务人员发送短信
ResponseWrapper sendArbitrarilyMsg = smsCodeFeignService.sendArbitrarilyMsg("18337117299", "", shortMessage);//给服务人员发送短信
if(!sendArbitrarilyMsg.isSuccess()){
log.info("异常反馈单号:"+feedBackName+"的消息短信推送供应商失败");//短信发送失败
}
} catch (Exception e) {
log.info("异常反馈单号:"+feedBackName+"的消息短信推送供应商出现异常");
}
try {
//APP推送供应商 获取供应商账号的 accountcode
ResponseWrapper<UserAccount> accountByMobile = accountFeignService.getAccountByMobile(ownerPhone);
if(accountByMobile == null || accountByMobile.getObj() == null){
log.info("获取远程供应商账号信息异常/返回信息为空");
return;
}
message.setTo(accountByMobile.getObj().getAccountCode()); //
message.setSubject(type+"异常反馈单信息"); //标题不能为空
message.setSummary("您有一条"+type+"异常反馈单信息,注意查收");//消息摘要不能为空
ResponseWrapper respon = messageFeignService.addMessage(message); //APP推送给供应商的账号
if(!respon.isSuccess()){
log.info("异常反馈单新增、修改信息,APP推送供应商失败");//APP失败
}
} catch (Exception e) {
log.info("异常反馈单新增、修改信息,APP推送供应商出现异常");
}
}else{
log.info("供应商电话为null!,无法推送");
}
}else if(senderFlag == ConstantUtil.INTNUM2){ //2 供应商 发送, 推送给 服务人员
log.info("log推送:推送给服务人员");
//只有修改功能,获取服务人员的accountcode 和 手机号
String feedbackidStr = saveFeedbackDTO.getFeedbackId();
Map<String,String> serviceWorkerMap = partsService.getServiceWorkerInfo(feedbackidStr);//根据异常反馈单id获取对应服务单对应的服务人员信息
if(serviceWorkerMap == null){
log.info("根据异常反馈单id:"+saveFeedbackDTO.getFeedbackId()+"获取到的服务人员信息对象为null");
return;
}
String serviceWorkerMobile = serviceWorkerMap.get("mobile"); //服务人员电话
String serviceWorkerId = serviceWorkerMap.get("id"); //服务人员id
String serviceWorkerName = serviceWorkerMap.get("name"); //服务人员姓名
String feedBackName = serviceWorkerMap.get("feedBackName"); //当前的异常反馈单号
//获取服务人员账号的 accountcode
try {
ResponseWrapper<UserAccount> accountByMobile = accountFeignService.getAccountByMobile(serviceWorkerMobile);
if(accountByMobile == null || accountByMobile.getObj() == null){
log.info("获取远程供应商账号信息异常/返回信息为空");
return;
}
message.setTo(accountByMobile.getObj().getAccountCode());
message.setSubject("供应商反馈意见信息"); //标题不能为空
message.setSummary("您有一条供应商反馈信息,请注意查收");//消息摘要不能为空
message.setContent("异常反馈单号:"+feedBackName+",已由供应商填写反馈信息。供应商意见:"+
SupplierSuggestionEnum.getNameStatic(saveFeedbackDTO.getSuggestion())
+";供应商意见备注:"+saveFeedbackDTO.getSuggestionMemo()); //内容不能为空
ResponseWrapper respon = messageFeignService.addMessage(message);//APP推送给服务人员的账号
if(!respon.isSuccess()){
log.info("异常反馈单号:"+feedBackName+"的信息变更,APP推送服务人员失败");//APP失败
}
} catch (Exception e) {
log.info("异常反馈单号:"+feedBackName+"的信息变更,APP推送服务人员出现异常");//APP失败
}
}
}
}
然后再controller中进行调用crmservice的pushByAppAndShortMessage()方法的时候,出现如下异常:
2019-01-10 13:29:34,762 ERROR (FeignClientsHeadersTransfer.java:60)- headers复制出错!
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at cn.appliedata.feign.FeignClientsHeadersTransfer.lambda$requestInterceptor$0(FeignClientsHeadersTransfer.java:26)
at feign.SynchronousMethodHandler.targetRequest(SynchronousMethodHandler.java:158)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:88)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
at com.sun.proxy.$Proxy141.addMessage(Unknown Source)
at cn.appliedata.operate.service.CrmService.pushByAppAndShortMessage(CrmService.java:97)
at cn.appliedata.operate.service.CrmService$$FastClassBySpringCGLIB$$7d315874.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
at org.springframework.cloud.sleuth.instrument.async.TraceAsyncAspect.traceBackgroundThread(TraceAsyncAspect.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:627)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:616)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:115)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.springframework.cloud.sleuth.instrument.async.SpanContinuingTraceRunnable.run(SpanContinuingTraceRunnable.java:52)
at java.lang.Thread.run(Thread.java:748)
2019-01-10 13:29:34,763 INFO (AbstractApplicationContext.java:583)- Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3b9a0944: startup date [Thu Jan 10 13:29:34 GMT+08:00 2019]; parent: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@66273da0
2019-01-10 13:29:34,786 INFO (AutowiredAnnotationBeanPostProcessor.java:155)- JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-01-10 13:29:34,803 INFO (ChainedDynamicProperty.java:115)- Flipping property: service-message.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-01-10 13:29:34,804 INFO (ShutdownEnabledTimer.java:58)- Shutdown hook installed for: NFLoadBalancer-PingTimer-service-message
2019-01-10 13:29:34,805 INFO (BaseLoadBalancer.java:192)- Client: service-message instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=service-message,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2019-01-10 13:29:34,805 INFO (DynamicServerListLoadBalancer.java:222)- Using serverListUpdater PollingServerListUpdater
2019-01-10 13:29:34,806 INFO (DynamicServerListLoadBalancer.java:150)- DynamicServerListLoadBalancer for client service-message initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=service-message,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@6b044553
2019-01-10 13:29:34,878 ERROR (SimpleAsyncUncaughtExceptionHandler.java:37)- Unexpected error occurred invoking async method 'public void cn.appliedata.operate.service.CrmService.pushByAppAndShortMessage(java.lang.Integer,java.lang.Integer,cn.appliedata.operate.bean.crmFlow.SaveFeedbackDTO)'.
java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: service-message
at org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient.execute(LoadBalancerFeignClient.java:71)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:97)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
at com.sun.proxy.$Proxy141.addMessage(Unknown Source)
at cn.appliedata.operate.service.CrmService.pushByAppAndShortMessage(CrmService.java:97)
at cn.appliedata.operate.service.CrmService$$FastClassBySpringCGLIB$$7d315874.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
at org.springframework.cloud.sleuth.instrument.async.TraceAsyncAspect.traceBackgroundThread(TraceAsyncAspect.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:627)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:616)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:115)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.springframework.cloud.sleuth.instrument.async.SpanContinuingTraceRunnable.run(SpanContinuingTraceRunnable.java:52)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.netflix.client.ClientException: Load balancer does not have available server for client: service-message
at com.netflix.loadbalancer.LoadBalancerContext.getServerFromLoadBalancer(LoadBalancerContext.java:483)
at com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.call(LoadBalancerCommand.java:184)
at com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.call(LoadBalancerCommand.java:180)
at rx.Observable.unsafeSubscribe(Observable.java:10151)
at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:94)
at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:42)
at rx.Observable.unsafeSubscribe(Observable.java:10151)
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber$1.call(OperatorRetryWithPredicate.java:127)
at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.enqueue(TrampolineScheduler.java:73)
at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.schedule(TrampolineScheduler.java:52)
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.onNext(OperatorRetryWithPredicate.java:79)
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.onNext(OperatorRetryWithPredicate.java:45)
at rx.internal.util.ScalarSynchronousObservable$WeakSingleProducer.request(ScalarSynchronousObservable.java:276)
at rx.Subscriber.setProducer(Subscriber.java:209)
at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:138)
at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:129)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.subscribe(Observable.java:10247)
at rx.Observable.subscribe(Observable.java:10214)
at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:444)
at rx.observables.BlockingObservable.single(BlockingObservable.java:341)
at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:112)
at org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient.execute(LoadBalancerFeignClient.java:63)
... 25 common frames omitted
2019-01-10 13:29:35,024 INFO (ChainedDynamicProperty.java:115) - Flipping property: service-sms-code.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-01-10 13:29:35,648 INFO (ChainedDynamicProperty.java:115)- Flipping property: service-account.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
这篇文章给了一些思路:https://segmentfault.com/a/1190000014418250
大致意思就是,fegin底调用了访问了RequestContextHolder.currentRequestAttributes()导致,因此在service层方法里头调用该方法要慎重,为了避免出错,可以再封装一下。所以我认为是servvice层代用fegin导致的。于是决定把这个方法放到工具类代码中(结合以前遇到的utils中注入bean),
代码如下:然后再controller中调用工具类中此方法,得以实现。正常运行。
package cn.appliedata.operate.util;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import cn.appliedata.common.ResponseWrapper;
import cn.appliedata.message.Message;
import cn.appliedata.message.Message.Type;
import cn.appliedata.model.account.UserAccount;
import cn.appliedata.operate.bean.Supplieres;
import cn.appliedata.operate.bean.crmFlow.SaveFeedbackDTO;
import cn.appliedata.operate.enums.SupplierSuggestionEnum;
import cn.appliedata.operate.feignClient.AccountFeignService;
import cn.appliedata.operate.feignClient.MessageFeignService;
import cn.appliedata.operate.feignClient.SmsCodeFeignService;
import cn.appliedata.operate.service.PartsService;
import cn.appliedata.operate.service.StocksiteService;
import lombok.extern.slf4j.Slf4j;
/**
* @author :ayfei
* @createTime :2019年1月10日下午2:31:41
* @description :
*/
@Slf4j
@Component
public class PushByAppAndShortMessageUtil {
@Autowired
private MessageFeignService messageFeignService;
@Autowired
private SmsCodeFeignService smsCodeFeignService;
@Autowired
private AccountFeignService accountFeignService;
@Autowired
private StocksiteService stocksiteService;
@Autowired
private PartsService partsService;
private static PushByAppAndShortMessageUtil utils ;
@PostConstruct //关键二 通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
public void init() {
utils = this;
utils.messageFeignService = this.messageFeignService; // 初使化时将已静态化的messageFeignService实例化
utils.smsCodeFeignService = this.smsCodeFeignService;
utils.accountFeignService = this.accountFeignService;
utils.stocksiteService = this.stocksiteService;
utils.partsService = this.partsService;
}
/*
* 消息推送[APP、短信]
*/
@Async
public static void pushByAppAndShortMessage(Integer i,Integer senderFlag,SaveFeedbackDTO saveFeedbackDTO){
log.info("log推送1:索引index = "+i);
Message message = new Message(); // 创建APP推送消息体
message.setType(Type.ORDER); // 消息类型不能为空
if(senderFlag == ConstantUtil.INTNUM1){ // 1 服务人员 发送,推送给 供应商
log.info("log推送:推送给供应商");
//有新增、有修改,获取供应商的 accountcode 和 手机号
String type = "";
String feedBackName = "";//异常反馈单号
if(saveFeedbackDTO.getType() == ConstantUtil.INTNUM1){
type = "新增";
message.setContent("您有一条"+type+"异常反馈单待处理,请前往工具->运维反馈进行处理"); //内容不能为空
}else if(saveFeedbackDTO.getType() == ConstantUtil.INTNUM2){
type = "更新";
Map<String,String> serviceWorkerMap = utils.partsService.getServiceWorkerInfo(saveFeedbackDTO.getFeedbackId());
feedBackName = serviceWorkerMap.get("feedBackName"); //当前的异常反馈单号
message.setContent("您有一条"+type+"异常反馈单待处理,异常反馈单号:"+feedBackName+",请前往工具->运维反馈进行处理"); //内容不能为空
}
//根据供应商id获取责任人id和电话
String ownerId = null;
String ownerPhone = null;
List<Supplieres> supplierSelect = utils.stocksiteService.supplierSelect(null, null, saveFeedbackDTO.getSupplierId());
if(supplierSelect == null || supplierSelect.get(0) == null){
log.info("根据供应商id获取责任人信息为空");
return;
}else{
ownerId = supplierSelect.get(0).getOwnerId();
ownerPhone = supplierSelect.get(0).getNewSupplierCall();
}
if(ownerPhone != null){
try {
//短信推送供应商
String shortMessage = "您有一条"+type+"异常反馈单:"+feedBackName+"待处理,请登录APP前往工具->运维反馈进行处理。";
//ResponseWrapper sendArbitrarilyMsg = utils.sendArbitrarilyMsg(ownerPhone, "", shortMessage);//给服务人员发送短信
ResponseWrapper sendArbitrarilyMsg = utils.smsCodeFeignService.sendArbitrarilyMsg("18337117299", "", shortMessage);//给服务人员发送短信
if(!sendArbitrarilyMsg.isSuccess()){
log.info("异常反馈单号:"+feedBackName+"的消息短信推送供应商失败");//短信发送失败
}
} catch (Exception e) {
log.info("异常反馈单号:"+feedBackName+"的消息短信推送供应商出现异常");
}
try {
//APP推送供应商 获取供应商账号的 accountcode
ResponseWrapper<UserAccount> accountByMobile = utils.accountFeignService.getAccountByMobile(ownerPhone);
if(accountByMobile == null || accountByMobile.getObj() == null){
log.info("获取远程供应商账号信息异常/返回信息为空");
return;
}
message.setTo(accountByMobile.getObj().getAccountCode()); //
message.setSubject(type+"异常反馈单信息"); //标题不能为空
message.setSummary("您有一条"+type+"异常反馈单信息,注意查收");//消息摘要不能为空
ResponseWrapper respon = utils.messageFeignService.addMessage(message); //APP推送给供应商的账号
if(!respon.isSuccess()){
log.info("异常反馈单新增、修改信息,APP推送供应商失败");//APP失败
}
} catch (Exception e) {
log.info("异常反馈单新增、修改信息,APP推送供应商出现异常");//APP异常
}
}else{
log.info("供应商电话为null!,无法推送");
}
}else if(senderFlag == ConstantUtil.INTNUM2){ //2 供应商 发送, 推送给 服务人员
log.info("log推送:推送给服务人员");
//只有修改功能,获取服务人员的accountcode 和 手机号
String feedbackidStr = saveFeedbackDTO.getFeedbackId();
Map<String,String> serviceWorkerMap = utils.partsService.getServiceWorkerInfo(feedbackidStr);//根据异常反馈单id获取对应服务单对应的服务人员信息
if(serviceWorkerMap == null){
log.info("根据异常反馈单id:"+saveFeedbackDTO.getFeedbackId()+"获取到的服务人员信息对象为null");
return;
}
String serviceWorkerMobile = serviceWorkerMap.get("mobile"); //服务人员电话
String serviceWorkerId = serviceWorkerMap.get("id"); //服务人员id
String serviceWorkerName = serviceWorkerMap.get("name"); //服务人员姓名
String feedBackName = serviceWorkerMap.get("feedBackName"); //当前的异常反馈单号
//获取服务人员账号的 accountcode
try {
ResponseWrapper<UserAccount> accountByMobile = utils.accountFeignService.getAccountByMobile(serviceWorkerMobile);
if(accountByMobile == null || accountByMobile.getObj() == null){
log.info("获取远程供应商账号信息异常/返回信息为空");
return;
}
message.setTo(accountByMobile.getObj().getAccountCode());
message.setSubject("供应商反馈意见信息"); //标题不能为空
message.setSummary("您有一条供应商反馈信息,请注意查收");//消息摘要不能为空
message.setContent("异常反馈单号:"+feedBackName+",已由供应商填写反馈信息。供应商意见:"+
SupplierSuggestionEnum.getNameStatic(saveFeedbackDTO.getSuggestion())
+";供应商意见备注:"+saveFeedbackDTO.getSuggestionMemo()); //内容不能为空
ResponseWrapper respon = utils.messageFeignService.addMessage(message);//APP推送给服务人员的账号
if(!respon.isSuccess()){
log.info("异常反馈单号:"+feedBackName+"的信息变更,APP推送服务人员失败");//APP失败
}
} catch (Exception e) {
log.info("异常反馈单号:"+feedBackName+"的信息变更,APP推送服务人员出现异常");//APP异常
}
}
}
}
===============================================
上述的这个方法,业务上属于消息推送,我决定让他异步执行。使用注解实现。但是一直没有生效,结合文章:
https://blog.csdn.net/qq_34545192/article/details/80484780,主要是因为第三点我没有做到!!!
在@SpringBootApplication启动类 添加注解@EnableAsync
异步方法使用注解@Async ,返回值为void或者Future
切记一点 ,异步方法和调用方法一定要**** 写在不同的类中 ****,如果写在一个类中,是没有效果的
为什么第三点会没有效果,那是因为Spring像@Transcation @Async等这些都是使用了动态代理,由Proxy$对象去调用被增强方法,重点来了:方法里想用增强方法(博主说的第三点)则需要得到当前的Proxy$对象 详情请看 ->Spring的 AopContext.currentProxy()方法
springcloud中servcie层调用fegin异常以及异步方法的实现的更多相关文章
- springCloud中的服务调用feign
springCloud中的服务调用(要在调用端写) 前提进行了服务注册 流程: 1.在服务模块中添加依赖 <!--服务调用--> <dependency> <groupI ...
- C# 中 async/await 调用传统 Begin/End 异步方法
最近在改进园子的图片上传程序,希望实现用户上传图片时同时将图片文件保存在三个地方:1)服务器本地硬盘:2)又拍云:3)阿里云OSS.并且在保存时使用异步操作. 对于异步保存到本地硬盘,只需用 Stea ...
- SpringCloud(5)---Feign服务调用
SpringCloud(5)---Feign服务调用 上一篇写了通过Ribbon进行服务调用,这篇其它都一样,唯一不一样的就是通过Feign进行服务调用. 注册中心和商品微服务不变,和上篇博客一样,具 ...
- 非常全面的讲解SpringCloud中Zuul网关原理及其配置,看它就够了!
Zuul是spring cloud中的微服务网关.网关:是一个网络整体系统中的前置门户入口.请求首先通过网关,进行路径的路由,定位到具体的服务节点上. Zuul是一个微服务网关,首先是一个微服务.也是 ...
- MVC003之调用BLL层方法(BLL层调用了WebService)
项目又BLL类库,在类库中引用了webservice.在web层调用BLL的方法时 错误如下: 在 ServiceModel 客户端配置部分中,找不到引用协定“OAService.IntranetSe ...
- SpringCloud项目,接口调用返回http 500 - Internal Server Error的错误
今天上班的时候,自己正在参与的Spring Cloud项目出现了问题,原本上周五还正常的项目突然所有接口调用都是返回http 500的错误. 项目的状态是在Eureka上可以看到对应微服务是在线状态, ...
- servlet层调用biz业务层出现浏览器 500错误,解决方法 dao数据访问层 数据库Util工具类都可能出错 通过新建一个测试类复制代码逐步测试查找出最终出错原因
package com.swift.jztk.servlet; import java.io.IOException; import javax.servlet.ServletException; i ...
- mybatis中存储过程的调用
dao层 // 调用存储过程 void callProcedureGrantEarnings(@Param("params") Map<String,Object> p ...
- SpringCloud 服务间互相调用 @FeignClient注解
SpringCloud搭建各种微服务之后,服务间通常存在相互调用的需求,SpringCloud提供了@FeignClient 注解非常优雅的解决了这个问题 首先,保证几个服务都在一个Eureka中注册 ...
随机推荐
- DOM 中 Property 和 Attribute 的区别(转)
property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute:特性),但实际上,二者是不同的东西,属于不同的范畴. property ...
- (转)JAVA中的权限修饰符
注:本博文是转载的,原文地址:http://blog.csdn.net/xk632172748/article/details/51755438 Java中修饰符总结: 访问控制修饰符 访问控制修饰符 ...
- spring----IOC注解方式以及AOP
技术分析之Spring框架的IOC功能之注解的方式 Spring框架的IOC之注解方式的快速入门 1. 步骤一:导入注解开发所有需要的jar包 * 引入IOC容器必须的6个jar包 * 多引入一个:S ...
- 树莓派 Learning 002 装机后必要的操作 --- 08 实现PC端 远程登入 树莓派 --- 法2 远程登录树莓派的图形桌面
树莓派 装机后必要的操作 - 实现PC端 远程登入 树莓派 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 PC端系统:win10 x64 ...
- 【MySQL】MySQL悲观锁 + 事物 + for update 解决普通流量并发的问题
使用mysql悲观锁解决并发问题 最近学习了一下数据库的悲观锁和乐观锁,根据自己的理解和网上参考资料总结如下: 悲观锁介绍(百科): 悲观锁,正如其名,它指的是对数据被外界(包括本系统当前的其他事 ...
- JavaScript学习系列7 -- JavaScript中的运算符
今天,我们来说一说JavaScript中的运算符,首先我们来讲一讲 一元运算符 JavaScript中的一元运算符有以下几种 1. delete delete 运算符主要用于删除对以前定义的对象属性或 ...
- SQL Server事务回滚对自增键的影响
SQL Server事务回滚时是删除原先插入导致的自增值,也就是回滚之前你你插入一条数据导致自增键加1,回滚之后还是加1的状态 --如果获取当前操作最后插入的identity列的值:select @@ ...
- <你的孤独,虽败犹荣> 很喜欢的句子
希望未来的工作中能够经常出差,做一个能看到除了湖南之外的世界的人 即使我们一辈子给人打工,也要打自己愿意打的工 正在经历的孤独,我们称之为迷茫,经过的那些孤独,我们称之为成长 青春,是一个容量极其有限 ...
- Openstack swift 学习笔记
Swift 不是文件系统或者实时的数据存储系统,而是对象存储,用于长期存储永久类型的静态数据.这些数据可以检索.调整和必要时进行更新.Swift最适合虚拟机镜像.图片.邮件和存档备份这类数据的存储. ...
- Cogs 2856. [洛谷U14475]部落冲突
2856. [洛谷U14475]部落冲突 ★★★ 输入文件:lct.in 输出文件:lct.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 在一个叫做Travi ...