jersey - asp.net(mvc) not consuming any thread while waiting for a chat message using async/await? -
what want achieve 100% thread agility asp.net 4.5 async/await while waiting chat message (or msmq message)to come. async/await can release http request handling thread thread pool, how not use thread while waiting chat messages come?
in java, can use latest jersey rest api achieve using @managedasync/@suspended annotation:
// java code, using jersey rest api @path("/chatroom") public class chathandler { private static final hashmap<integer, asyncresponse> map = new hashmap<>(); @get @path("/joinroom") @managedasync public void joinroom(@pathparam("userid") string id, @suspended final asyncresponse ar) { map.put(i, ar); } @get @path("/postchat") public string sendchat(@pathparam("userid") string id, @queryparam("message") string message) throws interruptedexception { map.get(i).resume(message); return "message sent user " + id; } }
here description above code. john first uses url this join chat room user john in order receive chat messages. in corresponding method joinroom(), jersey will, same async/await in asp.net 4.5, return http request thread thread pool. here put httpcontext, in jersey asyncresponse object, in hashmap later use.
then next 50 seconds, there nobody sending chat messages, backend jersey server not use threads on during 50 seconds. no thread spent on waiting next chat message. on 51st second, mary goes url send hello message user john. in sendchat() method, retrive httpcontext (asyncresponse) hash map, , resume chat message. @ time, jersey find available thread in http request thread pool resume asyncresponse , send chat message "hello" john's browser. mary's browser see "message sent user john".
so how can acheive same 100% thread agility asp.net async/await? in await method, can not find correct way wait chat message without occupying worker thread.
there asp.net system designed this: signalr. designed async
in mind, makes optimum use of threads.
if don't want use signalr, can use of number of asynchronous coordination primitives. tpl dataflow may fit kind of scenario, or can use semaphoreslim
or taskcompletionsource
or of primitives in asyncex library.
Comments
Post a Comment