gpt4 book ai didi

java - Servlet 到达 BufferedReader 会超时?

转载 作者:行者123 更新时间:2023-11-30 11:35:07 26 4
gpt4 key购买 nike

编辑:所有这些代码在外部环境下运行良好(即在 DEA12 中它运行完美),但是当我部署它时它在 bufferedreader 处停止。

编辑二: 所以,问题肯定出在缓冲阅读器上。如果我将 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

知道为什么会发生这种情况,或者我该如何进行故障排除?

最佳答案

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

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

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

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

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

尝试从服务器对这些 URL 执行 PING,TRACE 以验证您可以访问这些 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.

关于java - Servlet 到达 BufferedReader 会超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15280172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com