Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Ajax polling / message queues

Options
  • 28-05-2014 11:35pm
    #1
    Registered Users Posts: 21


    Anyone able to help with this issue I posted earlier?

    stackoverflow.com/questions/23915838/is-it-even-possible-to-make-this-loop-wait-a-few-seconds-each-time

    Basically I've been told by a friend that I need to not use java to stop for lets say a second for every domain (loop), instead I need to use ajax polling and message queues to call the java instead if that makes sense?

    I'm desperately lost and only know a tiny bit of ajax.

    The socket I'm using only allows 5 queries per second which is why I'm having this issue of needing to slow down my calls.

    Anyone any ideas?


Comments

  • Registered Users Posts: 21 Loltube


    Sorry, boards won't allow me to post a url directly


  • Registered Users Posts: 18 jonster


    Tried thread.sleep(1000) ?


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    It would be easier if you posted your code here instead of linking to your stackoverflow post - you'll get more responses that way.


  • Registered Users Posts: 21 Loltube


    .sleep wouldn't work. Tried just about everything in java!

    Evil Phil - Sorry, on my mobile so had to just post the link.


  • Registered Users Posts: 2,030 ✭✭✭colm_c


    I'm not familiar with the detail in java, but the theory should be sound.

    Use some kind of background job system, which manages the job queue and runs these jobs. The jobs should pushes a status message to somewhere (db, memcached, redis), and then you simply poll for the status message.

    If you set it up right, this should also allow multiple users to run the same job (with different config).


  • Advertisement
  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Can you give us more information on how Thread.sleep didn't work: simply didn't sleep, didn't progress after sleep, exception thrown, etc?


  • Registered Users Posts: 27,057 ✭✭✭✭GreeBo


    I think you need to break the task down a bit.

    Your initial request will probably timeout, so the first request that passes in the list of domains should kick off a new thread/process to start the domain checking code, it should return a jobId etc immediately to the user.
    You can keep track of the status of the domain checker by (for example) putting the results for each domain into some object and keeping that object in a Map indexed by the jobId you returned.

    Then to get a status you make a call passing in the jobId and it returns the results from the Map (meanwhile the new thread is processing the domains you originally sent in).

    similar example

    If your backend only allows 5/qps then you need to track this at a global level and not just at a thread level (otherwise if you get more than 5 active requests you might exceed your 5qps, even with a sleep) So you should have a global thread manager that only allows 5 concurrent requests.

    Finally, you say its not working...whats not working about your Thread.sleep() approach?

    Also, doing the sleep in your servlet will tie up connections unneccessarly, another reason to move the processing out of the servlet.


  • Registered Users Posts: 21 Loltube


    Can you give us more information on how Thread.sleep didn't work: simply didn't sleep, didn't progress after sleep, exception thrown, etc?

    Get a message from the socket saying that I've gone over the 5 queries a second. Even when sleeping for a period of time. Still on my mobile right now so sorry I can't go further in to detail


  • Registered Users Posts: 21 Loltube


    GreeBo wrote: »
    I think you need to break the task down a bit.

    Your initial request will probably timeout, so the first request that passes in the list of domains should kick off a new thread/process to start the domain checking code, it should return a jobId etc immediately to the user.
    You can keep track of the status of the domain checker by (for example) putting the results for each domain into some object and keeping that object in a Map indexed by the jobId you returned.

    Then to get a status you make a call passing in the jobId and it returns the results from the Map (meanwhile the new thread is processing the domains you originally sent in).



    If your backend only allows 5/qps then you need to track this at a global level and not just at a thread level (otherwise if you get more than 5 active requests you might exceed your 5qps, even with a sleep) So you should have a global thread manager that only allows 5 concurrent requests.

    Finally, you say its not working...whats not working about your Thread.sleep() approach?

    Also, doing the sleep in your servlet will tie up connections unneccessarly, another reason to move the processing out of the servlet.

    Thanks for the info. I'm just a bit lost as to why the sleep doesn't actually slow down my socket connections. But ice never done anything related to threads before in college so maybe it's just me.


  • Registered Users Posts: 10,491 ✭✭✭✭28064212


    Loltube wrote: »
    Get a message from the socket saying that I've gone over the 5 queries a second. Even when sleeping for a period of time. Still on my mobile right now so sorry I can't go further in to detail
    That doesn't necessarily tell you that sleep isn't working. Use sleep in your loop, but log a timestamp every time the loop runs.

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Advertisement
  • Registered Users Posts: 27,057 ✭✭✭✭GreeBo


    How many servlet requests are you making?

    As above, log each backend/socket call with a timestamp and check it yourself.


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    You'll probably (almost certainly for real world systems) have to move to a solution like that suggested by colm_c and GreenBo. Even if the Thread.sleep works, it will only keep rate limit the requesting thread, and not threads serving other requests to the same action. A couple concurrent requests, and you'll be over your limits.

    You need to queue all queries, and have a task that processes this queue. There is multiple ways to implement this kind of system - what suits best will depend on a lot factors.


  • Registered Users Posts: 21 Loltube


    GreeBo wrote: »
    How many servlet requests are you making?

    As above, log each backend/socket call with a timestamp and check it yourself.

    One request to my servlet. And the amount of domains can be up to 100 Let's say (5 allowed per second)


  • Registered Users Posts: 21 Loltube


    You'll probably (almost certainly for real world systems) have to move to a solution like that suggested by colm_c and GreenBo. Even if the Thread.sleep works, it will only keep rate limit the requesting thread, and not threads serving other requests to the same action. A couple concurrent requests, and you'll be over your limits.

    You need to queue all queries, and have a task that processes this queue. There is multiple ways to implement this kind of system - what suits best will depend on a lot factors.

    Thanks. Is ajax polling the best way to do this? It's just that I've never done anything like that before.

    Any way to do this in java would be brilliant.


  • Registered Users Posts: 27,057 ✭✭✭✭GreeBo


    Loltube wrote: »
    One request to my servlet. And the amount of domains can be up to 100 Let's say (5 allowed per second)
    Ok so then you need to add logging to the backend to confirm what TPS you are sending them. It may be an issue on their side, or perhaps its 5TPM for example.
    Loltube wrote: »
    Thanks. Is ajax polling the best way to do this? It's just that I've never done anything like that before.

    Any way to do this in java would be brilliant.

    You can do it the way you are doing it now, it all depends on what you want to do. Is this a real world system or just something you are trying out?

    If its going to be a production system then you need to implement a queueing system for incoming requests and have separate threads consuming this queue. You would need an over-arching solution to ensure you dont exceed the TPS you are allowed use.


  • Registered Users Posts: 21 Loltube


    GreeBo wrote: »
    Ok so then you need to add logging to the backend to confirm what TPS you are sending them. It may be an issue on their side, or perhaps its 5TPM for example.



    You can do it the way you are doing it now, it all depends on what you want to do. Is this a real world system or just something you are trying out?

    If its going to be a production system then you need to implement a queueing system for incoming requests and have separate threads consuming this queue. You would need an over-arching solution to ensure you dont exceed the TPS you are allowed use.

    Thanks for the info. It's a real world example (I'm building a site - just finished college). Don't mind sending the link on by PM if anyone is interested/


  • Registered Users Posts: 21 Loltube


    Thread t1 = new Thread(new Runnable() {
        public void run()
        {
             // code goes here.
        }});  
        t1.start();
    

    Wonder if this would make any difference


  • Registered Users Posts: 47 cregganna


    Don't know if it covers your use case but I've used Cometd, bayeuax, with 3.0 compliant Java servlet that supports suspendable requests such as jetty.

    This effectively gives you a publish/subscribe protocol between the server (web server) and client (javascript in browser). Under the covers it uses something called "long polling" which I was very sceptical about at the time but it appears to scale very well and was used by chess.com

    I think things have hardened and improved since I did it (about 2009?). In fact chess.com now uses websockets (see link below).

    Here's some links (doctored as I still can't post links :-|| )
    Jetty suspendable requests: http:// docs. codehaus. org/plugins/servlet/mobile#content/view/95420517
    Cometd: http:// docs. cometd. org/reference/
    chess.com announcement: http:// www. chess. com/forum/view/suggestions/new-live-chess-connection-type-websocket


  • Registered Users Posts: 47 cregganna


    cregganna wrote: »
    .....
    Apologies, please ignore my reply. I have just read your stackoverflow post and I'm completely off topic. Going to leave post in though to add to my posting count. This one helps too;-) Nearly at my 50.


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    cregganna wrote: »
    Apologies, please ignore my reply. I have just read your stackoverflow post and I'm completely off topic. Going to leave post in though to add to my posting count. This one helps too;-) Nearly at my 50.

    Don't think you are off-topic. OP may well want to use Comet so the frontend clients can be notified once their job is done.

    This, in combination with a background process the respects quotas + queue of domains inputted should get the job done.


  • Advertisement
  • Registered Users Posts: 18 jonster


    jonster wrote: »
    Tried thread.sleep(1000) ?

    Apologies for being so flippant, assumed this was a college project and didn't read the SO post to get the context of the issue.

    You're in much more experienced hands here on boards/dev than my Java101 suggestion.... *backs away slowly*


  • Registered Users Posts: 21 Loltube


    Anyone have any opinions on using the runnable interface? Saw that mentioned a few times when looking this up.


  • Registered Users Posts: 21 Loltube


    Actually just got a weird idea. If I make the method synchronised would that make any difference?


  • Registered Users Posts: 27,057 ✭✭✭✭GreeBo


    have u added logging yet?


  • Registered Users Posts: 27,057 ✭✭✭✭GreeBo


    Loltube wrote: »
    Actually just got a weird idea. If I make the method synchronised would that make any difference?

    Only if your problem is caused by multiple threads/requests which you have said it isn't.


Advertisement