-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
I am having a web application and a web service to update data in database. Can I use the webservice to do a server push to all web clients to refresh the UI?
I am aware that we can use the timer in web application to check changes from database and update the UI. However, this seems not to be the right way to do it. Can the web service somehow broadcast an event to the web application to do a server push rather than the web application has to always make query to check changes from DB.
Thank you for your supporting.
If we guess that the web application (frontend) is separated from the webservice (backend) than i think no one will have frontend libs in the backend project. Only my 10 cent.
Yes, you are right. However, I probably found a way around. I create this zul file
<?page title="Refresh" contentType="text/html;charset=UTF-8"?> <zk> <window > <zscript> import fi.uwasa.netmes.zkossController.*; String lineId=Executions.getCurrent().getParameter("lineId"); EventQueue que = EventQueues.lookup(WorkQueueController.REFRESH_EVENT_HOST_NAME, EventQueues.APPLICATION, true); que.publish(new Event(WorkQueueController.REFRESH_EVENT_NAME, null,lineId)); </zscript> </window> </zk>
Then in the webservices I only need to invoke the zul file and it will publish event to refresh browser. However, I am stuck at invoking the zul file. Can you provide some more help.
Thank you.
@Resource private WebServiceContext context; private void doServerPush(String lineId) throws IOException{ ServletContext servletContext = (ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT); URL url = servletContext.getResource("/public/refresh.zul"); URLConnection conn=url.openConnection(); conn.connect(); }
This will work:
@Resource private WebServiceContext context; private void doServerPush(String lineId) throws IOException{ HttpServletRequest request = (HttpServletRequest ) context.getMessageContext().get(MessageContext.SERVLET_REQUEST); String uri=request.getScheme()+"://"+request.getLocalAddr()+":"+request.getLocalPort()+request.getContextPath()+REFRESH_PAGE+"?lineId="+lineId; System.out.println(uri); HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(uri); HttpResponse response = httpclient.execute(httpget); }
Thank you very much.
Asked: 2011-03-22 05:26:23 +0800
Seen: 368 times
Last updated: Mar 22 '11