2

编辑:所有这些代码在外部环境中都可以正常工作(即在 DEA12 中它工作得很好),但是当我部署它时,它会在缓冲读取器处停止。

编辑二:所以,问题当然在于缓冲阅读器。如果我将 URLS 更改为包含少量文本的内容(例如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

任何想法为什么会发生这种情况或我如何解决它?

4

1 回答 1

1

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

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

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

您的服务器似乎无法访问该 URL 并且正在超时。

您可能想通过调用更改超时connection.setTimeOut()

尝试PING,TRACE从服务器到这些 URL 以验证您可以访问这些 URL 并且没有防火墙阻止

来自 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.
于 2013-03-07T19:46:37.600 回答