Servlet到达BufferedReader时会超时吗?

时间:2013-03-07 19:36:51

标签: java jsp bufferedreader

编辑:所有这些代码在外部环境中运行良好(即在DEA12中它工作正常),但是当我部署它时,它会停留在bufferedreader上。

编辑二:因此,问题肯定在于缓冲重新编码器。如果我将URL更改为具有少量文本的内容(例如https://www.google.com),一切都很完美。我必须使用的URL有很多文本(例如:http://www.otc.edu/GEN/schedule/all_classes_fall.txt)。有人知道解决这个问题吗?

我的serlvet超时,通过我的日志我缩小了它正在发生的线。 servlet通过URL读取数据并解析它们,但是当它到达bufferedreader时它会超时(我已经在代码中注释了它,它就在切换之后):

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);

}

我的日志:

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

为什么会出现这种情况或者我如何对其进行故障排除的任何想法?

1 个答案:

答案 0 :(得分:1)

URLConnetion不会从此行之前的连接开始读取(流数据),

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

connection.getInputStream()将使连接对象开始从URL中读取数据。

您的服务器似乎无法访问该网址并超时。

您可能希望通过调用connection.setTimeOut()

来更改超时

尝试从服务器对这些网址执行PING,TRACE以验证您是否可以访问这些网址并且没有防火墙阻止

来自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.