How to implement push to client using Java EE 7 WebSockets? -
How to implement push to client using Java EE 7 WebSockets? -
i've browsed lot of web socket examples, presentation slides , concentrated on rather simple scenarios in client-server communication initiated client.
i interested in scenario, seems as practical: pure server force client.
example have in mind application updates stocks value on website. imagine there external scheme stock exchange system, sending jms message every subscribed stock value change.
i know how translate such incoming jms event server force , efficiently , idiomatically java ee 7 point of view.
as far can understand spec, supposed write web socket endpoint
@serverendpoint("/demo") public class wsendpoint { private static final logger log = logger.getlogger(wsendpoint.class); @onmessage public void onmessage(string message, session session) { log.info("received : " + message + ", session:" + session.getid()); } @onopen public void open(session session) { log.info("open session:" + session.getid()); } @onclose public void close(session session, closereason c) { log.info("close session:" + session.getid()); } }
everything easy when getting message frontend, can whatever in @onmessage
method. in illustration not message client, i'll event external system.
there few approaches. instance can create thread in @onopen
method, demonstrated in this blog. in practice approach might show shortcoming since every client need create new, potentially long living thread.
one can improve using nio channels selectors, demand kind of "hand made" channels management. doable, rather cumbersome.
another solution ping other scheme updates, 1 time again kind of ugly. in add-on not sure if @onopen
method meant used in way.
ideally incoming jms message trigger web socket force client. ideas how implement nicely?
i way (no client request needed):
@serverendpoint("/hello") public class hellowebsocket { @onopen public void greettheclient(session session){ seek { session.getbasicremote().sendtext("hello stranger"); } grab (ioexception ioe) { system.out.println(ioe.getmessage()); } } }
java java-ee websocket java-ee-7
Comments
Post a Comment