MessagePanelPolling  function is called  through a wicket page, the wicket
page has an active ajax call to the async servlet, 

        public BasePage(PageParameters  pageParameters) {
                super(pageParameters);
                add(new MessageCheckBehaviour());
        }

    static class MessageCheckBehaviour extends  MyJqueryBehaviour{
           @Override
        public void renderHead(Component component, IHeaderResponse response) {
                super.renderHead(component, response);
        
//response.render(OnLoadHeaderItem.forScript("MessagePanel.longPoll('"+UserContextHolder.getUser().getInfUser().getSysUserId().toString()+"')"));
                response.render(JavaScriptHeaderItem.forReference(JS_FILE));
           }
            static final PackageResourceReference JS_FILE= new
JavaScriptResourceReference(MyJqueryBehaviour.class, "messagePolling.js");
   }


Async servlet waits  on a  blocking queue , a new item in queue causes
thread to  checks all registered AsyncContext's and if match found,  write
to the aynscontext response.


public class MessageNotificationServlet extends HttpServlet {
        
        static Logger  logger= 
Logger.getLogger(MessageNotificationServlet.class);
        
        private Queue<AsyncContext> asyncContexts = new
ConcurrentLinkedQueue<AsyncContext>(); 
        static BlockingQueue<String> messages = new 
LinkedBlockingQueue<String>();
        
        ExecutorService executorService;
        
        public static void addMessage(String msg){
                try{
                        messages.add(msg);
                }catch(Exception e){
                        throw new RuntimeException(e);
                }
        }
        
        @Override
        public void init(ServletConfig config) throws ServletException {
                super.init(config);
                executorService=Executors.newSingleThreadExecutor();
                executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                                while(true){
                                        try{
                                                 String message = 
messages.take();
                                                 for(AsyncContext asyncContext 
:asyncContexts ){
                                                         try{
                                                                
if(asyncContext.getRequest().getParameter("name").trim().equals(message)){
                                                                                
 String
paramName=asyncContext.getRequest().getParameter("name").trim();
                                                                                
 String
timestamp=asyncContext.getRequest().getParameter("timestamp").trim();
                                                                                
 try{
                                                                                
         PrintWriter  printWriter=asyncContext.getResponse().getWriter();
                                                                                
         printWriter.println("new_message");
                                                                                
         printWriter.flush();
                                                                                
         asyncContext.complete();
                                                                                
 }catch(Exception e){
                                                                                
         logger.debug(" failed writing to async "+paramName +" 
"+timestamp );
                                                                                
         logger.error((" async  failed  "+paramName +"  "+timestamp),e);
                                                                                
 }
                                                                         }
                                                         }catch (Exception e) {
                                                                 
asyncContexts.remove(asyncContext);
                                                                 logger.debug(" 
error with asyncontext ",e);
                                                         }
                                                 }
                                        }catch(Exception e){
                                                logger.debug(" error in msg 
check thread ",e);
                                        }
                                }
                        }
                });
        }
        @Override
        public void destroy() {
                super.destroy();
                messages.clear();
                asyncContexts.clear();
                executorService.shutdownNow();
        }
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse 
response)
throws ServletException, IOException {
                        String name=req.getParameter("name");
                        AsyncContext  asyncContext=req.startAsync();
                
asyncContexts.stream().filter(as->as.getRequest().getParameter("name").equals(name)).collect(Collectors.toList()).forEach(ac->ac.complete());
                        asyncContext.getRequest().setAttribute("start_time", 
new Date());
                        asyncContext.addListener(new AsyncListener() {
                                @Override
                                public void onTimeout(AsyncEvent event) throws 
IOException {
                                        logger.debug(" timeout occuered  for  "
+event.getAsyncContext().getRequest().getParameter("name") +" 
"+event.getAsyncContext().getRequest().getParameter("timestamp")+" 
"+event.getAsyncContext().getRequest().getAttribute("start_time"));
                                        event.getAsyncContext().complete();
                                }
                                
                                @Override
                                public void onStartAsync(AsyncEvent event) 
throws IOException {
                                        logger.debug(" onstart for  "
+event.getAsyncContext().getRequest().getParameter("name") +" 
"+event.getAsyncContext().getRequest().getParameter("timestamp") +" 
"+event.getAsyncContext().getRequest().getAttribute("start_time"));
                                }
                                
                                @Override
                                public void onError(AsyncEvent event) throws 
IOException {
                                        logger.debug(" error occuered  for  "
+event.getAsyncContext().getRequest().getParameter("name") +" 
"+event.getAsyncContext().getRequest().getParameter("timestamp") +" 
"+event.getAsyncContext().getRequest().getAttribute("start_time"));
                                        
asyncContexts.remove(event.getAsyncContext());  
                                }
                                
                                @Override
                                public void onComplete(AsyncEvent event) throws 
IOException {
                                        logger.debug(" completed  "
+event.getAsyncContext().getRequest().getParameter("name")+" 
"+event.getAsyncContext().getRequest().getParameter("timestamp") +" 
"+event.getAsyncContext().getRequest().getAttribute("start_time"));
                                        
asyncContexts.remove(event.getAsyncContext());
                                }
                        });
                        asyncContext.setTimeout(TimeUnit.MINUTES.toMillis(60));
                        asyncContexts.add(asyncContext);
                }
}


--
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/open-connection-to-async-servlet-and-initiate-wicket-ajax-request-results-ComponentNotFoundException-tp4674357p4674362.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to