在java web项目中我们通常会有这样的需求:当项目启动时执行一些初始化操作,例如从数据库加载全局配置文件,或者加载必要的配置信息进行初始化等,.
常用的方法有两种,第一种是自定义监听(Listener),第二种是配置随项目启动而启动的Servlet,在初始化方法里面实现自己的功能需求.
下面对这两种方法做一简单的介绍:
1.监听
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() 方法,用来做数据处理
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> </web-app>
部署运行结果如下:
2. 继承HttpServlet
1.首先创建servlet,代码如下:
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.