Any suggestions of doing refreshing data in a web module?

Discussion in 'Java' started by lightning, Mar 25, 2008.

  1. lightning

    lightning Guest

    Some data(xxx.txt) may be updated at some time.
    I start a new thread to refresh the data.
    Below is the code:


    public class IPLookerServlet extends HttpServlet {
    static TreeMap<Long, String> map;

    static ExecutorService s = Executors.newSingleThreadExecutor();

    static String ipRepFile;

    static boolean isRunning = true;

    /**
    * Destruction of the servlet. <br>
    */
    public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
    System.out.println("it destroyed!!!!!!!!!!!!!!");
    isRunning = false;
    s.shutdown();
    }

    /**
    * Initialization of the servlet. <br>
    *
    * @throws ServletException
    * if an error occure
    */
    public void init() throws ServletException {
    // Put your code here
    ipRepFile = getInitParameter("ipRepFile");
    try {
    map = getIPInfo(ipRepFile);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    s.execute(new Runnable() {
    int c = 0;

    public void run() {
    // TODO Auto-generated method stub
    while (isRunning) {
    try {
    Thread.sleep(1000);
    c = (c++) % (30 * 60);//every half an hour.
    if (c == 0) {
    map = getIPInfo(ipRepFile);
    System.out.println("refreshed ip repository");
    }
    } catch (IOException e) {
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    });
    System.out.println("init
    complete!!!!!!!!!");

    }

    private static TreeMap<Long, String> getIPInfo(String file) throws
    IOException {
    TreeMap<Long, String> r = new TreeMap<Long, String>();
    BufferedReader in = new BufferedReader(new FileReader(file));
    String buf = null;
    Pattern p = Pattern
    .compile("^(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s+(\\d+\\.\\d+\\.\\d+\\.\\d
    +)\\s+(.+)");
    while ((buf = in.readLine()) != null) {
    Matcher m = p.matcher(buf);
    while (m.find()) {
    long start = dealDot(m.group(1));
    r.put(start, m.group(3));
    }
    }
    in.close();
    return r;
    }
    ....
    }
     
    lightning, Mar 25, 2008
    #1
    1. Advertising

  2. lightning

    lightning Guest

    And I start the servlet at the container's start


    <servlet>
    <servlet-name>IPLookerServlet</servlet-name>
    <servlet-class>
    com.youliao.servlets.config.IPLookerServlet
    </servlet-class>
    <init-param>
    <param-name>ipRepFile</param-name>
    <param-value>e:\IPData.txt</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
    </servlet>



    On 3ÔÂ25ÈÕ, ÉÏÎç11ʱ50·Ö, lightning <> wrote:
    > Some data(xxx.txt) may be updated at some time.
    > I start a new thread to refresh the data.
    > Below is the code:
    >
    > public class IPLookerServlet extends HttpServlet {
    > static TreeMap<Long, String> map;
    >
    > static ExecutorService s = Executors.newSingleThreadExecutor();
    >
    > static String ipRepFile;
    >
    > static boolean isRunning = true;
    >
    > /**
    > * Destruction of the servlet. <br>
    > */
    > public void destroy() {
    > super.destroy(); // Just puts "destroy" string in log
    > // Put your code here
    > System.out.println("it destroyed!!!!!!!!!!!!!!");
    > isRunning = false;
    > s.shutdown();
    > }
    >
    > /**
    > * Initialization of the servlet. <br>
    > *
    > * @throws ServletException
    > * if an error occure
    > */
    > public void init() throws ServletException {
    > // Put your code here
    > ipRepFile = getInitParameter("ipRepFile");
    > try {
    > map = getIPInfo(ipRepFile);
    > } catch (IOException e) {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > }
    >
    > s.execute(new Runnable() {
    > int c = 0;
    >
    > public void run() {
    > // TODO Auto-generated method stub
    > while (isRunning) {
    > try {
    > Thread.sleep(1000);
    > c = (c++) % (30 * 60);//every half an hour.
    > if (c == 0) {
    > map = getIPInfo(ipRepFile);
    > System.out.println("refreshed ip repository");
    > }
    > } catch (IOException e) {
    > } catch (InterruptedException e) {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > }
    > }
    > }
    > });
    > System.out.println("init
    > complete!!!!!!!!!");
    >
    > }
    >
    > private static TreeMap<Long, String> getIPInfo(String file) throws
    > IOException {
    > TreeMap<Long, String> r = new TreeMap<Long, String>();
    > BufferedReader in = new BufferedReader(new FileReader(file));
    > String buf = null;
    > Pattern p = Pattern
    > .compile("^(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s+(\\d+\\.\\d+\\.\\d+\\.\\d
    > +)\\s+(.+)");
    > while ((buf = in.readLine()) != null) {
    > Matcher m = p.matcher(buf);
    > while (m.find()) {
    > long start = dealDot(m.group(1));
    > r.put(start, m.group(3));
    > }
    > }
    > in.close();
    > return r;
    > }
    > ...
    >
    >
    >
    > }- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -
     
    lightning, Mar 25, 2008
    #2
    1. Advertising

  3. lightning

    lightning Guest

    Some corrections...:

    c = (c+1) % (5 * 60);
    and field c is put in IPLookerServlet now.

    ...

    static String ipRepFile;

    static boolean isRunning = true;
    static int c = 0;
    ...

    On 3ÔÂ25ÈÕ, ÉÏÎç11ʱ50·Ö, lightning <> wrote:
    > Some data(xxx.txt) may be updated at some time.
    > I start a new thread to refresh the data.
    > Below is the code:
    >
    > public class IPLookerServlet extends HttpServlet {
    > static TreeMap<Long, String> map;
    >
    > static ExecutorService s = Executors.newSingleThreadExecutor();
    >
    > static String ipRepFile;
    >
    > static boolean isRunning = true;
    >
    > /**
    > * Destruction of the servlet. <br>
    > */
    > public void destroy() {
    > super.destroy(); // Just puts "destroy" string in log
    > // Put your code here
    > System.out.println("it destroyed!!!!!!!!!!!!!!");
    > isRunning = false;
    > s.shutdown();
    > }
    >
    > /**
    > * Initialization of the servlet. <br>
    > *
    > * @throws ServletException
    > * if an error occure
    > */
    > public void init() throws ServletException {
    > // Put your code here
    > ipRepFile = getInitParameter("ipRepFile");
    > try {
    > map = getIPInfo(ipRepFile);
    > } catch (IOException e) {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > }
    >
    > s.execute(new Runnable() {
    > int c = 0;
    >
    > public void run() {
    > // TODO Auto-generated method stub
    > while (isRunning) {
    > try {
    > Thread.sleep(1000);
    > c = (c++) % (30 * 60);//every half an hour.
    > if (c == 0) {
    > map = getIPInfo(ipRepFile);
    > System.out.println("refreshed ip repository");
    > }
    > } catch (IOException e) {
    > } catch (InterruptedException e) {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > }
    > }
    > }
    > });
    > System.out.println("init
    > complete!!!!!!!!!");
    >
    > }
    >
    > private static TreeMap<Long, String> getIPInfo(String file) throws
    > IOException {
    > TreeMap<Long, String> r = new TreeMap<Long, String>();
    > BufferedReader in = new BufferedReader(new FileReader(file));
    > String buf = null;
    > Pattern p = Pattern
    > .compile("^(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s+(\\d+\\.\\d+\\.\\d+\\.\\d
    > +)\\s+(.+)");
    > while ((buf = in.readLine()) != null) {
    > Matcher m = p.matcher(buf);
    > while (m.find()) {
    > long start = dealDot(m.group(1));
    > r.put(start, m.group(3));
    > }
    > }
    > in.close();
    > return r;
    > }
    > ...
    >
    >
    >
    > }- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -
     
    lightning, Mar 25, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harlan Messinger

    Self-refreshing and non-self-refreshing controls

    Harlan Messinger, Aug 4, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    470
    =?Utf-8?B?Y2xpY2tvbg==?=
    Aug 4, 2006
  2. Simon
    Replies:
    13
    Views:
    637
    Eric Sosman
    Mar 25, 2011
  3. Simon Ng
    Replies:
    5
    Views:
    262
  4. Simon Ng
    Replies:
    5
    Views:
    224
  5. Harlan Messinger

    Self-refreshing and non-self-refreshing controls

    Harlan Messinger, Aug 3, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    194
    Harlan Messinger
    Aug 3, 2006
Loading...

Share This Page