日期:2014-05-19  浏览次数:20692 次

关于在STRUTS2中使用CometProcessor实现COMET
小弟是新手,现在需要使用STRUTS2来实现一个COMET功能。
查了很多资料发现不外乎使用pushlet来实现,但是这个对我来说太大了,我只需要一个小功能。
于是自己写了一个,手工使用Thread.sleep来实现一个简陋的功能,但是实在太简陋了。
找到 http://tomcat.apache.org/tomcat-6.0-doc/aio.html#Example_code 这里,可以直接实现CometProcessor接口实现,现在我的问题是:
如果要使用CometProcessor,我该怎么做?
或者,如果有其它方法,有没有什么参考资料?

希望大家能给点建议,如果有参考代码就更好了。100分送上。

------解决方案--------------------
没用过,帮你顶下吧,开心每一天,加油!
------解决方案--------------------
没人回答吗?
1.改tomcat 的协议
2.建个CometProcessor
Java code
public class FightCometServlet  extends HttpServlet implements CometProcessor{
    private static Log log = LogFactory.getLog(FightCometServlet.class);    
    private static final Integer TIMEOUT = 60 * 1000;
    private FightLogic fightLogic;   

    @Override
    public void init() throws ServletException {
        ServletContext servletContext = this.getServletContext();
        this.fightLogic = (FightLogic)WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean("fightLogic");
        
    }
    
    public void event(final CometEvent event) throws IOException, ServletException {
        HttpServletRequest request = event.getHttpServletRequest();
        HttpServletResponse response = event.getHttpServletResponse();
        response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession(true);
        PrintWriter writer = response.getWriter();        
      
        Message message = null;
        if (event.getEventType() == CometEvent.EventType.BEGIN) {
            
            
            writer.println("什么什么");
            log.info("Writing:" + messageStr);
            writer.flush();
         writer.close();
        } else if (event.getEventType() == CometEvent.EventType.ERROR) {
            log.info("Error for session: " + request.getSession(true).getId());
            event.close();
        } else if (event.getEventType() == CometEvent.EventType.END) {
            log.info("End for session: " + request.getSession(true).getId());
            event.close();
        } else if (event.getEventType() == CometEvent.EventType.READ) {
            throw new UnsupportedOperationException("This servlet does not acceptdata");
        }

    }

}