Java Timerタイマ実行タイミングタスク(RCP起動進捗バー)

2054 ワード


public void loginBefore() {
	try {
	    url = new URL(PATH_URL);
	} catch (MalformedURLException e) {
	    return;
	}
	    isStart = false;
	    timer = new Timer();
	    timer.schedule(new TimerTask() {
		public void run() {
			check();
			}
		}, 0, 5*1000);// 5 sec
		showProgressDialog();
	}
	
	
	private void check() {
	    try {
		logger.info("check............");
		BufferedReader reader = new BufferedReader(new InputStreamReader(
		url.openStream()));
		String str = reader.readLine();
		if (isStart) {
		        return;
		}
			if (str.matches("Started")) {
			    logger.info("Web Server is started.");
			    isStart = true;
			    timer.cancel();//Terminate the timer thread
			    return;
			}
		} catch (Throwable t) {
			return;
		}
	}
	 
	private void showProgressDialog() {
		ProgressMonitorDialog progress = new ProgressMonitorDialog(null);
		progress.setCancelable(true);
		try {
			progress.run(true, true, new IRunnableWithProgress() {
				public void run(IProgressMonitor monitor)
						throws InvocationTargetException {
					monitor.beginTask("Procressing......",
							IProgressMonitor.UNKNOWN);
					while (true) {
						if (isStart) {
							break;
						}
						if (monitor.isCanceled()) {
							Display.getCurrent().asyncExec(new Runnable() {
								public void run() {
									PlatformUI.getWorkbench().close();
								}
							});
							isStart = true;
							break;
						}
						Display.getCurrent().readAndDispatch();
						monitor.worked(1);
					}
					monitor.done();
				}
			});
		} catch (InvocationTargetException e) {
			return;
		} catch (InterruptedException e) {
			return;
		}
	}