Jason Renaldo
Jason Renaldo

Reputation: 2822

Servlet will timeout when it reaches BufferedReader?

EDIT: All of this code works fine outside environment (i.e. in DEA12 it works perfect), but when I deploy it stalls at the bufferedreader.

EDIT TWO: So, the issue certainly lies with the bufferedreader. If I change the URLS to something with a small amount of text (say https://www.google.com) everything works perfect. The URLS I HAVE to use have a lot of text (example: http://www.otc.edu/GEN/schedule/all_classes_fall.txt). Does anybody know of a way around this?

My serlvet is timing out and via my logs I have narrowed down the line where it is taking place. The servlet reads in data via URLs and it parses them, however it is timing out when it reaches the bufferedreader (I have commented where in the code, it's right after the switch) :

private void loadAllClasses()
        throws IOException
{
    //Log beginning of load
    logger.info("Started loading classes at " + new Date());

    URLConnection connection = null;
    LinkedList<ClassInfo> currentList = null;
    final int NUMBEROFSEMESTERS = 3;
    final String SPLITONTAB = "\\t";
    final int STARTINDEX = 0;

    for(int counter = STARTINDEX; counter < NUMBEROFSEMESTERS; counter++)
    {
        //Change local fields for whatever semester we are in, there will always only be three semesters
        switch(counter)
        {
            //Build out the Fall classes
            case 0:
                currentList = null;
                try{
                    connection = this.urlFall.openConnection();
                    logger.info("Opened up Fall URL at " + new Date());
                }
                catch (Exception ex)
                {
                    logger.fatal("FATAL! COULD NOT OPEN GIVEN URL FOR FALL CLASSES!");
                }
                currentList = fallClassListings;
                break;
            //Build out the Spring classes
            case 1:
                currentList = null;
                try{
                    connection = this.urlSpring.openConnection();
                    logger.info("Opened up Spring URL at " + new Date());
                }
                catch (Exception ex)
                {
                    logger.fatal("FATAL! COULD NOT OPEN GIVEN URL FOR SPRING CLASSES!");
                }
                currentList = springClassListings;
                break;
            //Build out the Summer classes
            case 2:
                currentList = null;
                try{
                    connection = this.urlSummer.openConnection();
                    logger.info("Opened up Summer URL at " + new Date());
                }
                catch (Exception ex)
                {
                    logger.fatal("FATAL! COULD NOT OPEN GIVEN URL FOR SUMMER CLASSES!");
                }
                currentList = summerClassListings;
                break;
        }//end switch

        //Opening a URL Successful
        logger.info("Successfully opened URL, beginning parse at " + new Date());

        //!!!!IT HAPPENS HERE AS THE LOG BELOW WILL NEVER BE REACHED!!!!
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        logger.info("Bufferedreader opened at " + new Date());

        String line = reader.readLine();

        //This is what is reading through and parsing all of the text from the URL
        while(line != null)
        {
            //Log beginning of parse
            logger.info("Parsing next text line of current URL at " + new Date());

            //Keeps track of how big the array is
            int index = Integer.parseInt(properties.getProperty("FIRSTINDEX"));

            //Split data on tab character
            String[] data = line.split(SPLITONTAB);

            //Strip all the white space so everything doesn't turn out poorly formatted
            for(int arrayCounter = Integer.parseInt(properties.getProperty("FIRSTINDEX")); arrayCounter < data.length; arrayCounter++)
            {
                data[arrayCounter] = data[arrayCounter].trim();

                index++;
            }

            //ADD THE DATA TO THE ACTUAL CLASS INFO OBJECTS
            if(index == Integer.parseInt(properties.getProperty("MAXSIZEARRAY")))//Size of array was 14, which has all of the class information
            {
                //TEST CONDITION TO FIND A LAB, if the name is empty this is a new class. If it isn't it is
                //Supplementary data to the last class created.
                if(!data[Integer.parseInt(properties.getProperty("NAME"))].isEmpty())//REGULAR CLASS IF TRUE
                {
                    //Strip out empty space and make it say "N/A"
                    data = convertEmptySpace(data);

                    currentList.add(new ClassInfo(data));
                    logger.info("Added a class.");
                }
                else//THESE ARE LABS OR ADDITIONAL LECTURE TIMES, so add all the last information from the last class since it's the same.
                {
                    ClassInfo classForLab = new ClassInfo(data);

                    //Lab details are already set from the array, so fill the empty data correctly
                    classForLab.setSectionName(currentList.get(currentList.size()- Integer.parseInt(properties.getProperty("SUBTRACTTOP"))).getSectionName());
                    classForLab.setSectionSynonym(currentList.get(currentList.size()- Integer.parseInt(properties.getProperty("SUBTRACTTOP"))).getSectionSynonym());
                    classForLab.setSectionCredits(properties.getProperty("ZERO_OUT_INFO"));
                    classForLab.setSectionTitle(currentList.get(currentList.size()- Integer.parseInt(properties.getProperty("SUBTRACTTOP"))).getSectionTitle());
                    classForLab.setSectionCapacity(currentList.get(currentList.size()- Integer.parseInt(properties.getProperty("SUBTRACTTOP"))).getSectionCapacity());
                    classForLab.setSectionAvailableSeats(properties.getProperty("ZERO_OUT_INFO"));
                    classForLab.setSectionInstructor(currentList.get(currentList.size()- Integer.parseInt(properties.getProperty("SUBTRACTTOP"))).getSectionInstructor());
                    classForLab.setSectionMysteryVariable(currentList.get(currentList.size()- Integer.parseInt(properties.getProperty("SUBTRACTTOP"))).getSectionMysteryVariable());

                    //After everything is set, add lab to the class listings
                    currentList.add(classForLab);
                    logger.info("Added a lab.");
                }
            }

            //Log classes added
            logger.info("Done parsing text at " + new Date());

            //End of the current line.
            line = reader.readLine();
        }

        //Close the reader
        reader.close();
    }//All semester are loaded, add them to the master list as well

    logger.info("All classes were successfully retrieved via parsing at " + new Date());

    allClassListings.addAll(fallClassListings);
    allClassListings.addAll(springClassListings);
    allClassListings.addAll(summerClassListings);

}

My logs:

13:30:38,145 [TP-Processor18] INFO  Properties file was loaded successfully.
13:30:38,146 [TP-Processor18] INFO  URLs were successfully loaded at Thu Mar 07  13:30:38 CST 2013
13:30:38,146 [TP-Processor18] INFO  Started loading classes at Thu Mar 07 13:30:38 CST 2013
13:30:38,146 [TP-Processor18] INFO  Opened up Fall URL at Thu Mar 07 13:30:38 CST 2013
13:30:38,146 [TP-Processor18] INFO  Successfully opened URL, beginning parse at Thu Mar 07 13:30:38 CST 2013

Any ideas why this might be happening or how I might go about troubleshooting it?

Upvotes: 2

Views: 201

Answers (1)

Avinash Singh
Avinash Singh

Reputation: 3787

URLConnetion will not start reading (streaming data) from connection before this line,

BufferedReader reader = new BufferedReader(new 
              InputStreamReader(connection.getInputStream()));

connection.getInputStream() will cause the connection object to start reading data from the URL.

It seems that your server is not able to reach the URL and is timing out.

You may want to change the timeout by calling connection.setTimeOut()

Try doing a PING,TRACE from server to those URL's to verify you have access to those URL's and there is no firewall block

From JavaDocs -

>     openConnection()
>     ----------------------------> 
>     The connection object is created by invoking the openConnection method on a URL.
> 
>     getInputStream()
>     ---------------------------->
>     Returns an input stream that reads from this open connection.

Upvotes: 1

Related Questions