<pre name="code" class="java">package cn.com.zte.esb.progress;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.jboss.tools.flow.jpdl4.util.ZteMessages;
/**
* 测试Phoenix连接的进度条
*
*/
public class TestPhoenixProgress implements IRunnableWithProgress {
public Map<String, String> params = new HashMap<String, String>();
public Map<String, String> resultMap = new HashMap<String, String>();
class Login implements Runnable {
public Exception exception;
Login() {
}
public void run() {
resultMap.clear();
try {
String url=params.get("url");
String driver=params.get("driver");
Class.forName(driver);
Connection conn = DriverManager.getConnection(url);
if(conn!=null){
resultMap.put("success",url);
conn.close();
}else{
resultMap.put("error",url);
}
} catch (Exception e) {
resultMap.put("error",e.getMessage());
e.printStackTrace();
}
}
}
public void run(IProgressMonitor monitor) throws InvocationTargetException,InterruptedException {
monitor.beginTask(ZteMessages.getString("connection")+ "...", IProgressMonitor.UNKNOWN);
{
Login login = new Login();
Thread loginThread = new Thread(login, "LoginThread");
loginThread.start();
do {
Thread.sleep(250L);
//进度条被取消事件
if (monitor.isCanceled()) {
throw new InterruptedException("interrupted");
}
} while (loginThread.isAlive());
}
//完成
monitor.done();
String message_error = resultMap.get("error");
String message_success = resultMap.get("success");
//根据返回值抛出异常,调用出根据异常信息判断连接是否成功
if (message_success != null) {
throw new cn.com.zte.esb.exception.LoginException("success#"+message_success);
}
if (message_error != null) {
throw new cn.com.zte.esb.exception.LoginException("error#"+message_error);
}
}
}
//调用
final Map<String, String> map=new HashMap<String, String>();
map.put("url",connection.getText());
map.put("driver",driver);
ProgressMonitorDialog pmd = new ProgressMonitorDialog(Display.getDefault().getActiveShell());
TestPhoenixProgress progress=new TestPhoenixProgress();
progress.params=map;
try {
pmd.run(true, true, progress);
} catch (InvocationTargetException e1) {
Throwable targetException = e1.getTargetException();
if(targetException!=null){
String message=targetException.getMessage();
if(message!=null){
message=message.trim();
if(message.startsWith("success")){
MessageDialog.openInformation(Display.getDefault().getActiveShell(), ZteMessages
.getString("ConnectionTest"), ZteMessages.getString("ConnectionSuccess"));
}else if(message.startsWith("error")){
MessageDialog.openError(Display.getDefault().getActiveShell(), ZteMessages.getString("ConnectionTest"),"\n"+message.substring(6));
}else{
if(!"interrupted".equals(e1.getMessage()))
MessageDialog.openError(Display.getDefault().getActiveShell(), ZteMessages.getString("ConnectionTest"),e1.getMessage());
}
}
}
} catch (InterruptedException e1) {
String message = e1.getMessage();
if(message!=null){
message=message.trim();
if(message.startsWith("success")){
MessageDialog.openInformation(Display.getDefault().getActiveShell(), ZteMessages
.getString("ConnectionTest"), ZteMessages.getString("ConnectionSuccess"));
}else if(message.startsWith("error")){
MessageDialog.openError(Display.getDefault().getActiveShell(), ZteMessages.getString("ConnectionTest"),"\n"+message);
}else{
if(!"interrupted".equals(e1.getMessage()))
MessageDialog.openError(Display.getDefault().getActiveShell(), ZteMessages.getString("ConnectionTest"),e1.getMessage());
}
}
}
return;
RCP中可以取消的进度条
最新推荐文章于 2021-11-08 15:13:32 发布