Web服務啓動加載線程

在java web項目中咱們一般會有這樣的需求:當項目啓動時執行一些初始化操做,例如從數據庫加載全局配置文件,或者加載必要的配置信息進行初始化等,.java

經常使用的方法有兩種,第一種是自定義監聽(Listener),第二種是配置隨項目啓動而啓動的Servlet,在初始化方法裏面實現本身的功能需求git

下面對這兩種方法作一簡單的介紹:github

1.監聽web

 1.建一個監聽類,重寫 ServletContextListener的 contextInitialized和contextDestroyed 方法數據庫

package com.mindfind.thread;

import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
/**
 * Created by Ktao on 9/6/16.
 */
public class MyListenerWay implements ServletContextListener {
    private MyThreadTest myThreadTest;

    public void contextDestroyed(ServletContextEvent e) {
        if (myThreadTest != null && myThreadTest.isInterrupted()) {
            myThreadTest.interrupt();
        }
    }

    public void contextInitialized(ServletContextEvent e) {
        String str = null;
        if (str == null && myThreadTest == null) {
            myThreadTest = new MyThreadTest();
            myThreadTest.start(); // servlet 上下文初始化時啓動 socket
        }
    }
}

  2.定義一個線程類繼承自線程類,重寫 run() 方法,用來作數據處理app

package com.mindfind.thread;

/**
 * Created by wangtao on 9/6/16.
 */
public class MyThreadTest extends Thread {
    public void run() {
        while (!this.isInterrupted()) {// 線程未中斷執行循環
            try {
                Thread.sleep(1000); //每隔1000ms執行一次
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("test begin:" + System.currentTimeMillis());
        }
    }
}

  3.在web.xml配置 socket

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

    <listener>
        <listener-class> com.mindfind.thread.MyListenerWay</listener-class>
    </listener>
</web-app>

 

部署運行結果以下:測試

 

2. 繼承HttpServletthis

  1.首先建立servlet,代碼以下:spa

package com.mindfind.thread;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by wangtao on 9/6/16.
 */
public class MyWayServlet extends HttpServlet {

    private MyThreadTest threadTest;

    public MyWayServlet() {}

    public void init(){
        if(threadTest == null) {
            threadTest = new MyThreadTest();
            threadTest.start();
        }
    }

    public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
            throws ServletException, IOException {
    }

    public void destory(){
        if (threadTest != null && threadTest.isInterrupted()) {
            threadTest.interrupt();
        }
    }
}

    2.線程類用剛纔現成的

package com.mindfind.thread;

/**
 * Created by wangtao on 9/6/16.
 */
public class MyThreadTest extends Thread {
    public void run() {
        while (!this.isInterrupted()) {
            try {
                Thread.sleep(1000); //每隔1000ms執行一次
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("test begin:" + System.currentTimeMillis());
        }
    }
}

    3.在web.xml中配置以下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

    <!--<listener>
        <listener-class> com.mindfind.thread.MyListenerWay</listener-class>
    </listener>-->
    <!-- LISTENNING THREAD -->
    <servlet>
        <servlet-name>MyWayServlet</servlet-name>
        <servlet-class>com.mindfind.thread.MyWayServlet</servlet-class>
        <!--load-on-startup 數字和優先級成反比,都是大於0 -->
        <load-on-startup>9</load-on-startup>
    </servlet>
</web-app>

 

運行結果以下:

 

   對比以上兩種方式,Listener的啓動方式在時間上應該優先於任何一個Servlet,而Servlet的方式,能夠更好地設置或者調整某部分啓動的順序,這在某些時候頗有用處。

    以上測試的代碼,見gitHub:https://github.com/wangtao1/WebStart.

相關文章
相關標籤/搜索