Risk, if any, of closing ServletOutputStream using a user thread?

Hello All,
What is the risk of writing/flushing/closing ServletOutputStream using a user thread other than the HTTP thread associated with the request?
I want to do that to implement client request timeouts. Currently in our application, for every request received we are creating a new thread to make the HTTP thread timeout after a configurable amount of time is elapsed. Though creating a new thread is doing the job just fine but that approach is creating another problem that the servers are not scaling to the load we receive. We frequently get "unable to create threads due to unavailable memory" errors. At any point in time, any of our servers receive ~200 requests, which means that server has to create/handle 400+ threads. If the servers are slow due to external systems, then a lot more threads will be accumulated in the server. When the thread count reaches 500+ it fails to accept any further requests. So as a possible alternative to current approach, I am thinking of running few threads (may be a couple of threads) in the background and timeout the requests by closing the associated ServletOutputStream with TimeoutException.
Note: I know that as another possible solution, I can go for few more servers in the cluster but before that I want to check if the above approach is a possible solution because it is less complex and saves money on the hardware.
Thanks,
Srinivas

Hello Kaj,
Sorry for the cross-post. Actually, I couldn't decide if I should post it as a concurrency, Java Servlet or as a design question.
Maxideon,
Sure, it can be done with single thread also. I said a couple of threads because a single thread may not get sufficient CPU time to handle all timeouts. But otherwise yes, we can try it with single thread also.
ejp,
We do not own the client application. We are a web services application and there are many clients who consume our services.
The standard solution to handle timeouts in the server is by creating an additional thread, which is what we have followed. Now, that design is posing a new problem to us.
No. A custom TimeoutException.
I want to use 2 or 3 user threads to timeout the request if needed. That way I do not have to create 1 thread for each request. In the current approach server creates/handles 400 threads for 200 requests. With the alternative solution I can make it to create/handle 202 or 203 threads for 200 requests.
dubwai,
Here I am answering to your reply to my same question in the other forum.
The current design is working just fine for timeouts. But sometimes it is failing to scale to the load we receive. In the current design we have created 1 thread for each request to timeout HTTP thread, which means that for 200 requests we will have 400 threads (200 HTTP threads and 200 user threads) in the server. For some reason, if the load increases to 250 requests or if external systems are slow then more threads accumulate in the server and it fails to accept incoming requests when thread count reaches ~500. That is the problem we are having.
In the alternative design I proposed that we will create just a couple (may be 2 or 3) of threads running in the background, instead of 1 thread for each request. These background threads monitor HTTP threads and timeout the request if needed by writing and closing ServlerOutputStream. This way server will have to create/handle only ~200 HTTP threads plus a couple of user threads. For this approach to work I have to close ServletOutputStream using background threads instead of HTTP thread and want to know what is the rick of doing that?
To All,
Here is a rough implementation of user thread. The HTTP thread registers itself in threadMap variable with its starttime and in threadResponse variable with its associated HttpServletResponse. This user thread uses these values to find request timeout (10 seconds) and close response if needed.
public class TimeoutMonitor implements Runnable {
    public static Map<Thread, Long> threadMap = new HashMap<Thread, Long>();
    public static Map<Thread, HttpServletResponse> threadResponse =
        new HashMap<Thread, HttpServletResponse>();
     @Override
     public void run() {
         while(true) {
          try {
              wait(1000);
              Set<Thread> keys = threadMap.keySet();
              Iterator<Thread> threads = keys.iterator();
              while(threads.hasNext()) {
                  Thread thread = threads.next();
                  Long startTime = threadMap.get(thread);
               if((System.currentTimeMillis() - startTime) > 10000) {
                   HttpServletResponse response =
                                threadResponse.get(thread);
                   try {
                    PrintWriter writer = response.getWriter();
                    writer.write("Timeout Exception");
                    writer.flush();
                    writer.close();
                    threadMap.remove(thread);
                    threadResponse.remove(thread);
                   } catch (IOException e) {
                    e.printStackTrace();
          } catch (InterruptedException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
    }I have test run this program in tomcat and seem to be working fine. Please note that here response/writer are closed by user thread and not HTTP thread. My questions is, do you see any problem in doing that?
Thanks,
Srinivas

Similar Messages

  • How to use postlv user event in labview

    Hi sir 
     I am sending data labwindow cvi dll call to labview,for that we need postlv user event.please any one knows to use postlv user evenet in labview.
    Below will  be the  structureand i need to update in labview .
    typedef struct
    unsigned
    uDSPIO,
    // uDAQMode,
    // bControllerActive,
    bShutDown,
    bPowerOn,
    bPowerHi,
    bPowerFault,
    bSGIdle,
    bSGHold,
    uBitsIn,
    uBitsOut,
    uCycleCount,
    uCycResidue,
    uBlockCount,
    uLogChannels,
    uCurrentMode,
    uAsigndPhysCh[LOG_CHANNELS];
    float
    fSetPoint,
    fDAQRate,
    fRange[LOG_CHANNELS],
    fReadout[LOG_CHANNELS],
    fMax[LOG_CHANNELS],
    fMin[LOG_CHANNELS];
    char
    sChID[LOG_CHANNELS][32],
    sChUnits[LOG_CHANNELS][16];
    CTRL_CHANNEL_STATUS;
    Attachments:
    GDSCallback.vi ‏12 KB

    AThe fixed size arrays in your structure are inlined byt the C compiler. This means they are not an array pointer (and definitely never a LabVIEW array handle) but rather LOG_CHANNELS amount of unsigned ints and floats directly embedded in the structure. In LabVIEW terms this is most easily represented as a cluster with LOG_CHANNELS elements inside the main cluster.
    So basically your uAsigndPhysCh needs to be a cluster with LOG_CHANNELS elements of unsigned ints and  fRange,  fReadout, fMax and fin are clusters with LOG_CHANNELS single precision float values, and then sChID is a cluster with 32 * LOG_CHANNELS chars and sChUnits one with 16 * LOG_CHANNELS chars.
    However I would personally rethink this strategy. This huge cluster monster is passed into PostLVUserEvent() each time and copied into the event data on every event, causing quite a bit of overhead that way. Is it really necessary to post all this data every time?
    Also you have commented out uDAQMode and bControllerActive in your C structure but included it in the LabVIEW cluster which will certainly throw off your data. Last but not least the b prefix would probably indicate that you are only using that element as a boolean value, so why not consider to use bit fields there? In LabVIEW you can easily seperate those bitfields by either translating the resulting uInt32 into a boolean array or even more efficiently use boolean logic to detect the bits.
    And in order to let LabVIEW react on a user event you have to create that user event with the correct datatype (your cluster monster) and then pass the user event refnum to your DLL. This DLL then calls PostLVUserEvent() with the user event refnum as first parameter and a pointer to a data structure that matches the LabVIEW user event datatype EXACTLY whenever it wants to post an event to LabVIEW. The user event is also registered in an event structure in LabVIEW which will then get triggered everytime your DLL posts an event by correctly calling PostLVUserEvent().
    Rolf Kalbermatter
    CIT Engineering Netherlands
    a division of Test & Measurement Solutions

  • I all of you , any time that i use my Itune by closing i need to force all my system , do you have a solution to propose ?

    This is my first time that i use apple support communities .. since a few months i got the same problem in my laptop .. any time that i use my Itune and i normally close it .. than i need to force totally the closing of my pc .. that s happend only when i use Itune !!! do you have any suggestion to solve the problem ??
    Thanksssss GB

    You can buy Microsoft Office for Mac, it includes Word, Excel and Powerpoint. Or you can download a free copy of LibreOffice which contains substitutes (good ones) for the Microsoft Office programs.
    Do not even think of using Boot Camp unless you have a complete and current TM backup or a clone to restore if it goes wrong. You will also need to buy Windows and Microsoft Office for it. And you MUST read the directions very carefully.

  • Is it ok to keep PB closed while using an LCD display?

    Hello,
    I was wondering if it is "ok" to keep my Powerbook display closed while using my LCD display. Are there any advantages/disadvantages? Is it better for the screen life of the PB? Is it keeping heat in? Any info would be appreciated. Thanks much.
    John

    I would have a tendency to agree with Tom Fussy, as I also noticed this page from Apple:
    http://www.apple.com/powerbook/graphics.html
    This page most specifically speaks to using the system with the lid closed, and so I figure it must be okay. Personally AppleCare is on my system, so if it fries following what is posted on the Apple Website, it concerns me not, as I would just take it into Apple for repair. However, I understand a person would not want to do something he knew would cause a failure to the hardware anyway, if not for the minimum of an inconvenience of being without the laptop while it is being repaired.
    You might also wish to check this URL out as well:
    http://binaervarianz.de/projekte/programmieren/meltmac/
    Stuart

  • I want to buy a new apple tv but it used hdmi cables and my house is only wired for analog.  Is there any way I can use the apple tv on analog cables?

    I want to buy a new apple tv but it used hdmi cables and my house is only wired for analog.  Is there any way I can use the apple tv on analog cables?

    Welcome to the Apple Community.
    It's do-able, but I don't think it's a great idea.
    DVI
    Some users with DVI have managed to get their TV's to work with DVI-HDMI cables. DVI carries no audio, so alternative connections need to be explored to enable audio. DVI doesn't necessarily support HDCP as well as other standards used by HDMI (which may or may not be an issue)
    Analogue
    There are hardware converters that will convert HDMI to various other types of output, however there are some issues with doing so that you should be aware of.
    HDCP
    HDCP compliant converters will not allow you to watch HDCP protected content such as that from the iTunes Store. Non compliant converters exist but we cannot discuss them under the Terms of Use for these communities.
    Resolution and aspect ratio
    I'm not aware of any converters that will scale the output from the Apple TV, any TV or projector which is used will need to be widescreen and support resolutions of 720p (Apple TV 2), 720p/1080p (Apple TV 3)
    DAC
    DAC (Example Only - Not a recommendation or suggestion that this is suitable in your circumstances)

  • I've seen all the posts for fixing the wifi on the 4s, none of them work!  So, who can tell me why my iPhone 4S cannot find any wifi when it used to easily, and when it does it can never connect?

    I've seen all the posts for fixing the wifi on the 4s, none of them work!  So, who can tell me why my iPhone 4S cannot find any wifi when it used to find wifi, and when it does find a wifi network it can never connect?  You think apple would have a clue how to fix it!  Because turning the wifi on and off doesn't work, resorting network settings doesn't work, even restoring phone to factory default doesn't work...

    Did not work. I've selected iMessage to ON and left it. After a few hours I recieved a message "activation unsuccessful. Turn on iMessage to try again". This has been going on for the past 3 days.

  • How can I restore my data from iCal? I didn't make any backup, but I use  time machine with an external HD. I deleted iCal when I deleted my gmail account. I have tried to restore, but I can only restore the iCal software and not the data.

    How can I restore my data from iCal? I didn't make any backup, but I use  time machine with an external HD. I deleted iCal when I deleted my gmail account. I have tried to restore, but I can only restore the iCal software and not the data.

    So what is your question?
    If you forgot your encryption password:
    Warning: Make sure it's a password you will remember or write it down for safekeeping. If you encrypt an iPhone backup in iTunes and forget your password, you can't restore from backup and your data will be unrecoverable.
    If you can't remember the password and want to start again, you must perform a full software restore and chooseset up as a new device when iTunes prompts you to select the backup from which to restore.
    The above comes from here:
    http://support.apple.com/kb/HT4946

  • I can't seem to get my iMac (late 2009 model) into automatic sleep mode.  If I manually put it into sleep I don't have any issues.  I used to solve the issue by running "PleaseSleep" application but that doesn't work anymore since Lion.

    I can't seem to get my iMac (late 2009 model) into automatic sleep mode.  If I manually put it into sleep I don't have any issues.
    I used to solve the issue by running "PleaseSleep" application but that doesn't work anymore since Lion. I now want to fix the underlying problem.
    I'm not running any weird background processes and in my energy saver settings I've tagged "put the hard disk to sleep when possible:, "allow power button to put computer to sleep" and "automatically reduce brigthness". All pretty standard.
    Is there anyone who can give me some pointers ?

    Today I solved the same problem for my iMac running Snow Leopard. See https://discussions.apple.com/thread/3008791#15947706. The method may help you, too.
    For me it was the DynDNS Updater preventing my iMac from automatically entering sleep mode.
    To my knowledge the cause of this sleep problem can only be a peripheral device or a process. So I suggest to first unplug all peripherals and test whether that's the cause. If not, I suggest to terminate one process after another and to test automatic entering of sleep mode after each. Start with user processes; continue with system process if necessary.
    At least that's the way I found the offending process. Fortunately, I was able to change the configuration of that process to allow again automatic entering of sleep mode.
    Good luck!

  • Any way to disable "Use Relative Dates" everywhere in Finder?

    Is there any way to make Use Relative Dates to NOT be on by default in Finder?
    I just hate the yesterday, today, etc. crap. I want to just see the file dates. Period. I don't want to have to figure out what the date was yesterday to do a file date comparison.
    Yes, you can turn it off but each time I check Use as defaults and open a new folder in finder, the relative dates are back! Argggh!
    Thanks for any help on this.

    Yes its very easy, with finder open, finder bar at top see View, "drop down box" Show View Options, remove the check mark for use relative dates.

  • The Java plugin has created a time unfriendly issue with my e-mail account, I can not open or send any e-mail without using this plugin, how can I eliminate this problem without dropping m. firefox?

    A few days ago I tried to e-mail a friend, when I started A box came up and it required me to add a plugin..Java. I contacted my e-mail courier and microsoft, they told me that is was a plugin from Mozilla Firefox. Since then I must go through Java to send or open an e-mail... this is NOT to my satisfaction, it takes too long for all of this to open and if it is not cleared , I am dropping Mozilla and going elsewhere. Why has this occurred? Is this a result of the last update? If so can I delete the last update and continue on the other version?
    == This happened ==
    Every time Firefox opened
    == last week

    Sorry, I have no idea what you mean by '''"The Java plugin has created a time unfriendly issue with my e-mail account, I can not open or send any e-mail without using this plugin,"'''.
    If you think your problem is caused by the Java plugin, you should visit the Java support forum for assistance.
    http://forum.java.sun.com/index.jspa
    If you could explain what you mean by '''"time unfriendly''' issue with my e-mail account", maybe we'll be able to help you solve that issue.
    It might be helpful for us if we knew exactly which web-mail provider you are using. It seems strange to me that viewing or sending web-mail would actually "need" Java for operation, its more likely that some other item on the web page needs Java to run some advertising or other garbage that isn't a necessary part of the web-mail functionality you want to use.

  • Is there any way I could use my own apple ID to buy in-app purchases even if I used my aun'ts apple ID to install the app?

    I have an Apple Account registered in the U.S & no credit card linked (since im under 18). I always use my Aunt's account because I buy in-app purchases and other stuffs in the app store, I also used her account to download the app I play but she changed her password and gifted me a gift card to redeem in my own apple ID...
    But whenever I wanted to buy something from the game it asks for her password and not mine, I changed the logged in apple account in the iTunes & Apple Store hoping not to re-install the app because I achieved so much in the game..
    Is there any way I could use my own apple ID to buy in-app purchases even if I used my aun'ts apple ID to install the app?

    No, all apps are tied to the account that downloaded them, so only that account will be able to make in-app purchases within them - you would need to delete the game (which would also delete your progress/settings in it) and buy/download it with your account to be able to make in-app purchases with your own account.

  • I tried to launch iCloud on iMac.  I Changed my e-mail address recently, and iMac  doesn't  seem to know it.  Even worse, it doesn't know any password I've used with Apple ID.  Can anyone help me?

    I tried to launch iCloud on iMac.  I Changed my e-mail address recently, and iMac  doesn't  seem to know it.  Even worse, it doesn't know any password I've used with Apple ID.  Can anyone help me?

    No, i don't. if i did i would have tried it. do you have to sign up an appled ID for iCloud? If the answer is yes then they should mention that in the set-up instructions.

  • I don't have a wireless keyboard or mouse for my 2007 iMac, is there any way that can use remote desktop and access it from my 2010mbp.

    i don't have a wireless keyboard or mouse for my 2007 iMac, is there any way that can use remote desktop and access it from my 2010mbp

    Hi champrider,
    You can use an application such as Apple Remote Desktop to control your iMac remotely. See this article -
    OS X Mavericks: Allow access using Apple Remote Desktop
    This help page will provide you with some other useful resources for Apple Remote Desktop -
    Remote Desktop Help
    Thanks for using Apple Support Communities.
    Best,
    Brett L 

  • I have Acrobat 9.5 and when I try to create pdf from scanner, it displays an error "Adobe acrobat has stopped working". Is there any way I can use this functionality?

    I have Acrobat 9.5 and when I try to create>pdf from scanner, it displays an error "Adobe acrobat has stopped working". Is there any way I can use this functionality?

    Provide you have Acrobat 9 installed in an OS for which Acrobat 9 is compatible then you can use the functionality.
    As well, you need a scanner connected, powered up, and the software installed. Acrobat 9 "prefers" TWAIN drivers.
    Always visit the scanner vendor's site and download - install the latest greatest software.
    n.b., The Acrobat 9.x product family passed into "End of Support" mid-year 2013.
    As well the Acrobat 9.x product family is not compatible with contemporary OSs.
    Be well...

  • Is there any limitation of memory uses for apps in iPad2 or iPad3 ?

    Hi,
       Is there any limitation of memory uses for apps in ipad2 or ipad3 .For example an application can use 10% or 20% of available memory , is there anything like that or the apps can use the total available memory or like 90% of it ?
    I want to know about the memory distribution to the apps in ios .
    Would anyone suggest a right way ?
    Thanks

    iOS manages all memory. You should test your app in Instruments to be sure it is playing nice.
    See Advanced Memory Management Programming Guide

Maybe you are looking for

  • Web page in iframe does not maintain scroll position on refresh in FF4

    I have a web page on my local drive which refreshes live sports commentary every minute in an iframe. In FF3.6.x the page in the iframe retained the scroll position of where I had moved to. In FF4 the page within the iframe goes back to the top so I

  • Support for language

    hello, Does HTMLDB support Chinese Language ? thanks sanjay

  • ATG 10 CIM Confoguration Error

    Can anyone pls help. Getting the following error in CIM console. while configuring Publishing Server using ATG CIM. All the other servers are configured without any error. An unexpected error has occurred. Property #targetName can not occur more than

  • C++ Compiler aborts with licensing error message

    SUN OS - 5.6 with CC compiler version : >>> Sun WorkShop 6 update 1 Compilers C++ >>> fails after a while when trying to do a Make of few C++ files , the error message is : >>> License Error : Licensing product (Sun WorkShop Compiler C++ SPARC). Lice

  • I need select a frameGrabber for jmf, please.

    Hi. I'm student, and I need a FrameGrabber compatible with java media framework, (for example Pinnacle Studio AV/DV, or Matrox AV/DV). The important is : as which type serves to me ?. I want a recommendation. PS : the price, between US$50 to US$130.