Java 来自异步方法的假调用
我有Spring事件发布者和监听器,要求如下:Java 来自异步方法的假调用,java,spring-boot,jhipster,spring-cloud-feign,feign,Java,Spring Boot,Jhipster,Spring Cloud Feign,Feign,我有Spring事件发布者和监听器,要求如下: 从某个方法触发事件 侦听侦听器类中的事件 一旦听到事件,立即呼叫另一个微服务 所以目前我正在使用呼叫另一个微服务 当侦听器方法同步运行时,此调用工作正常,当侦听器方法标记为@Async时,调用失败 下面是示例代码: 外部接口配置: @AuthorizedFeignClient(name = "another-microservice") public interface CustomFeignInterface { @PostMapping
@AuthorizedFeignClient(name = "another-microservice")
public interface CustomFeignInterface {
@PostMapping("api/call")
public ResponseEntity<String> callMethod(@RequestBody Foo foo);
}
@Component
public class FooEventListener {
@Autowired
private CustomFeignInterface customFeignInt;
@Async
@EventListener
public void handleCustomEvent(Foo foo) {
try {
ResponseEntity<String> res = customFeignInt.callMethod(foo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@AuthorizedFeignClient(name=“另一个微服务”)
公共接口CustomFeignInterface{
@后期映射(“api/调用”)
公共响应性调用方法(@RequestBody Foo Foo);
}
Spring事件侦听器:
@AuthorizedFeignClient(name = "another-microservice")
public interface CustomFeignInterface {
@PostMapping("api/call")
public ResponseEntity<String> callMethod(@RequestBody Foo foo);
}
@Component
public class FooEventListener {
@Autowired
private CustomFeignInterface customFeignInt;
@Async
@EventListener
public void handleCustomEvent(Foo foo) {
try {
ResponseEntity<String> res = customFeignInt.callMethod(foo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@组件
公共类FooEventListener{
@自动连线
私人海关界面海关;
@异步的
@事件监听器
公共无效handleCustomEvent(Foo-Foo){
试一试{
ResponseEntity res=customfaignint.callMethod(foo);
}捕获(例外e){
e、 printStackTrace();
}
}
}
如上所述,当我删除@Async时,对另一个微服务的代码调用工作正常,但当我将@Async添加到FooEventListener.handleCustomEvent(…)
方法时,调用失败
我在某处发现,Feign不支持异步调用:
但是我仍然需要异步行为,我如何才能做到这一点呢?我看不出您链接到的虚假问题与
@async
有什么关系。它是如何失败的?当Async在另一个线程上执行时,是否确实传播了调用所需的线程局部变量?能否提供有关@Async注释“失败”的更多信息?