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

📄 sendfromdb.java

📁 移动彩信网关
💻 JAVA
字号:
package com.mengzhong.mms;

import com.mengzhong.mms.*;
import com.mengzhong.mms.publics.*;
import com.huawei.mms.pdu.MMessage;
import com.huawei.mms.pdu.MMSContent;
import java.io.FileInputStream;
import java.util.StringTokenizer;
import java.io.IOException;
import java.sql.*;
import java.io.*;
import org.apache.log4j.*;

/**
 * <p>Title: </p>
 * <p>Description: 定期从数据库取接口表数据进行发送,并且更新发送状态</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class SendFromDB
    extends Thread {
  private static Logger logger = Logger.getLogger(SendFromDB.class.getName()); //初始化记录器
  private static Connection conn = null;
  private static ResultSet rst = null;
  private static PreparedStatement selectTousercxpstmt = null;
  private static PreparedStatement selectToUserpstmt = null;
  private static PreparedStatement updateToUserpstem = null;
  private static PreparedStatement deleteToUserpstem = null;
  private String selectTouserSQLcx = "select Content_Id,MMC_Content_type,Content_decs_type,Content_decs from T_MMS_Content where Content_id =?";
  private String selectTouserSQL = "select SUBMIT_ID,Service_Code,Charged_Party_ID,To_No,Cc_No,Bcc_No,MMS_subjet,MMS_Content_Type,Content_Id_List,Insert_time,Settlement_time,Send_time,Send_status "
      + " from T_SUBMIT_inteface where Settlement_time<sysdate and send_status=1 and rownum<6";
  private String updateTouserSQL = "update T_SUBMIT_inteface set send_status=2,Send_time=sysdate  where SUBMIT_ID=? ";
  private String deleteTouserSQL =
      "delete from T_SUBMIT_inteface where SUBMIT_ID=?";
  private static SendFromDB sendfromdb = null;
  public static SendFromDB getCompereToUser() {
    if (sendfromdb == null)
      sendfromdb = new SendFromDB();
    return sendfromdb;
  }

  public static SendFromDB getInstance() {
    if (sendFromDb == null)
      PropertyConfigurator.configure("/home/mmsc10100/cfg/log.cfg"); //定义log的cfg文件
    sendFromDb = new SendFromDB();
    return sendFromDb;
  }

  public SendFromDB() {
    connectDatabase();
    try {
      selectTousercxpstmt = conn.prepareStatement(selectTouserSQLcx);
      selectToUserpstmt = conn.prepareStatement(selectTouserSQL);
      updateToUserpstem = conn.prepareStatement(updateTouserSQL);
      deleteToUserpstem = conn.prepareStatement(deleteTouserSQL);
    }
    catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }

  public static void connectDatabase() {
    conn = DatabaseAccess.connectDatabase(PublicConstants.CONNECTION_URL,
                                          PublicConstants.DB_USER,
                                          PublicConstants.DB_PASS);
  }

  public void run() {
    com.mengzhong.mms.publics.PublicMethod.threadSleep(500);
    while (true) {
      int messageID = 0;
      com.mengzhong.mms.publics.PublicMethod.threadSleep(1000);
      try {
        if (conn == null) {
          connectDatabase();
          selectToUserpstmt = conn.prepareStatement(selectTouserSQL);
        }
        rst = selectToUserpstmt.executeQuery(); //获得记录集
        while (rst.next()) {
          try {
            messageID = rst.getInt("SUBMIT_ID");
            System.out.println("messageID" + messageID);
            String server_code = rst.getString("Service_Code");
            System.out.println("Service_Code" + server_code);
            String charged_party_id = rst.getString("Charged_Party_ID");
            System.out.println("charged_party_id" + charged_party_id);
            String to_no = rst.getString("To_No");
            System.out.println("To_No" + to_no);
            String cc_no = rst.getString("Cc_No");
            System.out.println("cc_no" + cc_no);
            String bbc_no = rst.getString("Bcc_No");
            System.out.println("bbc_no" + bbc_no);
            String mms_subjet = rst.getString("MMS_subjet");
            System.out.println("mms_subjet" + mms_subjet);
//           mms_subjet=new String(mms_subjet.getBytes("ISO8859_1"),"GBK");
//          mms_subjet=new String(mms_subjet.getBytes("UTF-8"),"GB2312");
            String mms_content_type = rst.getString("MMS_Content_Type");
            System.out.println("mms_content_type" + mms_content_type);
            String content_Id_list = rst.getString("Content_Id_List");
            System.out.println("content_Id_list" + content_Id_list);

            MMessage m = new MMessage();
            m.setSubject(mms_subjet);
            m.setContentType(mms_content_type);
            if (to_no == "" || to_no == null) {
              System.out.println("SendFromDB getMMS to_no is null !");
              return; //不执行
            }
            else {

              StringTokenizer st = new StringTokenizer(to_no, ";");
              while (st.hasMoreTokens()) {
                m.addTo(st.nextToken().trim());
              }
            }
            if (cc_no != "" && cc_no != null) {
              StringTokenizer st = new StringTokenizer(cc_no, ";");
              while (st.hasMoreTokens()) {
                m.addCc(st.nextToken().trim());
              }
            }
            if (bbc_no != "" && bbc_no != null) {
              StringTokenizer st = new StringTokenizer(bbc_no, ";");
              while (st.hasMoreTokens()) {
                m.addBcc(st.nextToken().trim());
              }
            }
//          MMSContent mmc = new MMSContent();
//          byte content[] = null;
//          try
//          {
//              content = (new String("嘿嘿")).getBytes("UTF8");
//          }
//          catch(Exception e)
//          {
//              content = new byte[0];
//              e.printStackTrace();
//          }
//          mmc.setContent(content, 0, content.length);
//          mmc.setType("text/plain");
//          mmc.setContentId("内容ID");
//         m.addContent(mmc);

            StringTokenizer st = new StringTokenizer(content_Id_list, ";");
            int index_content_Id = 0; //
            while (st.hasMoreTokens()) {
              index_content_Id++;
              String contentId = st.nextToken().trim();
              ContentDesc contentTemp = getContent(contentId);
              MMSContent mmc = new MMSContent();
              byte content[] = null;
              if (contentTemp.contentDescType == 0) { //字符串
                System.out.println("Get MMS Content contentTemp.contentDesc" +
                                   contentTemp.contentDesc);
                try {
                  content = (new String(contentTemp.contentDesc)).getBytes(
                      "UTF8");
                }
                catch (Exception e) {
                  content = new byte[0];
                  System.out.println("ContentDesc Convert Errors:" + e);
                }

              }
              else if (contentTemp.contentDescType == 1) { // 文件路径
                System.out.println(
                    "Get MMS Contentn file path contentTemp.contentDesc" +
                    contentTemp.contentDesc);
                try {
                  content = readFromFile(contentTemp.contentDesc);
                  if (contentTemp.contentType.equals("text/plain")) {
                    System.out.println(
                        "contentTemp.contentDescType==1 && is content text");
                    content = (new String(content)).getBytes("UTF8");
                  }
                }

                catch (Exception e) {
                  content = new byte[0];
                  System.out.println("ReadFromFile Error: " + e);
                }
              }
              else {
                content = new byte[0];
                System.out.println(
                    " Get Content contentTemp.contentDescType Error: contentTemp.contentDescType= " +
                    contentTemp.contentDescType);
              }

              mmc.setContent(content, 0, content.length);
              mmc.setType(contentTemp.contentType);
              if (index_content_Id == 1 &&
                  m.getContentType().equalsIgnoreCase(
                  "application/vnd.wap.multipart.related")) {
                System.out.println("11111111111111111111111111111111111" +
                                   contentTemp.contentId);
                m.setPresentationId(contentTemp.contentId);
              }
              else if (index_content_Id > 1 &&
                       m.getContentType().
                       equalsIgnoreCase("application/vnd.wap.multipart.related")) {
                mmc.setContentLocation(contentTemp.contentId);
                System.out.println("11111111111111111111111111111111112" +
                                   contentTemp.contentId);
              }
              mmc.setContentId(contentTemp.contentId);
              m.addContent(mmc);
            }
            SendManager.instance().send(charged_party_id, to_no, server_code, m); //发送
            updateAlreadysend2User(messageID);
          }
          catch (Exception ex) {
            System.out.println("ResultSet Incomplete Error: " + ex);
            deleteAlreadyAttendUser(messageID);
          }
        }
        rst.close();
      }
      catch (Exception e) {
        System.out.println("Circulating operation Error:" + e);
      }
    }
  }

  public static byte[] readFromFile(String file_name) throws IOException {
    FileInputStream fin = new FileInputStream(file_name);
    byte buf[] = new byte[fin.available()];
    fin.read(buf);
    fin.close();
    return buf;
  }

  public ContentDesc getContent(String id) {
    ContentDesc content_desc = new ContentDesc();
    try {
      System.out.println("getContent id=" + id);
      selectTousercxpstmt.setString(1, id);
      ResultSet rscx = selectTousercxpstmt.executeQuery();
      while (rscx.next()) {
        content_desc.contentId = rscx.getString("Content_Id");
        content_desc.contentType = rscx.getString("MMC_Content_type");
        content_desc.contentDescType = rscx.getInt("Content_decs_type");
        content_desc.contentDesc = rscx.getString("Content_decs");
        return content_desc;
      }
      rscx.close();
    }
    catch (Exception e) {
      System.out.println("getContent Error: " + e);
    }
    return content_desc;
  }

  //更新己发送给用户的记录
  private boolean updateAlreadysend2User(int id) {
    try {
      updateToUserpstem.setInt(1, id);
      if (updateToUserpstem.executeUpdate() > 1)
        return true;
    }
    catch (Exception e) {
      System.out.println(" updateToUserpstmt Error: " + e);
    }
    return false;
  }

  //删除己处理过的用户
  private boolean deleteAlreadyAttendUser(int id) {
    try {
      deleteToUserpstem.setInt(1, id);
      if (deleteToUserpstem.executeUpdate() > 1)
        return true;
    }
    catch (Exception e) {
      System.out.println(" deleteAlreadyAttendUser Error: " + e);
    }
    return true;
  }

  private static SendFromDB sendFromDb;
}

class ContentDesc {
  public String contentId;
  public String contentType;
  public int contentDescType;
  public String contentDesc;
}

⌨️ 快捷键说明

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