开发者

HTMLUnit webClient.getPage(locationToPing) is blocked when the jar is invoked from Runtime.exec()

I am writing an application that needs to load a jar periodically. The Jar fetches the content of a website making use of HTMLUnit.When this jar is run from commond prompt it runs as expected. But when I run it using java code by making use of Runtime, it blocks at the place , page= webClient.getPage(locationToPing); and doesnt proceed furhter.

The Jar contains only one java class as given below,

package tempproj2;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Fetcher {

String locationToPing;
volatile WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
HtmlPage page ;
public static final long SLEEP_TIME_IF_CONNECTION_WAS_REFUSED=10000; //SECS
public long connectionRefusedTime=0;

private static Fetcher theOnlyInstanceOfThisClass=new Fetcher();

public static Fetcher getInstance(){
    return theOnlyInstanceOfThisClass;
}
private Fetcher(){
    init();
    fetchUsingGet();
}
private void init(){
    locationToPing="http://www.mail.yahoo.com";
}

private  void fetchUsingGet(){
    try {
        System.out.println("-----------Start  1 --------------")            ;
        webClient.setUseInsecureSSL(true);
        webClient.setThrowExceptionOnScriptError(false);
        System.out.println("-----------Start  2 --------------")            ;
        webClient.setJavaScriptEnabled(true);
        System.out.println("-----------Start  3 --------------")            ;
        webClient.setTimeout(5000);  //30 secs - its int n not long be careful abt it.
        System.out.println("-----------Start  4 --------------locationToPing="+locationToPing)            ;
        page= webClient.getPage(locationToPing);
        System.out.println("-----------Start  5 --------------"+page.asXml())            ;
    } catch (Exception ex) {
        System.out.println("-----------IC Fetecher     Error here --------------"+ex.getMessage())            ;
        connectionRefusedTime=System.currentTimeMillis();
       ex.printStackTrace();
    }
}

private void reload(){

    if(page==null){
        fetchUsingGet();
        return;
    }
    try {

        page=(HtmlPage)page.refresh();
    } catch (java.net.SocketTimeoutException ex) {
        fetchUsingGet();
        ex.printStackTrace();
    }catch(IOException ex){
        ex.printStackTrace();
    }
}


public static void main(String ar[]){
    Fetcher fetcher=new Fetcher();
    while(true){
        try {
            Thread.sleep(4*1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Fetcher.class.getName()).log(Level.SEVERE, null, ex);
        }
        fetcher.reload();

    }


}

}

Given below are the different ways I tried to run the above code

  1. Run the class as it is开发者_Go百科 - Runs fine.
  2. Make a Jar of the above class and run from the command prompt using java -jar , command - Runs fine.
  3. Make a Jar of above class and run it from another java class using the following code,

    try {
        String execCmd="java -jar "+downloadApplicationJarLocation.getAbsolutePath();
        Process pr=Runtime.getRuntime().exec(execCmd) ;
    
            InputStreamReader is = new InputStreamReader(pr.getInputStream());
            BufferedReader br = new BufferedReader(is);
            String line = null;
            String processMessage="";
            while ((line = br.readLine()) != null) {
                System.err.println("Line 1 "+line)    ;            
                processMessage+=line;
            }
            System.out.println("Finished ");
            is.close();
            br.close();
    
    
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    

But when I try the above code only the code up to page= webClient.getPage(locationToPing); is executed and gets blocked. "Start 5" is never printed on the screen.

  1. I modifed the above code and instead of directly calling the e Java -jar command in the Runtime.exec(), I placed it in a batch file and called this batch file. Process pr=Runtime.getRuntime().exec(batchFileLocation) ; Now it executed as expected and didnt block the code.Also "Start 5" gets printed on the screen.

My application needs to invoke the jar periodically from java and destroy the previous process if any. So Option 4 doesnt hold good for me as the subprocess is stil alive and not very easy to destroy the subprocess.

Option 3 is the ideal way for me but am quite not able to understand why does it get blocked and doesnt proceed any longer? Is it something to do with threading? Am I using HTMLUnit the way how it is supposed to be used?

Thanks in Advance

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