⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smgpdbaccess.java

📁 中国电信小灵通短信平台SP端程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    String strSql = "";
    if(SMGParameter.DBType ==1)
//    strSql = "select * from (select rownum as my_rownum,a.* from( "+
//             "SELECT * FROM SMGP_to_ismg WHERE submit_status = 1"+
//             ") a where rownum<= 30 )where my_rownum> 0";
      strSql = "SELECT * FROM SMGP_to_ismg WHERE submit_status = 1 "+
               " and rownum<= 100 ";
    else
    strSql = "select * from SMGP_to_ismg where submit_status = 1";


    ResultSet rs    = null;
    Statement stmt  = null;
    Connection conn = null;

    try {
        //Debug.outDebug("[SMGPMaster]读取待发送信息到缓冲..." + strSql);
        conn = ConnectionPool.getConnection();
        stmt = conn.createStatement();
        rs   = stmt.executeQuery(strSql);
    }
    catch (Exception ex) {
        this.isAvail = false;
        Debug.outWarn("[SMGPMaster]Fetch data from SMGP_to_ismg table failed: " + ex) ;
        return false;
    }

    byte[] msg;
    try {
         int num        = 0;   //取出待发送数据的计数器
         String content = "";
         while( rs.next()) {
            if(num++ > 1000) break;  //每次最多取出1000条记录

            String msgcontent      = rs.getString ("MSG_CONTENT")==null?"":rs.getString ("MSG_CONTENT").trim();

            //如果发送字符长,则截成多段
            int length     = 70;  //标准为70个字符
            int iMsgLenth = msgcontent.length();

//            int iLength = (0 + length > iMsgLenth) ? iMsgLenth - 0 : length;
//            int iBeginIndex = 0;
//            int iEndIndex   = iEndIndex   =
//                (iBeginIndex + iLength > iMsgLenth) ? (iMsgLenth) : (iBeginIndex + iLength);
//
//            for (;iBeginIndex  < iEndIndex;
//                 iBeginIndex = iEndIndex,
//                 iEndIndex   = (iBeginIndex + iLength > iMsgLenth) ?
//                 (iMsgLenth) : (iBeginIndex + iLength)
//                )
//            {
             SMGP mtMsg   = new SMGP(SMGP.SMGP_SUBMIT);
             mtMsg.SequenceID = rs.getInt("to_ismg_id");
             mtMsg.NeedReport = (byte)rs.getInt("registered_delivery");
             mtMsg.Priority   = 0;
             mtMsg.ServiceID  = rs.getString("service_id");
             mtMsg.FeeType    = rs.getString("fee_type");
             mtMsg.FeeCode    = rs.getString("fee_code");
             mtMsg.FixedFee   = rs.getString("fixedfee");
             mtMsg.MsgFormat  = (byte)rs.getInt("msg_fmt");
             mtMsg.ValidTime  = "";
             mtMsg.AtTime     = "";
             mtMsg.SrcTermID  = rs.getString("src_terminal_id"); //短消息发送用户号码118+CP代码
             mtMsg.ChargeTermID     = rs.getString("fee_termainal_id");
             mtMsg.DestTermIDCount  = 1;
             mtMsg.DestTermID       = rs.getString("dest_terminal_id");

             mtMsg.MsgContent = msgcontent;
             mtMsg.MsgLength  = (byte)mtMsg.MsgContent.getBytes().length;

             //Debug.outInfo("[发送数据]"+PublicFunction.getFormatTime()+ " (" +
             //                   mtMsg.DestTermID + ")" + msgcontent+"");
             Debug.outInfo("\n[SMGPMaster]"+PublicFunction.getFormatTime()+" 发送数据:("+mtMsg.DestTermID+")"+msgcontent +" @"+mtMsg.SrcTermID);

             synchronized(vctMTData){
               vctMTData.addElement(mtMsg);
             }

//           } // end for (;iBeginIndex  < iEndIndex;.....
        }
    }
    catch(Exception e){
       e.printStackTrace();
       return false;
    }

    //关闭连接
    try{
        rs.close() ;
        stmt.close() ;
    }
    catch(SQLException e){
      e.printStackTrace();
      this.isAvail = false;
      return false;
    }

    //System.out.println("开始只标志为");

    //置发送标记SEND_STATUS为-2
    SMGP mtMsg;
    int sndSize    = vctMTData.size() ;
    try{
      stmt = conn.createStatement();
      conn.setAutoCommit(false);

      for(int i = 0; i < sndSize; i++){
          mtMsg = (SMGP)vctMTData.elementAt (i);
          strSql = "update smgp_to_ismg set submit_status = -2 where to_ismg_id =  " + mtMsg.SequenceID ;
          stmt.addBatch(strSql);
      }

      stmt.executeBatch();
      conn.commit();
      conn.setAutoCommit(true);
    }
    catch(Exception ex){
      Debug.outWarn("[SMGPMaster]更新代发送数据状态位失败:"+ex.toString());
      vctMTData.removeAllElements();
      this.isAvail = false;
      return false;
    }


