简单解决复杂的Oracle IAS问题

数据库 Oracle 数据库运维
本文作者最近碰到了一个简单的NoClassDefFoundError问题,不过针对这个涉及Oracle IAS的错误,却有十分简单的解决方案。

笔者做了一个小的系统辅助功能,可以周期性访问某个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 < " + strUrl + " > 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;
    }

【编辑推荐】

  1. 实例讲解Oracle 9i数据坏块的处理
  2. Oracle 11g中实现自我调整功能
  3. Oracle数据库设计提升性能的五条法则
责任编辑:彭凡 来源: 百度空间
相关推荐

2012-04-06 09:45:41

开发

2022-12-12 09:46:49

Kubernetes容器

2023-06-28 06:33:37

2010-04-16 17:31:22

ORACLE回滚段

2010-04-15 14:33:47

删除Oracle服务

2010-04-29 17:46:31

Oracle死锁

2020-11-11 07:09:05

隔离直播系统

2014-04-18 09:30:49

基于网络的APMAPM应用管理

2022-09-05 17:26:27

技术

2022-04-18 09:00:00

数据库向量机器学习

2010-05-07 10:06:10

Oracle PL D

2011-10-13 10:59:44

ADSL猫远程控制网络维护

2009-09-15 17:45:34

Linq多条件

2010-04-28 18:01:15

Unix系统

2023-01-04 10:24:42

2010-04-26 13:38:34

Oracle dele

2010-05-06 17:24:05

Unix命令

2010-04-13 14:25:24

Oracle问题解决

2015-10-21 14:27:18

ORACLE 超长字符解决办法

2022-12-27 08:43:18

系统思维设计思维创新
点赞
收藏

51CTO技术栈公众号