java操作Oracle 方式二 ( 多线程 )

多线程 方式  也是  连接-》操作-》断开连接   这样的操作过程,只是采用了多线程

这种方式的特点是 每次都是新的连接,多线程,解决了 网络环境不好时连接oracle比较费时,影响主程序其它功能的响应。

OracleUtil.java 基础类代码 详见:https://www.cnblogs.com/hailexuexi/p/18302732

完整代码

dbCdrThread.java
package com.JavaRabbitMQToDataBase.dbOracle;

import java.util.UUID;
import com.alibaba.fastjson.JSONObject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class dbCdrThread extends Thread{
    protected static final Logger logger = LoggerFactory.getLogger(dbCDR.class);
    //连接对象
    Connection connection=null;
    //创建预编译对象
    PreparedStatement ps=null;
    //创建结果集
    ResultSet rs = null;
    //CDR 参数
    private String _eventJson;

    public dbCdrThread(String eventJson){
        _eventJson=eventJson;
    }

    //线程 执行
    public void run() {
        this.insert();
    }

    //插入
    public int insert() {
        logger.debug("dbCDR insert() CDR 准备执行SQL" + this._eventJson);
        int result = 0;
        String sql = "";
        String callid ="";
        String callfrom ="";
        String strAgentName="";//坐席工号
        String strCallIdUnique="";//呼入弹屏唯一码

        if (OracleUtil.oracle_open.equals("true") == false) {
            logger.warn("dbCDR insert() CDR 没有启动连接数据库 " + OracleUtil.oracle_open + " " + _eventJson);
            return -2;
        }
        try {
            connection = OracleUtil.getCon();//连接对象

            //{"event":"NewCdr","callid":"1627349862.433","timestart":"2021-07-27 09:37:42","callfrom":"1008","callto":"1009"
            // ,"callduraction":"16","talkduraction":"11","srctrunkname":"","dsttrcunkname":"","pincode":"","status":"ANSWERED"
            // ,"type":"Internal","callPath":"","recording":"20210727093747-1627349862.433-1008-1009-Internal.wav","didnumber":"","agentringtime":"0"
            // ,"sn":"369264842620"}
            String strId = UUID.randomUUID().toString();//唯一码
            JSONObject jsonObj = JSONObject.parseObject(this._eventJson);//

            String event = jsonObj.get("event").toString();
            callid = jsonObj.get("callid").toString();
            String timestart = jsonObj.get("timestart").toString();
            //主叫号码
            callfrom = jsonObj.get("callfrom").toString();
            //被叫号码
            String callto = jsonObj.get("callto").toString();
            //如果有 6703(1008) 时将队列号6703 去掉
            if(callto.contains("(")==true){
                callto = callto.substring(5);  //6703(
                callto = callto.substring(0, callto.indexOf(")"));//去掉 )
            }

            String callduraction = jsonObj.get("callduraction").toString();
            String talkduraction = jsonObj.get("talkduraction").toString();
            String srctrunkname = jsonObj.get("srctrunkname").toString();
            String dsttrcunkname = jsonObj.get("dsttrcunkname").toString();

            String pincode = jsonObj.get("pincode").toString();
            String status = jsonObj.get("status").toString();
            String type = jsonObj.get("type").toString();
            String recording = jsonObj.get("recording").toString();
            String didnumber = jsonObj.get("didnumber").toString();

            String agentringtime = jsonObj.get("agentringtime").toString();
            String sn = jsonObj.get("sn").toString();
            String callPath = jsonObj.get("callPath").toString();

            //坐席工号
            if(jsonObj.containsKey("agentname")==true){
                strAgentName=jsonObj.get("agentname").toString();
            }
            //呼入弹屏唯一码
            if(jsonObj.containsKey("callidunique")==true){
                strCallIdUnique=jsonObj.get("callidunique").toString();
            }

            sql = "insert into CTI_CDR ";
            sql = sql + " ( ID,event,callid,timestart,callfrom, ";
            sql = sql + " callto,callduraction,talkduraction,srctrunkname,dsttrcunkname, ";
            sql = sql + " pincode,status,type,recording,didnumber, ";
            sql = sql + " agentringtime,sn,callpath,agentname,callidunique ) ";
            sql = sql + " values(?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,? )";

            ps = connection.prepareStatement(sql);
            ps.setString(1, strId);
            ps.setString(2, event);
            ps.setString(3, callid);
            ps.setString(4, timestart);
            ps.setString(5, callfrom);

            ps.setString(6, callto);
            ps.setString(7, callduraction);
            ps.setString(8, talkduraction);
            ps.setString(9, srctrunkname);
            ps.setString(10, dsttrcunkname);

            ps.setString(11, pincode);
            ps.setString(12, status);
            ps.setString(13, type);
            ps.setString(14, recording);
            ps.setString(15, didnumber);

            ps.setString(16, agentringtime);
            ps.setString(17, sn);
            ps.setString(18, callPath);

            ps.setString(19, strAgentName);
            ps.setString(20, strCallIdUnique);

            //logger.debug("dbCDR insert() CDR 执行SQL!" + ps.toString()  );

            result = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("dbCDR insert() CDR 执行SQL出错! " + result + " "+ callid+ " "+callfrom+  " " + sql + " " + e.toString());
        } finally {
            try {
                if (ps != null) { // 
                    ps.close();////ps需要关闭,不然会出现 java.sql.SQLException: ORA-01000: 超出打开游标的最大数
                }
                OracleUtil.closeCon(connection);
            } catch (SQLException e) {
                e.printStackTrace();
                logger.error("dbCDR insert() CDR 关闭连接出错!" + result + " "+ callid+ " "+callfrom+ " " + " " + sql + " " + e.toString());
            }
        }
        logger.debug("dbCDR insert() CDR 执行SQL成功!" + result + " "+ callid+ " "+callfrom+ " " + sql);
        return result;
    }
 
}

调用方法

//{"callid":"1635822668.528","callto":"6700(1008)","pincode":"","srctrunkname":"MNWG","recording":"20211102111141-1635822668.528-13941128270-1008-Inbound.wav","dsttrcunkname":"","type":"Inbound","callPath":"6700","callduraction":"15","callfrom":"13941128270","didnumber":"123456","agentringtime":"8","sn":"369264842620","event":"NewCdr","timestart":"2021-11-02 11:11:41","talkduraction":"7","status":"ANSWERED"}
dbCDR thdCDR = new dbCDR(eventJson);
thdCDR.start();

 

posted @ 2024-07-15 13:04  海乐学习  阅读(3)  评论(0编辑  收藏  举报