Here the source code of my servlet. This code is part of aplication code of
the book "Google Web Toolkit Applications"
public class MessengerServiceCometImpl extends HttpServlet implements
CometProcessor {
class CometMessengerService extends AbstractMessengerService{
final ThreadLocal perThreadRequest = new ThreadLocal();
public String getCurrentId() {
return
((HttpServletRequest)perThreadRequest.get()).getSession(true).getId();
}
public void onEvents(String id) {
synchronized(pendingRequests){
PendingRequest pr = (PendingRequest)pendingRequests.get( id
);
if( pr != null ){
pendingRequests.remove(id);
sendResponse( pr.event, pr.rpcRequest );
}
}
}
}
class PendingRequest{
RPCRequest rpcRequest;
CometEvent event;
public PendingRequest(RPCRequest rpcRequest, CometEvent event) {
this.rpcRequest = rpcRequest;
this.event = event;
}
}
Map pendingRequests = new HashMap();
CometMessengerService messengerService = new CometMessengerService();
public void event(CometEvent event) throws IOException, ServletException
{
if (event.getEventType() == CometEvent.EventType.READ) {
//get the RPC request
RPCRequest rpcRequest = RPC.decodeRequest( readRequest( event )
);
Method targetMethod = rpcRequest.getMethod();
//if its the event request then wait for events
synchronized(pendingRequests){
messengerService.perThreadRequest.set(
event.getHttpServletRequest() );
if( targetMethod.getName().equals("getEvents") &&
!messengerService.hasEvents() ){
//save this request for processing later.
pendingRequests.put( messengerService.getCurrentId(),
new PendingRequest( rpcRequest, event ) );
}
else{
//otherwise process the RPC call as usual
sendResponse( event, rpcRequest );
}
}
}
}
public void sendResponse( CometEvent event, RPCRequest rpcRequest ) {
try{
try{
messengerService.perThreadRequest.set(
event.getHttpServletRequest() );
String result =
RPC.invokeAndEncodeResponse(messengerService, rpcRequest.getMethod(),
rpcRequest.getParameters());
writeResponse(event.getHttpServletResponse(), result);
event.close();
} catch (IncompatibleRemoteServiceException e) {
writeResponse( event.getHttpServletResponse(),
RPC.encodeResponseForFailure(null, e) );
}
}catch (Throwable e) {
writeResponse( event.getHttpServletResponse(), "Server Error" );
}
}
public String readRequest( CometEvent event ) throws IOException,
ServletException{
int contentLength =
event.getHttpServletRequest().getContentLength();
if (contentLength == -1) {
// Content length must be known.
throw new ServletException("Content-Length must be specified");
}
InputStream in = event.getHttpServletRequest().getInputStream();
byte[] payload = new byte[contentLength];
int offset = 0;
int len = contentLength;
int byteCount;
while (offset < contentLength) {
byteCount = in.read(payload, offset, len);
if (byteCount == -1) {
throw new ServletException("Client did not send " +
contentLength
+ " bytes as expected");
}
offset += byteCount;
len -= byteCount;
}
return new String(payload, "UTF-8");
}
public void writeResponse( HttpServletResponse response, String body ){
try {
// this line is added by me
response.addHeader("Transfer-Encoding", "Chunked");
PrintWriter writer = response.getWriter();
writer.print(body);
writer.flush();
} catch (IOException e) {
log("IOExeption sending response", e);
}
}
}
2010/2/21 Chris Lercher <[email protected]>
> Here are some things you can check - maybe one of these helps (?)
>
> - In your servlet: Do you expect that anything gets flushed
> (reliably), before you call the close() method on CometEvent? If yes,
> then what you're actually trying to do is _streaming_ (which doesn't
> work). Calling writer.flush() is certainly not enough, since mod_proxy
> has no way to know that writer.flush() has been called.
>
> - Did you try to set "no-cache" headers etc.?
>
>
> On Feb 21, 1:11 pm, Fran <[email protected]> wrote:
> > I tested the mod_proxy_http, in the last post you can see the config
> > that I used.
> > But in Comet HTTP streaming, I found mod_proxy blocked the response
> > until server close the
> > stream (after timeout), then send the whole response to client at
> > once.
> >
> > In the first time I was wrong because I thought that if I put
> > timeout=1 I found the solution but Its not real because the server
> > close the conection after timeout
> >
> > In the five post of mine is an example of the problem.
> >
> > This dude has a similar problem:
> http://forums.java.net/jive/thread.jspa?threadID=41377
> >
> > I am hopeless :(
> >
> > On 21 feb, 03:21, dablack <[email protected]> wrote:
> >
> >
> >
> > > Sorry to lead you on a wild goose chase. I guess you will have to use
> > > the Apache mod_proxy_http module with the Tomcat NIO connector using
> > > the http protocol after all. I don't have any experience with the
> > > mod_proxy_http module at all so not really anything I can help you
> > > with.
> >
> > > Good luck.
> >
> > > On Feb 20, 7:21 pm, Fran <[email protected]> wrote:
> >
> > > > The problem persist with the news changes.
> > > > Seems that apache cant connect to tomcat :(
> > > > The error log of apache and mod_jk dont show nothing. Not ever nio
> conection
> > > > stablished
> >
> > > > 2010/2/21 dablack <[email protected]>
> >
> > > > > I borrowed that connector configuration from a website I came
> across.
> > > > > I'm not sure why port=0 either. If I put more research into the NIO
> > > > > connector I could probably find out. Why don't you try adding the
> > > > > lines,
> >
> > > > > enableLookups="false"
> > > > > scheme="http"
> >
> > > > > to the NIO connector configuration in the server.xml file that I
> gave
> > > > > you earlier.
> >
> > > > > As far as accessing the ajp service
> usinghttp://localhost:8009/servlet,
> > > > > that will not work. Your browser uses the http protocol and the
> 8009
> > > > > port is being serviced by the ajp protocol. You should still have
> your
> > > > > connector using the http protocol configured to listen on port
> 8081.
> >
> > > > > Hopefully the scheme line above will help the NIO connector
> > > > > communicate back to Apache.
> >
> > > > > On Feb 20, 4:45 pm, Fran <[email protected]> wrote:
> > > > > > I test your config, dablack, but I cant connect to the servlet.
> > > > > > With this config I cant access not even to:
> http://localhost:8009/servlet
> > > > > > that is tomcat directly without apache
> >
> > > > > > On 20 feb, 23:36, Fran <[email protected]> wrote:
> >
> > > > > > > I cant undestand why this config:
> >
> > > > > > > <Connector protocol="AJP/1.3" port="0"
> channelNioSocket.port="8009"
> > > > > > > channelNioSocket.maxThreads="
> > > > > > > 150"
> > > > > > > channelNioSocket.maxSpareThreads="50"
> > > > > > > channelNioSocket.minSpareThreads="25"
> > > > > > > channelNioSocket.bufferSize="16384" />
> >
> > > > > > > Why port 0 ?
> >
> > > > > > > 2010/2/20 dablack <[email protected]>
> >
> > > > > > > > Fran,
> >
> > > > > > > > I'm really not an expert on setting up the connection between
> Apache
> > > > > > > > and Tomcat but I believe that the connections only operate in
> two
> > > > > > > > protocols: http or ajp. NIO is a Tomcat connector that can
> > > > > communicate
> > > > > > > > in either http or ajp. Because of that, I don't see any
> reason that
> > > > > > > > the Apache mod_jk connector module couldn't communicate with
> the NIO
> > > > > > > > connector. Since I don't use an NIO connector I could be
> wrong, but
> > > > > > > > that is the way I see it. As a starting point, try these
> > > > > > > > configurations:
> >
> > > > > > > > server.xml
> >
> > > > > > > > <Connector protocol="AJP/1.3" port="0"
> channelNioSocket.port="8009"
> > > > > > > > channelNioSocket.maxThreads="150"
> > > > > > > > channelNioSocket.maxSpareThreads="50"
> > > > > > > > channelNioSocket.minSpareThreads="25"
> > > > > > > > channelNioSocket.bufferSize="16384" />
> >
> > > > > > > > (You seem to want to use port 8081 which should be fine, but
> the
> > > > > > > > standard ajp port is 8009. However, I wouldn't put it on port
> 8081 if
> > > > > > > > you are already using that port for another protocol such as
> http.)
> >
> > > > > > > > httpd.conf
> >
> > > > > > > > LoadModule jk_module modules/mod_jk.so
> > > > > > > > JkWorkersFile conf/workers.properties
> > > > > > > > JkLogFile /var/log/httpd/mod_jk.log
> > > > > > > > JkLogLevel info
> > > > > > > > JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
> > > > > > > > JkOptions +ForwardKeySize +ForwardURICompat
> -ForwardDirectories
> > > > > > > > JkRequestLogFormat "%w %V %T"
> > > > > > > > JkMount /*.svc ajp13
> >
> > > > > > > > (For the JkMount I use *.svc because I use the svc extension
> for my
> > > > > > > > GWT service target to differentiate service points from other
> files.)
> >
> > > > > > > > (For example, in my web.xml file I will map my service
> something like
> > > > > > > > this to use the svc extension:
> > > > > > > > <servlet>
> > > > > > > > <servlet-name>MyService</servlet-name>
> > > > > > > >
> <servlet-class>com.mysite.server.MyServiceImpl</servlet-class>
> > > > > > > > </servlet>
> > > > > > > > <servlet-mapping>
> > > > > > > > <servlet-name>MyService</servlet-name>
> > > > > > > > <url-pattern>/MyService.svc</usr-pattern>
> > > > > > > > </servlet-mapping>
> >
> > > > > > > > And in my GWT code I create my service target like this:
> >
> > > > > > > > target.setServiceEntryPoint( GWT.getModuleBaseURL() +
> > > > > > > > "MyService.svc" );
> >
> > > > > > > > I did this because I had troubles with the 'JkMount /* ajp13'
> entry,
> > > > > > > > but when I used /*.svc the problems cleared up.)
> >
> > > > > > > > workers.properties
> >
> > > > > > > > workers.tomcat_home=/usr/tomcat/apache-tomcat-6.0.16
> > > > > > > > workers.java_home=/usr/java/jdk1.6.0_06
> > > > > > > > worker.list=ajp13
> > > > > > > > worker.ajp13.port=8009
> > > > > > > > worker.ajp13.host=localhost
> > > > > > > > worker.ajp13.type=ajp13
> > > > > > > > worker.ajp13.lbfactor=1
> > > > > > > > worker.loadbalancer.type=lb
> > > > > > > > worker.loadbalancer.balance_workers=ajp13
> >
> > > > > > > > (Of course, match the ajp port in the workers.property file
> to the
> > > > > > > > port you define in the server.xml file. Also, if your Tomcat
> is not
> > > > > on
> > > > > > > > the same server as your Apache you will need to change the
> localhost
> > > > > > > > host entry to the correct host URL.)
> >
> > > > > > > > I hope this helps.
> >
> > > > > > > > On Feb 20, 12:51 pm, Fran <[email protected]> wrote:
> > > > > > > > > Bad notices.
> >
> > > > > > > > > This dude has same problem that me and he was answered that
> mod_jk
> > > > > > > > > dont support NIO
> >
> > > > > > > > >
> http://www.mail-archive.com/[email protected]/msg67701.html
> >
> > > > > > > > > :(
> >
> > > > > > > > > On 20 feb, 19:13, Fran <[email protected]> wrote:
> >
> > > > > > > > > > Here the error when I change the protocol of
> > > > > > > > > > protocol="org.apache.coyote.http11.Http11NioProtocol" to
> > > > > protocol="AJP/
> > > > > > > > > > 1.3"
> >
> > > > > > > > > > [error] jk_ajp_common.c (1962): (worker1) Tomcat is down
> or
> > > > > refused
> > > > > > > > > > connection. No response has been sent to the client (yet)
> > > > > > > > > > [error] jk_ajp_common.c (2466): (worker1) connecting to
> tomcat
> > > > > failed.
> >
> > > > > > > > > > On 20 feb, 19:04, Fran <[email protected]> wrote:
> >
> > > > > > > > > > > Should be due to protocol.
> > > > > > > > > > > My messenger seems that need NIO protocol but if I put
> the NIO
> > > > > > > > > > > protocol MOD_JK cant conect apache with tomcat
> >
> > > > > > > > > > > If I use this tomcat config, mod_jk cant conect apache
> with
> > > > > tomcat.
> >
> > > > > > > > > > > <Connector
> > > > > > > > > > > connectionTimeout="20000"
> > > > > > > > > > > port="8081"
> > > > > > > > > > > protocol="org.apache.coyote.http11.Http11NioProtocol"
> > > > > > > > > > > maxThreads="5"
> > > > > > > > > > > acceptorThreadCount="2"
> > > > > > > > > > > redirectPort="8443"
> > > > > > > > > > > socket.directBuffer="false" />
> >
> > > > > > > > > > > If I use the last tomcat config, tomcat cant execute
> the
> > > > > servlet.
> > > > > > > > > > > This is the error log:
> >
> > > > > > > > > > > GRAVE: Error, processing connection
> > > > > > > > > > > java.lang.IndexOutOfBoundsException
> > > > > > > > > > > at java.io.BufferedInputStream.read(Unknown
> Source)
> > > > > > > > > > > at
> > > > > > > >
> org.apache.jk.common.ChannelSocket.read(ChannelSocket.java:620)
> > > > > > > > > > > at
> > > > > > > >
> org.apache.jk.common.ChannelSocket.receive(ChannelSocket.java:577)
> > > > > > > > > > > at
> >
> > > > >
> org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:
> > > > > > > > > > > 685)
> > > > > > > > > > > at org.apache.jk.common.ChannelSocket
> > > > > > > > > > > $SocketConnection.runIt(ChannelSocket.java:889)
> > > > > > > > > > > at org.apache.tomcat.util.threads.ThreadPool
> > > > > > > > > > > $ControlRunnable.run(ThreadPool.java:690)
> > > > > > > > > > > at java.lang.Thread.run(Unknown Source)
> >
> > > > > > > > > > > On 20 feb, 18:49, Fran <[email protected]> wrote:
> >
> > > > > > > > > > > > I am configuring mod_jk
> >
> > > > > > > > > > > > For tomat, in server.xml :
> >
> > > > > > > > > > > > <Connector
> > > > > > > > > > > > connectionTimeout="20000"
> > > > > > > > > > > > port="8081"
> > > > > > > > > > > > protocol="AJP/1.3"
> > > > > > > > > > > > maxThreads="5"
> > > > > > > > > > > > acceptorThreadCount="2"
> > > > > > > > > > > > redirectPort="8443"
> > > > > > > > > > > > socket.directBuffer="false" />
> >
> > > > > > > > > > > > For apache, in httpd.conf :
> >
> > > > > > > > > > > > <IfModule mod_jk.c>
> > > > > > > > > > > > JkWorkersFile
> "/etc/httpd/conf/workers.properties"
> > > > > > > > > > > > JkLogFile "/etc/httpd/logs/mod_jk.log"
> > > > > > > > > > > > JkLogLevel warn
> > > > > > > > > > > > JkMount /msn/* worker1
> > > > > > > > > > > > </IfModule>
> >
> > > > > > > > > > > > The result is that the url:
> > > > > > > >http://localhost/msn/Messenger.htmlworks,
> > > > > > > > > > > > but this html conects to servelet that not work.
> > > > > > > > > > > > The mod_jk runs html in apache that fisically are in
> tomcat,
> > > > > but
> > > > > > > > cant
> > > > > > > > > > > > run the servlet.
> >
> > > > > > > > > > > > ¿Is necesary some special configure?
> >
> > > > > > > > > > > > Thanks!
> >
> > > > > > > > > > > > On 20 feb, 16:04, Fran <[email protected]> wrote:
> >
> > > > > > > > > > > > > Ok, thanks.
> >
> > > > > > > > > > > > > I will test mod_jk. I
> >
> > ...
> >
> > read more »
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-web-toolkit%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.