简体   繁体   English

如何编写Java EJB异步服务?

[英]How to write a java EJB async service?

I tried to do this 我试图做到这一点

@Consumes(APPLICATION_XML)
@Produces(APPLICATION_XML)
public class TestService {
    @POST
    @Path("querymember")
    @Asynchronous
    public Future<String> queryMemberAysnc(Member member) {
        return new AsyncResult<>(businessLogic.query(member));
    }
}

When I do this in synchronous way, it works fine. 当我以同步方式执行此操作时,它可以正常工作。 But once I change it to async, it is giving me "org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.writeResponseErrorMessage No message body writer has been found for response class AsyncResult." 但是,一旦我将其更改为异步,它就会给我“ org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.writeResponseErrorMessage找不到响应类AsyncResult的消息正文编写器。”

I couldn't find any other example on writing an aysnc operation with EJB. 我找不到使用EJB编写aysnc操作的其他示例。 Any hint on how I can fix this in a right way? 关于如何正确解决此问题的任何提示?

The asynchronous method should be implemented inside EJB Session Bean, because web service call result of type Future make no sence. 异步方法应该在EJB Session Bean内实现,因为类型为Future的Web服务调用结果没有意义。

So, the example idea could be: 1. Implement EJB Session Bean, with @Asynchronous method 2. Implement method in your web service, which will call asynchronous method -> save returned 'Future' object somewhere in your storage by key (static Map, etc) -> return this key to client 3. Implement method in your web service for getting a result by key, which will retreive 'Future' object by key from your storage -> check if Future have result -> return the result to client 因此,示例思路可能是:1.使用@Asynchronous方法实现EJB会话Bean 2.在Web服务中实现方法,该方法将调用异步方法->通过键将返回的“ Future”对象保存在存储中的某个位置(静态Map ,等等)->将此密钥返回给客户端3。在Web服务中实现用于通过密钥获取结果的方法,该方法将从存储中逐个获取'Future'对象->检查Future是否有结果->将结果返回到客户

The client should periodically check for results, until asynchronous method invocation is finished, and result is returned. 客户端应定期检查结果,直到异步方法调用完成并返回结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM