You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Nimit Walia <ni...@itgindia.com> on 2006/11/07 04:57:34 UTC

Comet + tomcat6

F ANYBODY CAN HELP PLS HELP ME AS SOON AS POSSIBLE

 

We are using comet feature provided by Tomcat 6.0. According to
Documantation we have implemented the CometProcessor Interface this is same
as example given in Tomcat Advanced IO documentation. but when we  sends a
request(that is a normal Http Request URL is

"http://192.168.10.28:8080/CometChatApp/ChatServlet") to that servlet always
service  method get invoked. which is contrary to Documentation that if we
implement CometProcessor always event method get invoked, if we do not
provide any service method we get the following error. - "HTTP Status 405 -
HTTP method GET is not supported by this URL - The specified HTTP method is
not allowed for the requested resource (HTTP  method GET is not supported by
this URL).".

we couldn't understand how to implement CometProcessor or send the request
to servlet(is there any other way to send request or we have to  set some
attribute in request). please help us cos we have an urgent requirment to
implemet our project on comet and we want to use the  feature provided by
tomcat container rather then using some other third party api's.

 

so we will be very thankful to you if u give us some guidence.

 

I am sending you the Servlet Code -----------------

 

----------------------------------------------------------------------------

-------------------------------------------

 

 

public class ChatServlet  extends HttpServlet implements CometProcessor {

 

    protected ArrayList<HttpServletResponse> connections = 

        new ArrayList<HttpServletResponse>();

    protected MessageSender messageSender = null;

    

    public void init() throws ServletException {

            System.out.println("Inside Init ChatServlet...");

        messageSender = new MessageSender();

        Thread messageSenderThread = 

            new Thread(messageSender, "MessageSender[" +

getServletContext().getContextPath() + "]");

        messageSenderThread.setDaemon(true);

        messageSenderThread.start();

    }

   

    public void destroy() {

            System.out.println("Inside Destroy ChatServlet...");

        connections.clear();        

        messageSender.stop();

        messageSender = null;

    }

    

    /**

     * Process the given Comet event.

     * 

     * @param event The Comet event that will be processed

     * @throws IOException

     * @throws ServletException

     */

    public void event(CometEvent event)

        throws IOException, ServletException {

            System.out.println("Inside event ChatServlet...");

        // Note: There should really be two servlets in this example, to
avoid

        // mixing Comet stuff with regular connection processing

        HttpServletRequest request = event.getHttpServletRequest();

        HttpServletResponse response = event.getHttpServletResponse();

        

        if (event.getEventType() == CometEvent.EventType.BEGIN) {

            String action = request.getParameter("action");

            System.out.println("Inside CometEvent BEGIN action = "+action);

            if (action != null) {

                if ("login".equals(action)) {

                    String nickname = request.getParameter("nickname");

                    request.getSession(true).setAttribute("nickname",

nickname);

                    response.sendRedirect("post.jsp");

                    event.close();

                    return;

                } else {

                    String nickname = (String)
request.getSession(true).getAttribute("nickname");

                    String message = request.getParameter("message");

                    messageSender.send(nickname, message);

                    response.sendRedirect("post.jsp");

                    event.close();

                    return;

                }

            } else {

                if (request.getSession(true).getAttribute("nickname") ==

null) {

                    // Redirect to "login"

                    log("Redirect to login for session: " +
request.getSession(true).getId());

                    response.sendRedirect("login.jsp");

                    event.close();

                    return;

                }

            }

            begin(event, request, response);

        } else if (event.getEventType() == CometEvent.EventType.ERROR) {

            error(event, request, response);

        } else if (event.getEventType() == CometEvent.EventType.END) {

            end(event, request, response);

        } else if (event.getEventType() == CometEvent.EventType.READ) {

            read(event, request, response);

        }

    }

 

    protected void begin(CometEvent event, HttpServletRequest request,
HttpServletResponse response)

