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.

相关文章
相关标签/搜索