`

发现Mozilla的东西问题出的都很诡异

阅读更多

Mozilla的东西文档比较少,并且出现问题都很诡异,在网上能搜出来的答案很少。今天又遇到一个很诡异的问题。在普通的应用程序中使用:

nsIWebNavigation webNavigation = (nsIWebNavigation) webBrowser
              .queryInterface(nsIWebNavigation.NS_IWEBNAVIGATION_IID);
    if (webNavigation == null)
      error(Mozilla.NS_ERROR_NO_INTERFACE);

    webNavigation.loadURI(url, nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null);
 

没有问题,

但是把应用程序部署到Tomcat上面,第一个请求loadURI会成功,以后的请求都会失败:

org.eclipse.swt.SWTException: Failed to execute runnable (org.mozilla.xpcom.XPCOMException: The function "loadURI" returned an error condition (0x80040111)
 

在 

http://markmail.org/message/c4gbufihj2oid6db#query:org.mozilla.xpcom.XPCOMException%3A%20The%20function%20loadURI%20returned%20an%20error%20condition%20(0x80040111)+page:1+mid:c4gbufihj2oid6db+state:results
 

有一个类似的问题。但没有人回复。

分享到:
评论
1 楼 fuliang 2008-11-04  
package com.ibm.scissorhands.runtime.feeds.impl;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.ibm.scissorhands.swt.browser.MozillaBrowser;

public class TestMozillaBrowser {
	Display display;
	Shell shell;
	MozillaBrowser browser;

	public TestMozillaBrowser() {
		this(new Display());
	}

	TestMozillaBrowser(Display display) {
		this.display = display;
		shell = new Shell(display);
		shell.setLayout(new FillLayout());
		browser = new MozillaBrowser(shell, SWT.NONE);
	}

	public TestMozillaBrowser(String url) {
		this();
		setURL(url);
	}

	private void setURL(final String url) {
		try {
			System.out.println("Set URL: " + url);
			browser.setUrl(url);
		} catch (RuntimeException err) {
			display.dispose();
			throw err;
		}
	}

	private synchronized void start() {
		shell.open();
		try {
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception err) {
			err.printStackTrace();
		} finally {
			display.dispose();
		}
	}

	
	 static class TestThread extends Thread{
		 TestMozillaBrowser xulRunner = null;
		@Override
		public void run() {
			try {
				xulRunner = new TestMozillaBrowser("www.google.com");
				xulRunner.start();
			} catch (RuntimeException err) {
				System.err.println("ERROR: Open URL failed.");
				throw err;
			}
		}
		
		public void disposeUI(){
			Display.getDefault().syncExec(new Thread(){
				@Override
				public void run() {
					xulRunner.shell.dispose();
				}
			});
		}
	}
	
	public static void main(String[] args)throws Exception {
		TestThread[] threads = new TestThread[2];
	    threads[0] = new TestThread();
		threads[0].start();
		Thread.currentThread().sleep(2000);//wait for the shell constructed
		threads[0].disposeUI();//Do dispose the UI,the browser will be disposed when receive the SWT.Dispose Event
		
		threads[1] = new TestThread();
		threads[1].start();
		Thread.currentThread().sleep(2000);
		threads[1].disposeUI();
	}
}

上面的代码可以重现这个异常。
swt中多个Display,很容易出现问题,因为一个Display就是一个UI线程,一个UI事件的dispather,只有在确保syncExec使用正确的display,分发到正确的UI线程上才能得到正确的结果,因此swt通常只有一个Display,虽然在windows上已经支持多个Display(Linux尚未支持),上面的问题,在保证整个应用程序只有一个Display的情况下可以解决。

相关推荐

Global site tag (gtag.js) - Google Analytics