1

I'm trying to run a Jetty Server that can have a number of people connect to the server and see a list of print outs. I want everybody who connects to see the same values printed out.

For instance, if I have a single list keeping track of the time and I want 5 or so people to be able to go to my website (e.g. localhost:8080/time) and have them all see what time it is every 30 seconds, how would i set that up?

What I have:

  • I am using Jetty.
  • I created a single server bound to port 8080.
  • I created my own handler that extends AbstractHandler
    • this writes to the screen saying when an event has transpired (i.e. 30 seconds have passed)
  • If two people connect to this page, they each see a print out every minute (that is it switches back and forth letting each person know when every other event has transpired)
  • If 3 people connect, only two can stay connected and the third just spins getting no output to the screen

I have not set up an Connectors of my own since my attempts to do so have been unsuccessful and i'm not sure how I understand if that is the solution to my problem.

Any help would be much appreciated and if anybody has some idea but needs some clarification on what I am doing I would be glad to give more details.

Thanks!

Handler code:

@Override
public void handle(String target, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException
{

httpServletResponse.setContentType("text/html;charset=utf-8");
httpServletResponse.setStatus(HttpServletResponse.SC_OK);

request.setContextPath("/time");
request.setHandled(true);

while (true) {

    synchronized(Main.list) {
         while (!Main.list.isEmpty()) {
              Double time = Main.list.get(0);
              httpServletResponse.getWriter().println("<h1>The time now is " + time + "</h1>"); 
              httpServletResponse.flushBuffer();    
              Main.list.remove(0);
         }
         try {
              Main.list.wait();
         } catch (InterruptedException e) {
              e.printStackTrace();
         }
    }

 }

So the list object is a static ArrayList defined in the Main class that I wake up (i.e. notify) every 30 seconds. Hopefully this helps someone understand more what I am talking about as i'm not sure what I could change in the handler...

2
  • I think you need to post some code. You shouldn't need to play with connectors - you problem is almost certainly in the way you've set up your handler, but your description doesn't give us enough to work from.
    – Tim
    Feb 28, 2012 at 13:03
  • @Tim I just edited the question to include the handler code. if it gives you any ideas please let me know, thanks for taking the time to look at it!
    – abroad
    Feb 28, 2012 at 13:57

1 Answer 1

0

How are you feeding clients into your handler? Browsers have limits to the number of connections are made to to a particular host, perhaps your seeing that.

there is nothing intrinsically wrong that handler code aside from it being a generally odd thing to see in a handler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.