Mam taska
Mam 80 tysięcy linków w pliku linia o linii.
Pobieram je do ArrayList<String>
i teraz: robie ExecutorService pool=Executors.newFixedThreadPool(4,new CustomUncoughtExceptionHandler(ueh));
a do tego pool'a submituje teraz nowe obiekty Runnable po czym wstawiam
pool.shutdown();
while(!pool.isTerminated())
{
try {
pool.awaitTermination(15, TimeUnit.SECONDS);
generateRaport();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
co mi daje generowanie jakiegoś tam raportu w środku.
PROBLEM: Po przerobieniu kilkudziesięciu tysięcy linków, wątki z puli przestają pracować. Nie wiem dlaczego tak się dzieje. Objawia się to tym, że co 15 sekund wyskakuje ten sam raport. Dodam (ale to widać w kodzie poniżej), że wątki podczas pracy drukują dane więc nie jest to normalne, że tylko raport się wyświetla. Czy ma to związek z zbyt wielką pulą zadań (80k w tym przypadku), a może za dużo rady jest tworzony nowy URLConnection ? proszę o pomoc. Nie sypie żadnymi wyjątkami itd. po prostu pula "zamarza:
System.out.println("Pobrano liste linków "+list.size());
for (c = Integer.parseInt(args[0]); c < list.size(); c++) {
final int position = c;
Runnable task = new Runnable() {
public void run() {
globalStarted++;
System.out.println(position+"/"+list.size());
String querryUrl = list.get(position); // crete querry for first pagein industrie
// querryUrl=URLEncoder.encode(querryUrl,"UTF-8");
// querry link
String content = null;
try {
content = CommonTools.ConnectAndGetContent(querryUrl, "UTF-8", true);
} catch (IOException e) {
// TODO Auto-generated catch block
logger.log(Level.SEVERE,"Index: "+position+" msg:"+e.getMessage());
e.printStackTrace();
} // conect
/*
* if(content.contains("Nie znaleźliśmy wyników zawierających"
* )) System.out.println("dupcia"); else
* System.out.println("ZADZIAŁAŁO!!"); FileOutputStream
* fos=new FileOutputStream("d:\\test.html");
* fos.write(content.getBytes()); fos.close(); // test write
* to disc
*/
if (content == null)
{
System.out.println("Pobrałem null content");
logger.log(Level.SEVERE,"Index: "+position+" POBRANO null content ->"+list.get(position));
globalSkipped++;
return; // ErrorHandlingNeeded
}
int i=0;
ArrayList<String> raws=null;
int localFinished=0;
try
{
String token="href=\"";
raws=CommonTools.MultipleBetween("<h2><a id=\"preview_", ";jsessionid=", content);
globalFinished++;
synchronized(links)
{
globalCounter+=raws.size();
for(String s:raws)
{
links.add("http://www.pkt.pl"+s.substring(s.indexOf(token)+token.length()));
localFinished++;
}
}
/* for(String s:links)
System.out.println(s); */
}
catch(Exception ex)
{
System.out.println("Błąd parsingu lub brak wyników\n"+ex.getMessage());
logger.log(Level.SEVERE,"Index: "+position+"/"+localFinished+" Błąd parsingu lub brak wyników ->"+list.get(position));
globalSkipped++;
//ex.printStackTrace();
return; // ErrorHandlingNeeded
}
}
};
fragment klasy CommonTools
private static URLConnection CreateConnection(String handler,String charset,boolean different) throws IOException {
HttpURLConnection conn = (HttpURLConnection) new URL(handler).openConnection();
conn.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1");
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
if(different)
{
conn.setRequestProperty("Accept-Encoding","deflate");
conn.setRequestProperty("Accept-Language","pl,en-us;q=0.7,en;q=0.3");
conn.setRequestProperty("Connection","keep-alive");
conn.setRequestProperty("Host","www.pkt.pl");
}
HttpURLConnection.setFollowRedirects(true);
// conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
//conn.setRequestProperty("Cookie", cookie);
Map<String, List<String>> mapa = conn.getHeaderFields();
return conn;
}
public static String getContent(URLConnection connection,String charset) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream(),charset));
Writer zawartosc = new StringWriter();
int n = 0;
int i = 0;
char[] buffer = new char[1024];
while ((n = rd.read(buffer)) != -1) {
zawartosc.write(buffer, 0, n);
i += n;
}
rd.close();
return zawartosc.toString();
}
public static String ConnectAndGetContent(String url,String charset,boolean different) throws IOException
{
return getContent(CreateConnection(url,charset,different),charset);
}
public static String ConnectAndGetContent(String url) throws IOException
{
return ConnectAndGetContent(url,"UTF-8",false);
}