您好,欢迎访问一九零五行业门户网

简单解决复杂的Oracle IAS问题

笔者做了一个小的系统辅助功能,可以周期性访问某个url、执行某个sql语句 or 执行某个系统命令。执行sql语句和系统命令比较简单,
笔者做了一个小的系统辅助功能,可以周期性访问某个url、执行某个sql语句 or 执行某个系统命令。
执行sql语句和系统命令比较简单,这里不再详述,主要说一下访问某个url。
实际上jdk自身已有工具类用于创建http请求,类名是:java.net.httpurlconnection,,但考虑到基础类通常比较粗糙,很多情况要自己考虑和处理,就转头去google了下,发现果然有开源的工具包可以使用,几个工具包中以httpclient较为常用,而且是apache的东东,于是决定采用httpclient。
从apache上down了包commons-httpclient-3.1.jar和commons-codec-1.3.jar两个包,后者是httpclient依赖的包。
帮助写的很好,即便是像我这样英文很烂,也能很快上手。
public boolean visiturl(string url) {
// commons httpclient 3.1
httpclient client = new httpclient();
httpmethod method = new getmethod(url);
// provide custom retry handler is necessary
method.getparams().setparameter(httpmethodparams.retry_handler, new defaulthttpmethodretryhandler(3, false));
boolean rs = false;
try {
// execute the method.
int statuscode = client.executemethod(method);
if (statuscode != httpstatus.sc_ok) {
logger.error(method failed: + method.getstatusline());
}
else {
rs = true;
}
} catch (httpexception e) {
logger.error(fatal protocol violation: + e.getmessage());
} catch (ioexception e) {
logger.error(fatal transport error: + e.getmessage());
} finally {
// release the connection.
method.releaseconnection();
}
return rs;
}
本机tomcat下run一下,工作正常,随即丢到服务器(oracle ias环境)上测试,程序应该出乎意料的报了个错。
09/03/16 19:03:43 java.lang.noclassdeffounderror
09/03/16 19:03:43 at org.apache.commons.httpclient.httpmethodbase.writerequestline(httpmethodbase.java:2015)
09/03/16 19:03:43 at org.apache.commons.httpclient.httpmethodbase.writerequest(httpmethodbase.java:1864)
09/03/16 19:03:43 at org.apache.commons.httpclient.httpmethodbase.execute(httpmethodbase.java:975)
09/03/16 19:03:43 at org.apache.commons.httpclient.httpmethoddirector.executewithretry(httpmethoddirector.java:368)
09/03/16 19:03:43 at org.apache.commons.httpclient.httpmethoddirector.executemethod(httpmethoddirector.java:164)
09/03/16 19:03:43 at org.apache.commons.httpclient.httpclient.executemethod(httpclient.java:437)
09/03/16 19:03:43 at org.apache.commons.httpclient.httpclient.executemethod(httpclient.java:324)
09/03/16 19:03:43 at com.zbht.util.timertaskmanager.runurltask(timertaskmanager.java:237)
09/03/16 19:03:43 at _system._timer__task._test._jspservice(_test.java:182)
09/03/16 19:03:43 at com.orionserver.http.orionhttpjsppage.service(orionhttpjsppage.java:59)
09/03/16 19:03:43 at oracle.jsp.runtimev2.jsppagetable.service(jsppagetable.java:462)
09/03/16 19:03:43 at oracle.jsp.runtimev2.jspservlet.internalservice(jspservlet.java:594)
09/03/16 19:03:43 at oracle.jsp.runtimev2.jspservlet.service(jspservlet.java:518)
09/03/16 19:03:43 at javax.servlet.http.httpservlet.service(httpservlet.java:856)
09/03/16 19:03:43 at com.evermind.server.http.servletrequestdispatcher.invoke(servletrequestdispatcher.java:713)
09/03/16 19:03:43 at com.evermind.server.http.servletrequestdispatcher.forwardinternal(servletrequestdispatcher.java:370)
09/03/16 19:03:43 at com.evermind.server.http.httprequesthandler.doprocessrequest(httprequesthandler.java:871)
09/03/16 19:03:43 at com.evermind.server.http.httprequesthandler.processrequest(httprequesthandler.java:453)
09/03/16 19:03:43 at com.evermind.server.http.ajprequesthandler.run(ajprequesthandler.java:302)
09/03/16 19:03:43 at com.evermind.server.http.ajprequesthandler.run(ajprequesthandler.java:190)
09/03/16 19:03:43 at oracle.oc4j.network.serversocketreadhandler$saferunnable.run(serversocketreadhandler.java:260)
09/03/16 19:03:43 at com.evermind.util.releasableresourcepooledexecutor$myworker.run(releasableresourcepooledexecutor.java:303)
09/03/16 19:03:43 at java.lang.thread.run(thread.java:595)
错误信息看上去比较低级:noclassdeffounderror,类没找到,迅速了检查了一下本机和服务器上的jar包是否相同,“一模一样”!这就奇怪了。
检查本机的开发环境,只添加了这两个jar,其他的都没有动过,又检查服务器的运行环境,一样没有变化。于是删掉本机开发环境下的这两个jar,问题浮出来了,类中对httpclient的7、8个引用中只有1个提示未找到指定的类,看来oracle自己的某个包中已经包含某个较低版本的httpclient,jar包冲突的问题是件让人沮丧的事情,尝试解决这种问题会所耗费的时间也许是其他方法的n倍,无心恋战。
其实此处要进行的操作很简单,就是访问指定的url,根据返回的内容检查是否成功,httpclient是完整模拟浏览器,考虑了很多种问题,使用起来反倒是复杂了,决定转用jdk的基础类:java.net.httpurlconnection
事情出奇的顺利,空间里找到了之前写的一个方法,正好解决这个问题,以下是代码清单:
private boolean visiturl(string strurl, string successflag) {
boolean rs = false;
httpurlconnection jconn = null;
bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream();
try {
url url = new url(strurl);
jconn = (httpurlconnection) url.openconnection();
jconn.setdooutput(true);
jconn.setdoinput(true);
jconn.connect();
inputstream in = jconn.getinputstream();
byte[] buf = new byte[4096];
int bytesread;
while ((bytesread = in.read(buf)) != -1) {
bytearrayoutputstream.write(buf, 0, bytesread);
}
string strread = new string(bytearrayoutputstream.tobytearray());
logger.debug(strread);
strread = stringutil.nvl(strread);
if(strread.indexof(successflag) != -1) {
logger.info(visit url  success !);
rs = true;
}
} catch (malformedurlexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
} finally {
jconn.disconnect();
try {
bytearrayoutputstream.close();
} catch (ioexception e) {
e.printstacktrace();
}
}
return rs;
}
其它类似信息

推荐信息