IM麻煩與使用該代碼的簡單REST服務:作爲用於同步訪問
@GET
@Path("next/{uuid}")
@Produces({"application/xml", "application/json"})
public synchronized Links nextLink(@PathParam("uuid") String uuid) {
Links link = null;
try {
link = super.next();
if (link != null) {
link.setStatusCode(5);
link.setProcessUUID(uuid);
getEntityManager().flush();
Logger.getLogger("Glassfish Rest Service").log(Level.INFO, "Process {0} request url : {1} #id {2} at {3} #", new Object[]{uuid, link.getLinkTxt(), link.getLinkID(), Calendar.getInstance().getTimeInMillis()});
}
} catch (NoResultException ex) {
} catch (IllegalArgumentException ex) {
}
return link;
}
這應該提供一個鏈接對象,並且將其標記(setStatusCode(5)),以防止下一個訪問以服務發送相同的對象。問題在於,當有很多快速客戶端訪問Web服務時,這個客戶端會爲不同的客戶端提供相同鏈接對象的2或3倍。我該如何解決這個問題?
這裏使用到resquest: @NamedQuery(名稱= 「Links.getNext」,查詢= 「選擇升FROM鏈接升WHERE l.statusCode = 2」)
和super.next()梅索德:
public T next() {
javax.persistence.Query q = getEntityManager().createNamedQuery("Links.getNext");
q.setMaxResults(1);
T res = (T) q.getSingleResult();
return res;
}
THX
不知道我可以回答你吞下異常的問題! –
不知道這種答案可以幫助這裏的任何人...... – user1754385