multithreading - How to persist Entity in separate thread with JPA on J2EE App server? -
i have chat application needs store messages db. connection db little bit slow, therefore delays response chat client.
is possible persist message entity in separate thread? i'm need in: reduce delay before send-recieve message on client.
i try it, doen't work.
dao object:
@stateless public class messagesdao { @persistencecontext(type= persistencecontexttype.extended) private entitymanager entitymanager; private persistencethread persistencethread = new persistencethread(); //another methods public void addmessage(message message) { thread thread = new thread(persistencethread); persistencethread.setmessage(message); thread.start(); } private class persistencethread implements runnable { private message message; public void setmessage(message message) { this.message = message; } public void run() { entitymanager.persist(message); } } }
interface service calls dao persist new message , return clients:
@stateless @path("/messages") @produces("application/xml") @consumes("application/xml") public class messagesserviceimpl { @ejb private messagesdao messagesdao; @post @broadcast(resumeonbroadcast = true) public message postmessage(message message) { messagesdao.addmessage(message); return message; } @get @path("/wait") @suspend(outputcomments = false) public message waitforlastmessage() { return null; } //another methods }
thanks.
give @asynchronous annotation try:
@stateless public class messagesdao { @persistencecontext(type = persistencecontexttype.extended) private entitymanager entitymanager; @asynchronous public void addmessage(message message) { entitymanager.persist(message); } }
just bear in mind requires ejb 3.1.
Comments
Post a Comment