package net.hlj.util;import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;public class MyListener implements ServletContextListener { private Timer timer = null; public void contextInitialized(ServletContextEvent event) {
timer = new Timer(true);
System.out.println("定时任务起动");
// 设置任务计划,启动和间隔时间
timer.schedule(new GetStockBatch(), 0, 5000); } public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
}}

解决方案 »

  1.   


    package net.hlj.util; 
    import java.io.BufferedReader;import java.io.InputStreamReader;
    import java.net.URL;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
     
    import java.util.ArrayList;
    import java.util.Date;
     
    import java.util.TimerTask;
     import net.hlj.model.StockModel;
     public class GetStockBatch   extends TimerTask  implements Runnable {
    public String getContent(String strUrl)
    // 一个public方法,返回字符串,错误则返回"error open url"
    {
    try { URL url = new URL(strUrl);
    BufferedReader br = new BufferedReader(new InputStreamReader(url
    .openStream()));
    String s = "";
    StringBuffer sb = new StringBuffer("");
    while ((s = br.readLine()) != null) {
    sb.append(s + "\r\n");
    }
    br.close();
    return sb.toString();
    } catch (Exception e) {
    return "error open url" + strUrl; }
    } public static void stock(int group) {
    System.out.println("-----------------------"+group);
    Connection conn = null;
    String code = "";
    ResultSet rs = null;
    ArrayList<StockModel> list=new ArrayList<StockModel>();
    try {
    DBConnection db = new DBConnection();
    conn = db.getConnection();
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    String stock[] = null;
    Date date1 = new Date();
    try {

    String sql = "select  code ,type from tbl_stock where thread="+group+"   order by id desc ";
    String sql2 = "";
    Statement stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
    while (rs.next()) {

    code = rs.getString("code");
    //System.out.println(code);
    String type = rs.getString("type");
    stock = getStock(type + code);
    String updown = "0";
    float up_down = 0;
    if (stock[4] != null && !stock[4].equals("")
    && stock[3] != null && !stock[3].equals("")
    && !stock[3].equals("0")) {
    up_down = (Float.parseFloat(stock[4]) - Float
    .parseFloat(stock[3]))
    / Float.parseFloat(stock[3]);
    updown = UtilTool.twofloat(up_down * 100);
    }
    StockModel obj=new StockModel();
    obj.setType(type);
         obj.setNowPrice(stock[4]);
    obj.setUpDown(updown);
    obj.setTotal(stock[9]);
    obj.setClosePrice(stock[3]);
    obj.setOpenPrice(stock[2]);
    obj.setHeightPrice(stock[5]);
    obj.setLowPrice(stock[6]);
    obj.setBuy1(stock[12]);
    obj.setBuy2(stock[14]);
    obj.setBuy3(stock[16]);
    obj.setBuy4(stock[18]);
    obj.setBuy5(stock[20]);

    obj.setCode(code);
    list.add(obj);
        }
    stmt.close();
    rs.close();

    } catch (Exception e) {
    System.out.println("stock ======================================================"+code);
    e.printStackTrace(); }
    try {
    StockModel sm= new StockModel();
      String  sql2 = "update tbl_stock set nowPrice =cast (? as money),upDown=cast (? as money),total=? ,closePrice=cast (? as money) ,openPrice=cast (? as money) ,heightPrice=cast (? as money) "
    + ",lowPrice =cast (? as money) "
    + ",buy1 =cast (? as money) "
    + ",buy2 =cast (? as money) "
    + ",buy3 =cast (? as money) "
    + ",buy4 =cast (? as money) "
    + ",buy5 =cast (? as money) "
    + ",sale1 =cast (? as money) "
    + ",sale2 =cast (? as money) "
    + ",sale3 =cast (? as money) "
    + ",sale4 =cast (? as money) "
    + ",sale5 =cast (? as money) "
    + ",buy1Num =cast (? as money) "
    + ",buy2Num =cast (? as money) "
    + ",buy3Num =cast (? as money) "
    + ",buy4Num =cast (? as money) "
    + ",buy5Num =cast (? as money) "
    + ",sale1Num =cast (? as money) "
    + ",sale2Num =cast (? as money) "
    + ",sale3Num =cast (? as money) "
    + ",sale4Num =cast (? as money) "
    + ",sale5Num =cast (? as money) "
    + ",addDate = ? "
    + ",datetime = ? " + "where code=?";
    String sql3 = "insert into tbl_stockList(nowPrice,upDown,total,closePrice,openPrice,heightPrice,lowPrice,"
    + "buy1,buy2,buy3,buy4,buy5,sale1,sale2,sale3,sale4,sale5,"
    + "buy1Num,buy2Num,buy3Num,buy4Num,buy5Num,sale1Num,sale2Num,"
    + "sale3Num,sale4Num,sale5Num,code,type,addDate,datetime)values(cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),cast (? as money),?,?,?,?)"; PreparedStatement stmt3 = conn.prepareStatement(sql3);
    PreparedStatement stmt2 = conn.prepareStatement(sql2);
    java.util.Iterator it=list.iterator();
    while(it.hasNext()) 
    {

    sm=(StockModel)it.next();
    stmt3.setString(1,sm.getNowPrice() );
    ----------------------

    //-----------------------------------
    stmt2.setString(1,sm.getNowPrice() );
    stmt2.setString(2, sm.getUpDown());
    stmt2.setString(3, sm.getTotal());
    stmt2.setString(4, sm.getClosePrice());
    stmt2.setString(5, sm.getOpenPrice());
    stmt2.addBatch();
      stmt3.addBatch();

        
     
    }
      stmt2.executeBatch(); 
      stmt3.executeBatch(); 
      stmt2.close();
      stmt3.close();

    //----------------------------------------
     
    //-----------------------------------------
      conn.close(); } catch (Exception e) {
    System.out.println("stockInsert  ======================================================"+code);
    e.printStackTrace(); }



    Date date2 = new Date();
     
    System.out.println("time: " + (date2.getTime() - date1.getTime())+ " total seconds"); 
    } public static String[] getStock(String code) { GetStockBatch ou = new GetStockBatch();
    String url = "http://hq.sinajs.cn/list=" + code;
    String str = ou.getContent(url);
    // System.out.println(url);
    String s = str.substring(str.indexOf("hq_str_") + 7, str.indexOf("="));
    String s2 = str.substring(str.indexOf("=") + 1, str.length());
    String s3 = (s + "," + s2).replace("\"", "");
    String stock[] = s3.replace(";", "").split(",");
    for (int k = 0; k < stock.length; k++) {
    //System.out.println(stock[k]);
    }
    return stock;
    } @Override
    public void run() {


    boolean isopen=UtilTool.openTime();

    if(isopen)
    {

     StockThead1 st1=new StockThead1();
     StockThead2 st2=new StockThead2();
     StockThead3 st3=new StockThead3();
     StockThead4 st4=new StockThead4();
     StockThead5 st5=new StockThead5();
     StockThead6 st6=new StockThead6();
     StockThead7 st7=new StockThead7();
     StockThead8 st8=new StockThead8();
     new Thread(st1).start();
     new Thread(st2).start();
    //  new Thread(st3).start();
    //  new Thread(st4).start();
    //  new Thread(st5).start();
    //  new Thread(st6).start();
    //  new Thread(st7).start();
    //  new Thread(st8).start();
    ////


    }

    }
     

    public static void main(String a[]) { stock( 1); }
    }
    class StockThead1 implements Runnable {    int group=1;
      public void run() {
     
     GetStockBatch.stock(group);
     
     }
    }  class StockThead2 implements Runnable {     int group=2;
      public void run() {
     
       GetStockBatch.stock(group);
       

     }
    }  class StockThead3 implements Runnable {     int group=3;
      public void run() {
     
       GetStockBatch.stock(group);
       

     }
    }
      class StockThead4 implements Runnable {     int group=4;
      public void run() {
     
       GetStockBatch.stock(group);
       

     }
    }
      class StockThead5 implements Runnable {     int group=5;
      public void run() {
     
       GetStockBatch.stock(group);
       

     }
    }
      class StockThead6 implements Runnable {     int group=6;
      public void run() {
     
       GetStockBatch.stock(group);
       

     }
    }
      class StockThead7 implements Runnable {     int group=7;
      public void run() {
     
       GetStockBatch.stock(group);
       

     }
    }
      class StockThead8 implements Runnable {     int group=8;
      public void run() {
     
      GetStockBatch.stock(group);
       

     }
    }