使用彗星处理器

时间:2018-05-07 13:13:50

标签: java tomcat comet

这可能是重复的,但没有一个答案有用..

我有一个实现CometProcessor的http servlet,我已经引用了this示例,并且还将tomcat连接器协议切换为非阻塞io。但每当我尝试运行该项目时,它都会给我error img

错误请帮忙

下面是我的Servlet(加上助手类)和web.xml

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.CometEvent;
import org.apache.catalina.CometProcessor;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;

public class TomcatWeatherServlet extends HttpServlet implements CometProcessor {

    private static final long       serialVersionUID    = 1L;
    private Logger                  logger              = Logger.getLogger(TomcatWeatherServlet.class.getName());

    private MessageSender           messageSender       = null;
    private static final Integer    TIMEOUT             = 60 * 1000;

    @Override
    public void destroy() {
        messageSender.stop();
        messageSender = null;

    }

    @Override
    public void init() throws ServletException {
        messageSender = new MessageSender();
        Thread messageSenderThread = new Thread(messageSender,
                "MessageSender[" + getServletContext().getContextPath() + "]");
        messageSenderThread.setDaemon(true);
        messageSenderThread.start();

    }

    public void event(final CometEvent event) throws IOException, ServletException {
        HttpServletRequest request = event.getHttpServletRequest();
        HttpServletResponse response = event.getHttpServletResponse();
        if (event.getEventType() == CometEvent.EventType.BEGIN) {
            request.setAttribute("org.apache.tomcat.comet.timeout", TIMEOUT);
            logger.log(Level.INFO, "Begin for session: " + request.getSession(true).getId());
            messageSender.setConnection(response);
            Weatherman weatherman = new Weatherman(95118, 32408);
            weatherman.start();
        } else if (event.getEventType() == CometEvent.EventType.ERROR) {
            logger.log(Level.INFO, "Error for session: " + request.getSession(true).getId());
            event.close();
        } else if (event.getEventType() == CometEvent.EventType.END) {
            logger.log(Level.INFO, "End for session: " + request.getSession(true).getId());
            event.close();
        } else if (event.getEventType() == CometEvent.EventType.READ) {
            throw new UnsupportedOperationException("This servlet does not accept data");
        }

    }

    private class Weatherman {

        private final List<URL> zipCodes;
        private final String    YAHOO_WEATHER   = "http://weather.yahooapis.com/forecastrss?p=";

        public Weatherman(Integer... zips) {
            zipCodes = new ArrayList<URL>(zips.length);
            for (Integer zip : zips) {
                try {
                    zipCodes.add(new URL(YAHOO_WEATHER + zip));
                } catch (Exception e) {
                    logger.log(Level.INFO, "weather man failed");
                }
            }
        }

        public void start() {
            Runnable r = new Runnable() {

                public void run() {
                    int i = 0;
                    while (i >= 0) {
                        int j = i % zipCodes.size();
                        SyndFeedInput input = new SyndFeedInput();
                        try {
                            InputStreamReader isr = new InputStreamReader(zipCodes.get(i).openStream());
                            SyndFeed feed = input.build(isr);
                            SyndEntry entry = (SyndEntry) feed.getEntries().get(0);
                            messageSender.send(entryToHtml(entry));
                            Thread.sleep(3000L);
                        } catch (Exception e) {
                            logger.log(Level.INFO, "start failed");
                        }
                        i++;
                    }
                }
            };
            Thread t = new Thread(r);
            t.start();
        }

        private String entryToHtml(SyndEntry entry) {
            StringBuilder html = new StringBuilder("<h2>");
            html.append(entry.getTitle());
            html.append("</h2>");
            html.append(entry.getDescription().getValue());
            return html.toString();
        }
    }

    private class MessageSender implements Runnable {

        protected boolean                   running     = true;
        protected final ArrayList<String>   messages    = new ArrayList<String>();
        private ServletResponse             connection;

        private synchronized void setConnection(ServletResponse connection) {
            this.connection = connection;
            notify();
        }

        public void stop() {
            running = false;
        }

        /**
         * Add message for sending.
         */
        public void send(String message) {
            synchronized (messages) {
                messages.add(message);
                logger.log(Level.INFO, "Message added #messages=" + messages.size());
                messages.notify();
            }
        }

        public void run() {
            while (running) {
                if (messages.size() == 0) {
                    try {
                        synchronized (messages) {
                            messages.wait();
                        }
                    } catch (InterruptedException e) {
                        logger.log(Level.INFO, e.getMessage());
                    }
                }
                String[] pendingMessages = null;
                synchronized (messages) {
                    pendingMessages = messages.toArray(new String[0]);
                    messages.clear();
                }
                try {
                    if (connection == null) {
                        try {
                            synchronized (this) {
                                wait();
                            }
                        } catch (InterruptedException e) {
                            logger.log(Level.INFO, e.getMessage());
                        }
                    }
                    PrintWriter writer = connection.getWriter();
                    for (int j = 0; j < pendingMessages.length; j++) {
                        final String forecast = pendingMessages[j] + "<br>";
                        writer.println(forecast);
                        logger.log(Level.INFO, "Writing:" + forecast);
                    }
                    writer.flush();
                    writer.close();
                    connection = null;
                    logger.log(Level.INFO, "Closing connection");
                } catch (IOException e) {
                    logger.log(Level.INFO, "IOExeption sending message", e);
                }
            }
        }
    }
}



 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>WeatherServlet</servlet-name>
        <servlet-class>servlet.TomcatWeatherServlet</servlet-class> 
    </servlet>
    <servlet-mapping>
        <servlet-name>WeatherServlet</servlet-name>
        <url-pattern>/Weather</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

1 个答案:

答案 0 :(得分:0)

这里有几个问题。

首先,您尚未在servlet中实现doGet方法,这就是为什么在该资源上调用GET会返回405响应。使用常规HTTP呼叫GET /Weather时,您期望得到什么?

其次,Tomcat在8.5中放弃了对Comet的支持,转而采用更标准的WebSocket API。

所以,你不能在Tomcat 9中使用Comet。我很惊讶你甚至可以在Tomcat 9中初始化那个servlet。你必须要抓住一堆JAR文件从旧的Tomcat安装,以使其均匀加载。但除非你降级到Tomcat 8.0,否则它永远不会真正起作用。

最好切换到WebSocket。