线程中查询与处理数据提取到方法,数据处理完后线程在行休眠

juor 发布于 2015/09/30 21:41
阅读 170
收藏 1

@mycms2013 你好,想跟你请教个问题:

将线程中查询数据与处理提到方法中:

之前这样作是在查询完200条进行处理后线程休眠,现在要改为查询和处理还是一样每次200条,只是查询处理完数据库记录后线程在休眠,该怎么实现,请指教

public static void main(String[] args) {
  new MASSdkSend();

 }
 public void query() {
  logger.info("query 方法启动……");
  Thread query = new Thread() {
   public void run() {
    logger.info("Entry  query method Thread run method……");
    while (true) {
     Connection _conn = ConnectionManager.getConnection(null, null);
     logger.info("query Thread create Connection Object:[{}]",_conn);
     String selectsms = "select sms_id,cellphone,content from com_sms_send where user_define1 is null and (sendtime<now() or sendtime is null) LIMIT 0,200";
     String updatesms = "update com_sms_send set user_define1=?, user_define2=? where sms_id=? and user_define1 is null";
     String _cellphone = "", _content = "", _msgGroupinfo = "";
     int _sms_id = 0;
     try {
      PreparedStatement _spstmt = _conn
        .prepareStatement(selectsms);
      PreparedStatement _upstmt = _conn
        .prepareStatement(updatesms);
      ResultSet _rs = _spstmt.executeQuery();
      logger.info("Method query executeQuery:[{}].",selectsms);
      while (_rs.next()) {
       _msgGroupinfo = UUIDHexGenerator
         .generator();
       _cellphone = _rs.getString("cellphone");
       _content = _rs.getString("content");
       _sms_id = _rs.getInt("sms_id");
       int sendResult = client.sendDSMS(
         new String[] { _cellphone },
         _content, "", 1,
         SystemGlobalArea.SMSDBAUTH,
         _msgGroupinfo,
         SystemGlobalArea.SMSAUTOSEND);
       if (null != String.valueOf(sendResult)) {
        _upstmt.setString(1,
          String.valueOf(sendResult));
        _upstmt.setString(2, _msgGroupinfo);
        _upstmt.setInt(3, _sms_id);
        _upstmt.executeUpdate();
        logger.info("号码:[{}],内容:[{}],状态:[{}],批次号:[{}]",_cellphone,_content,sendResult,_msgGroupinfo);
        logger.info("Method query executeUpdate:[{}].",updatesms);
       }
      }
     } catch (SQLException e) {
      e.printStackTrace();
      logger.error("Method query exception:[{}]",e);
     }finally {
      ConnectionManager.freeConnection(null, null, _conn);
      logger.info("Method query Connection object close.");
     }
     try {
      Thread.sleep(60000);
      logger.info("Method query Thread Entry sleep……");
     } catch (Exception e) {
      e.printStackTrace();
      logger.error("Method query Thread sleep exception.");
     }
    }
   }
  };
  query.start();
  logger.info("线程 query 启动……");
 }

//数据查询与处理方法

public void porecce(){}

加载中
0
r
runrunsnail

说实在的,不知道你在说啥,之前,之后的叙述是一样的赶脚- -!!!

你给的线程内代码是这样的:循环开始→数据查询→数据处理→线程休眠→循环结束

你的需求是啥?

juor
juor
当前是处理完这批查询出来的数据后线程就休眠了对不?意思就是数据库还有没查询出来的数据要处理,我要把数据库查询处理完,线程在休眠,这样就是把查询到处理那段代码要循环处理完是不?该怎么放到方法中去
0
r
runrunsnail

看了你其他提问,貌似要提取【数据查询→数据处理】到一个方法中,拷贝粘贴很难吗?- -!!

你是不知道提取后怎么调用吗?有点晕@¥@

juor
juor
对,就是在方法里面怎么循环取完数据
0
r
runrunsnail
public static void main(String[] args) {
 new MASSdkSend();


}
public void query() {
 logger.info("query 方法启动……");
 Thread query = new Thread() {
  public void run() {
  
  porecce();
  
    try {
     Thread.sleep(60000);
     logger.info("Method query Thread Entry sleep……");
    } catch (Exception e) {
     e.printStackTrace();
     logger.error("Method query Thread sleep exception.");
    }
  }
 };
 query.start();
 logger.info("线程 query 启动……");
}


//数据查询与处理方法


public static void porecce(){
   logger.info("Entry  query method Thread run method……");
   while (true) {
    Connection _conn = ConnectionManager.getConnection(null, null);
    logger.info("query Thread create Connection Object:[{}]",_conn);
    String selectsms = "select sms_id,cellphone,content from com_sms_send where user_define1 is null and (sendtime<now() or sendtime is null) LIMIT 0,200";
    String updatesms = "update com_sms_send set user_define1=?, user_define2=? where sms_id=? and user_define1 is null";
    String _cellphone = "", _content = "", _msgGroupinfo = "";
    int _sms_id = 0;
    try {
     PreparedStatement _spstmt = _conn
       .prepareStatement(selectsms);
     PreparedStatement _upstmt = _conn
       .prepareStatement(updatesms);
     ResultSet _rs = _spstmt.executeQuery();
     logger.info("Method query executeQuery:[{}].",selectsms);
     while (_rs.next()) {
      _msgGroupinfo = UUIDHexGenerator
        .generator();
      _cellphone = _rs.getString("cellphone");
      _content = _rs.getString("content");
      _sms_id = _rs.getInt("sms_id");
      int sendResult = client.sendDSMS(
        new String[] { _cellphone },
        _content, "", 1,
        SystemGlobalArea.SMSDBAUTH,
        _msgGroupinfo,
        SystemGlobalArea.SMSAUTOSEND);
      if (null != String.valueOf(sendResult)) {
       _upstmt.setString(1,
         String.valueOf(sendResult));
       _upstmt.setString(2, _msgGroupinfo);
       _upstmt.setInt(3, _sms_id);
       _upstmt.executeUpdate();
       logger.info("号码:[{}],内容:[{}],状态:[{}],批次号:[{}]",_cellphone,_content,sendResult,_msgGroupinfo);
       logger.info("Method query executeUpdate:[{}].",updatesms);
      }
     }
    } catch (SQLException e) {
     e.printStackTrace();
     logger.error("Method query exception:[{}]",e);
    }finally {
     ConnectionManager.freeConnection(null, null, _conn);
     logger.info("Method query Connection object close.");
    }
   }


}
r
runrunsnail
回复 @juor : porecce方法里是循环处理,每次循环处理200条,直到处理完,porecce也就结束了,然后休眠。不是你的需求吗?
juor
juor
这样没有意义吧?每次查询200条,线程就休眠了,还有数据没处理完呢,应该是每次查询处理200条,直到查询处理完,线程才休眠对
返回顶部
顶部