//    for(int i = 0; i < sndSize; i++){
//        mtMsg = (SMGP)vctMTData.elementAt (i);
//        strSql = "update smgp_to_ismg set submit_status = -2 where to_ismg_id =  " + mtMsg.SequenceID ;
//        try {
//            stmt = conn.createStatement();
//            stmt.execute(strSql);
//            stmt.close() ;
//        }
//        catch (SQLException ex) {
//            System.out.println("[SMGPMaster]DBAccess.Set Send_Status error :" +ex.toString());
//            vctMTData.removeElementAt (i);
//            i--;
//            sndSize--;
//            this.isAvail = false;
//            return false;
//        }
//    }

    discloseconn(conn,stmt,null);  //关闭connection(放回连接池)
    return true;
  }




  /**释放连接池的连接closeconn();  //关闭connection(放回连接池)**/
  private void discloseconn(Connection conn,Statement stmt,ResultSet rst){
    try{
      if(rst != null){
        rst.close();
      }
       if(conn != null){
        conn.close();
      }
      if(stmt != null){
        stmt.close();
      }
   }
    catch (Exception sqle){
      System.out.println("Database disconnecting failed"+sqle);
    }
  }



  /**
   * <p>Title: SMGPMODataSaveThread保存上行数据和处理MT响应的线程</p>
   * <p>Description: 湖南移动短信网关通讯程序</p>
   * <p>Copyright: Copyright (c) 2004</p>
   * <p>Company: sunun tech ltd.</p>
   * @author zhanghao
   * @version 1.0
   */
  public class SMGPMODataSaveThread extends Thread{

    public boolean isAvail = false;

    public SMGPMODataSaveThread(){
      isAvail = true;
    }

    public void run(){
      //转储缓冲中的MO数据
      Vector vctWillSaveMO = new Vector(1,1);
      Vector vctMTRespMsg  = new Vector(1,1);

      try{
        while(isAvail){
          //如果上行缓冲有数据或MT响应缓冲中有数据
          if(SMGPDBAccess.getInstance().isAvail){

            if(vctMOData.size()>0){
              synchronized(vctMOData){
                while(vctMOData.size()>0){
                  vctWillSaveMO.addElement(vctMOData.elementAt(0));
                  vctMOData.removeElementAt(0);
                }
              }

              //调用SMGPDBAccess的存储方法存储上行数据
              SMGPDBAccess.getInstance().saveMODataFromVector(vctWillSaveMO);

              //System.out.println("vctWillSaveMO = " + vctWillSaveMO.size());\
            }

            if(vctRespMsg.size()>0){
             synchronized(vctRespMsg){

               while(vctRespMsg.size()>0){
                 vctMTRespMsg.addElement(vctRespMsg.elementAt(0));
                 vctRespMsg.removeElementAt(0);
               }
             }

             //调用SMGPDBAccess的存储方法存储上行数据
             SMGPDBAccess.getInstance().dealProccessMsg(vctMTRespMsg);

             //System.out.println("vctWillSaveMO = " + vctWillSaveMO.size());\
           }

          }
          else{
            PublicFunction.sleep(20);
          }
        }
      }
      catch(Exception ex){
        //打印异常,并关闭连接
        Debug.outInfo("[SMGPMaster]保存数据失败:"+ex.toString());
        //将未写入数据库的MO数据放回原MO缓冲
        while(vctWillSaveMO.size()>0){
          vctMOData.addElement(vctWillSaveMO.elementAt(0));
          vctWillSaveMO.removeElementAt(0);
        }

        while(vctMTRespMsg.size()>0){
          vctRespMsg.addElement(vctMTRespMsg.elementAt(0));
          vctMTRespMsg.removeElementAt(0);
        }

        DBAccess.getInstance().disclose(null);
      }
    }
  }



  /**
   * 发送成功后,删除待发送表中的数据
   * @param mtData
   * @return
   */
  public void dealProccessMsg(SMGP SMGPMsg){
    Statement stmt = null;
    PreparedStatement pstmt = null;
    String sqlStatement =null;
    Connection conn     = null;

    try {
      if(SMGPMsg.Status ==0){
        String strSql ="delete from smgp_to_ismg where to_ismg_id = "+SMGPMsg.SequenceID;
        System.out.println("[SMGPMaster]发送短信成功,MsgID:"  +SMGPMsg.SequenceID+"");
        conn = ConnectionPool.getConnection();
        stmt = conn.createStatement();
        stmt.execute(strSql);
        stmt.close();
      }
      else{
        System.out.println("[SMGPMaster]发送短信失败,错误状态码:"  +SMGPMsg.Status+"") ;
        String strSql ="update smgp_to_ismg set submit_status = "+SMGPMsg.Status+" where to_ismg_id = "+SMGPMsg.SequenceID;
        conn = ConnectionPool.getConnection();
        stmt = conn.createStatement();
        stmt.execute(strSql);
        strSql ="update smgp_to_ismg_bak set submit_status = "+SMGPMsg.Status+" where to_ismg_id = "+SMGPMsg.SequenceID;
        stmt.execute(strSql);
        stmt.close();
      }

    }
    catch(Exception e){
      Debug.outWarn("[SMGPMaster] dealProccessMsg() error:msgid=" + SMGPMsg.Status +","+e.toString()) ;
      disclose(conn);
    }
    finally{
      discloseconn(conn,null,null);  //关闭connection(放回连接池)
    }
  }




  /**
   * 断开数据库连接
   */
  private void disclose(Connection conn) {
    try {
      if(this.isAvail){
        if(conn != null){
          conn.close();
        }
        this.isAvail = false;
        Debug.outInfo("通讯程序关闭数据库连接");
      }
    }
    catch (Exception sqle){
      System.out.println("Database disconnecting failed");
    }
  }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -