3
我正在創建需要有狀態的REST Web服務。考慮以下情況:Java EE 6 - 作爲有狀態會話bean的有狀態REST
- Web服務執行困難和耗時的計算並返回非常大的結果。所以這個服務只返回一些結果,整個結果保存在有狀態bean的服務器上。
- 當結果存在時。客戶可以詢問現有結果的一部分。
我試圖通過@Stateful
會話bean來做到這一點,但它仍然像@Stateless
那樣工作。現在我想知道它甚至是可能的,因爲客戶端不接受任何Cookie,因此服務器無法識別它。
是否可以通過REST擁有有狀態Bean?
代碼示例:
@Path("/similarity/")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Stateful
@StatefulTimeout(600000) // 10 minutes
public class SimilarityResource {
private List<SimilarityResult> savedSimilarityResults = new ArrayList<SimilarityResult>();
@POST
@Path("/atom-count/")
public List<SimilarityResult> atomCountSimilarity(JAXBElement<SimilarityRequestXML> sr) {
try {
if (this.savedSimilarityResults.isEmpty()) {
List<SimilarityResult> similarityResults = acs.findAllSimilar(); // Time consuming
this.savedSimilarityResults = similarityResults; // Save results
return similarityResults;
} else {
CompoundResponse cr = new CompoundResponse("Hureeey stateful bean works!.", 404);
throw new WebApplicationException(cr.buildResponse());
}
} catch (CompoundSearchException e) {
CompoundResponse cr = new CompoundResponse(500, e);
throw new WebApplicationException(cr.buildResponse());
}
}
}
我想到的是,當我把這個/atom-count/
方法兩次,它應該響應與404