com.ibm.websphere.samples.asynchbeans.wstrader.tickstream.web
Class StreamingServlet
java.lang.Object
javax.servlet.GenericServlet
javax.servlet.http.HttpServlet
com.ibm.websphere.samples.asynchbeans.wstrader.tickstream.web.BaseAsynchServlet
com.ibm.websphere.samples.asynchbeans.wstrader.tickstream.web.StreamingServlet
- All Implemented Interfaces:
- java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
public class StreamingServlet
- extends BaseAsynchServlet
This is a simple implementation of HTTP push. A HTTP client sends a HTTP request to this servlet and
we then push events back asynchronously using the HTTP response. We actually block the servlet engine
thread that received the request to do this. The servlet thread enters a loop in the StreamListener.sendMessages
method and this loop waits for an asynchronous event. When one arrives then it writes the event to the response
and then flushes it. The applet will then get it. Next, the sendMessages method simply loops around and waits
for another event. When the client logs out or times out then sendMessages is signaled and returns to this
servlet which then returns to the servlet engine.
Note, this is a sample. We are holding a thread per concurrent streamer using this implementation. This does not
scale very well. If you wanted to attach 1000s of clients to a server then you'd need 1000s of threads in the servlet
engine thread pool. There would be too much thrashing. There are better approaches than the simplistic one
shown here. For example, we may use a third party HTTP streaming product, the server job is then to push events
to this product when the occur and the product then pushes it to the client using HTTP. Lotus Sametime could be
used to do this sort of streaming as could other types of IM product.
Another alternative would be JMS tunnelled over HTTP. You may think that then the applet can talk directly to the
JMS pub/sub to get the tickers but you'd probably be surprised. Imagine we had a high volume feed sending 10s of
prices per second per ticker. An applet attaching over a slow connection would not be able to keep up. You
could use non persistent JMS and then the broker would start dropping messages ad hoc. However, you may also want
more control and do your own throttling of messages. A more useful approach in this scenarion would be to
hav a JMS client in the applet, possibly tunnelled over HTTP. The client makes a temporary queue and sends this
to the server. The server then pushes the events to the client by sending them to the queue. The server can do
additional filtering/processing on the events to tune the flow of events to the bandwidth of the client.
So, the point is this sample won't scale well. There are better approaches but the purpose here was simply
to demonstrate how HTTP push could be used.
- See Also:
- Serialized Form
Method Summary |
protected void |
doGet(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp)
|
protected void |
doPost(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp)
|
protected void |
doProcess(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp)
Handle all client requests. |
Methods inherited from class javax.servlet.http.HttpServlet |
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service |
Methods inherited from class javax.servlet.GenericServlet |
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
StreamingServlet
public StreamingServlet()
- Constructor for TickerStreamerServlet.
doGet
protected void doGet(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp)
throws javax.servlet.ServletException,
java.io.IOException
- Overrides:
doGet
in class javax.servlet.http.HttpServlet
- Throws:
javax.servlet.ServletException
java.io.IOException
- See Also:
HttpServlet.doGet(HttpServletRequest, HttpServletResponse)
doPost
protected void doPost(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp)
throws javax.servlet.ServletException,
java.io.IOException
- Overrides:
doPost
in class javax.servlet.http.HttpServlet
- Throws:
javax.servlet.ServletException
java.io.IOException
- See Also:
HttpServlet.doPost(HttpServletRequest, HttpServletResponse)
doProcess
protected void doProcess(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp)
throws javax.servlet.ServletException,
java.io.IOException
- Handle all client requests.
- Parameters:
req
- the client requestresp
- the response to the client
- Throws:
javax.servlet.ServletException
java.io.IOException