        throws IOException, ServletException {

            System.out.println("Inside begin ChatServlet...");

 

            System.out.println("Inside Begin ");

            request.setAttribute("org.apache.tomcat.comet", Boolean.TRUE);

        log("Begin for session: " + request.getSession(true).getId());

 

        PrintWriter writer = response.getWriter();

        writer.println("<!doctype html public \"-//w3c//dtd html 4.0

transitional//en\">");        

        writer.println("<head><title>JSP Chat</title></head><body
bgcolor=\"#FFFFFF\">");

        writer.flush();

 

        synchronized(connections) {

            connections.add(response);

        }

    }

    

    protected void end(CometEvent event, HttpServletRequest request,
HttpServletResponse response)

        throws IOException, ServletException {

            System.out.println("Inside end ChatServlet...");

            log("End for session: " + request.getSession(true).getId());

        synchronized(connections) {

            connections.remove(response);

        }

        request.removeAttribute("org.apache.tomcat.comet");

        PrintWriter writer = response.getWriter();

        writer.println("</body></html>");

        

        event.close();

        

    }

    

    protected void error(CometEvent event, HttpServletRequest request,
HttpServletResponse response)

        throws IOException, ServletException {

            System.out.println("Inside error ChatServlet...");

        log("Error for session: " + request.getSession(true).getId());

        synchronized(connections) {

            connections.remove(response);

        }

        event.close();

    }

    

    protected void read(CometEvent event, HttpServletRequest request,
HttpServletResponse response)

        throws IOException, ServletException {

            System.out.println("Inside read ChatServlet...");

        InputStream is = request.getInputStream();

        byte[] buf = new byte[512];

        do {

            int n = is.read(buf);

            if (n > 0) {

                log("Read " + n + " bytes: " + new String(buf, 0, n) 

                        + " for session: " +
request.getSession(true).getId());

            } else if (n < 0) {

                error(event, request, response);

                return;

            }

        } while (is.available() > 0);

    }

 

 

 

    /**

     * Poller class.

     */

    public class MessageSender implements Runnable {

 

        protected boolean running = true;

        protected ArrayList<String> messages = new ArrayList<String>();

        

        public MessageSender() {

        }

        

        public void stop() {

            running = false;

        }

 

        /**

         * Add specified socket and associated pool to the poller. The
socket will

         * be added to a temporary array, and polled first after a maximum
amount

         * of time equal to pollTime (in most cases, latency will be much
lower,

         * however).

         *

         * @param socket to add to the poller

         */

        public void send(String user, String message) {

            System.out.println("Inside Send Message = User = "+message+"

"+user);

            synchronized (messages) {

                messages.add("[" + user + "]: " + message);

                messages.notify();

            }

        }

 

        /**

         * The background thread that listens for incoming TCP/IP
connections and

         * hands them off to an appropriate processor.

         */

        public void run() {

 

            // Loop until we receive a shutdown command

            while (running) {

                // Loop if endpoint is paused

 

                if (messages.size() == 0) {

                    try {

                        synchronized (messages) {

                            messages.wait();

                        }

                    } catch (InterruptedException e) {

                        // Ignore

                    }

                }

 

                synchronized (connections) {

                    String[] pendingMessages = null;

                    synchronized (messages) {

                        pendingMessages = messages.toArray(new String[0]);

                        messages.clear();

                    }

                    for (int i = 0; i < connections.size(); i++) {

                        try {

                            PrintWriter writer =
connections.get(i).getWriter();

                            for (int j = 0; j < pendingMessages.length; j++)
{

                                // FIXME: Add HTML filtering

                                writer.println(pendingMessages[j] + "<br>");

                            }

                            writer.flush();

                        } catch (IOException e) {

                            log("IOExeption sending message", e);

                        }

                    }

                }

 

            }

 

        }

 

    }

 

 

 

 

}

 


Re: Comet + tomcat6

Posted by Rémy Maucherat <re...@gmail.com>.
The place to look is: http://tomcat.apache.org/tomcat-6.0-doc/aio.html

I have added a bigger warning that these features require the APR or
NIO HTTP connector.

Rémy

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re[2]: Comet + tomcat6

Posted by Dima Retov <di...@axisway.com>.
JK> If you could guide us or send us a link as to how to use APR and NIO
JK> connectors (which support Comet).

APR is not comet.
NIO is comet.

I do not think that APR support async IO.
APR only handles sleeping keep-alive connections in async way.
APR processes requests in regular way.

Am I right?


Wednesday, November 8, 2006, 2:03:29 PM, you wrote:

JK> Hi Mark

JK> Thank you for your suggestions. We are using the default connector for
JK> tomcat6(probably the Http connectors). We would be greatful to you
JK> If you could guide us or send us a link as to how to use APR and NIO
JK> connectors (which support Comet).

JK> Regards
JK> Jayant

JK> -----Original Message-----
JK> From: Mark Thomas [mailto:markt@apache.org] 
JK> Sent: Tuesday, November 07, 2006 9:59 AM
JK> To: Tomcat Users List
JK> Subject: Re: Comet + tomcat6

JK> Nimit Walia wrote:
>> F ANYBODY CAN HELP PLS HELP ME AS SOON AS POSSIBLE

JK> Please do not send the same message multiple times.
JK> http://tomcat.apache.org/faq/tomcatuser.html#why

JK> I haven't looked at or been involved in the Comet development but as far as
JK> I can tell from looking at the code and the docs you need to be using the
JK> APR (http://tomcat.apache.org/tomcat-6.0-doc/apr.html) or NIO connectors.

JK> Which connector are you using?

JK> There is a request flag that indicates comet support
JK> (org.apache.tomcat.comet.support) but it is set on every request by the
JK> connectors that support comet so you should not need to do anything here.

JK> The only other thing I can suggest is to look at the chat example provided
JK> in the TC6 for further clues.

JK> HTH,

JK> Mark

JK> ---------------------------------------------------------------------
JK> To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe,
JK> e-mail: users-unsubscribe@tomcat.apache.org
JK> For additional commands, e-mail: users-help@tomcat.apache.org


JK> --
JK> No virus found in this incoming message.
JK> Checked by AVG Free Edition.
JK> Version: 7.1.409 / Virus Database: 268.13.30/521 - Release Date: 11/7/2006
 




-- 
Best regards,
 Dima                            mailto:dima@axisway.com



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Comet + tomcat6

Posted by Mark Thomas <ma...@apache.org>.
Jayant Kumar wrote:
> Hi Mark
> 
> Thank you for your suggestions. We are using the default connector for
> tomcat6(probably the Http connectors). We would be greatful to you 
> If you could guide us or send us a link as to how to use APR and NIO
> connectors (which support Comet).

It was in my last message.

Mark

> -----Original Message-----
> From: Mark Thomas [mailto:markt@apache.org] 
> Sent: Tuesday, November 07, 2006 9:59 AM
> To: Tomcat Users List
> Subject: Re: Comet + tomcat6
<snip/>
> I haven't looked at or been involved in the Comet development but as far as
> I can tell from looking at the code and the docs you need to be using the
> APR (http://tomcat.apache.org/tomcat-6.0-doc/apr.html) or NIO connectors.
<snip/>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Comet + tomcat6

Posted by Jayant Kumar <ja...@itgindia.com>.
Hi Mark

Thank you for your suggestions. We are using the default connector for
tomcat6(probably the Http connectors). We would be greatful to you 
If you could guide us or send us a link as to how to use APR and NIO
connectors (which support Comet).

Regards
Jayant

-----Original Message-----
From: Mark Thomas [mailto:markt@apache.org] 
Sent: Tuesday, November 07, 2006 9:59 AM
To: Tomcat Users List
Subject: Re: Comet + tomcat6

Nimit Walia wrote:
> F ANYBODY CAN HELP PLS HELP ME AS SOON AS POSSIBLE

Please do not send the same message multiple times.
http://tomcat.apache.org/faq/tomcatuser.html#why

I haven't looked at or been involved in the Comet development but as far as
I can tell from looking at the code and the docs you need to be using the
APR (http://tomcat.apache.org/tomcat-6.0-doc/apr.html) or NIO connectors.

Which connector are you using?

There is a request flag that indicates comet support
(org.apache.tomcat.comet.support) but it is set on every request by the
connectors that support comet so you should not need to do anything here.

The only other thing I can suggest is to look at the chat example provided
in the TC6 for further clues.

HTH,

Mark

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe,
e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.409 / Virus Database: 268.13.30/521 - Release Date: 11/7/2006
 

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.409 / Virus Database: 268.13.32/523 - Release Date: 11/7/2006
 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Comet + tomcat6

Posted by Mark Thomas <ma...@apache.org>.
Nimit Walia wrote:
> F ANYBODY CAN HELP PLS HELP ME AS SOON AS POSSIBLE

Please do not send the same message multiple times.
http://tomcat.apache.org/faq/tomcatuser.html#why

I haven't looked at or been involved in the Comet development but as
far as I can tell from looking at the code and the docs you need to be
using the APR (http://tomcat.apache.org/tomcat-6.0-doc/apr.html) or
NIO connectors.

Which connector are you using?

There is a request flag that indicates comet support
(org.apache.tomcat.comet.support) but it is set on every request by
the connectors that support comet so you should not need to do
anything here.

The only other thing I can suggest is to look at the chat example
provided in the TC6 for further clues.

HTH,

Mark

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org