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

📄 sendforsub.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.*;

/**
 *
 * <p>Title: </p>
 * <p>Description: 定期检查数据库,查询是否有订阅下发消息需要发送</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
public class SendForSub
    extends Thread {
  private static SendForSub _instance_SFS;
  private static Connection conn = null;
  private static ResultSet rst = null;
  private static PreparedStatement selectListpstmt = null;
  private static PreparedStatement selectContentpstmt = null;
  private static PreparedStatement updateListpstmt = null;
  private static PreparedStatement selectSubuserpstmt = null;
  private String selectList = "select * from t_month_send where send_status=1 and to_char(settlement_time,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd') and rownum<6";
  private String selectContent = "select Content_Id,MMC_Content_type,Content_decs_type,Content_decs from T_MMS_Content where Content_id =?";
  private String updateList =
      "update t_month_send set send_status=2,Send_time=sysdate where month_send_id=?";
  private String selectSubuser =
      "select * from t_user_info where service_code =? and exit_flag=0";
  public SendForSub() {
    connectDatabase();
    try {
      selectListpstmt = conn.prepareStatement(selectList);
      selectContentpstmt = conn.prepareStatement(selectContent);
      updateListpstmt = conn.prepareStatement(updateList);
      selectSubuserpstmt = conn.prepareStatement(selectSubuser);
    }
    catch (SQLException ex) {
    }
  }

  public static SendForSub getInstance() {
    if (_instance_SFS == null) {
      _instance_SFS = new SendForSub();
    }
    return _instance_SFS;
  }

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

  public void run() {
    int messageID = 0;
    try {
      if (conn == null) {
        connectDatabase();
        selectListpstmt = conn.prepareStatement(selectList);
      }

      while (true) {
        rst = selectListpstmt.executeQuery();
        while (rst.next()) {
          messageID = rst.getInt("month_send_id");
          String service_code = rst.getString("service_code");
          String mms_subject = rst.getString("mms_subject");
          String mms_content_type = rst.getString("mms_content_type");
          String content_list = rst.getString("content_id_list");
          selectSubuserpstmt.setString(1, service_code);
          ResultSet rstx = selectSubuserpstmt.executeQuery();
          while (rstx.next()) {
            String message_from = rstx.getString("charged_party_id");
            String to_no = rstx.getString("to_no");
            MMessage m = new MMessage();
            m.setSubject(mms_subject);
            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());
              }
            }
            StringTokenizer st = new StringTokenizer(content_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(message_from, to_no, service_code, m);

          }
          updateAlreadysend(messageID);
        }
      }
    }
    catch (Exception ex) {
      System.out.println("SendForSub Circulating operation Error:" + ex);
    }
  }

  public ContentDesc getContent(String id) {
    ContentDesc content_desc = new ContentDesc();
    try {
      System.out.println("getContent id=" + id);
      selectContentpstmt.setString(1, id);
      ResultSet rscx = selectContentpstmt.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;
  }

  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;
  }

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

}

⌨️ 快捷键说明

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