服务启动自动运行类方法
利用ServletContextListener类来实现:
?
package com.autorun;
?
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
?
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;
?
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
?
@SuppressWarnings("serial")
public class AutoRun extends HttpServlet implements ServletContextListener {
/**
?* 服务器停止前自动执行
?*/
@Override
public void contextDestroyed(ServletContextEvent arg0) {
?DeleteFile();
}
/**
* 服务器启动后自动运行
*/
@Override
public void contextInitialized(ServletContextEvent arg0) {
WriteFile();
ApplicationContext app = new ClassPathXmlApplicationContext("classpath*:spring/*.xml");
? ? ? ? ? ? ? ? ?//使用线程池启动一个线程进行轮询队列
? ? ? ? ? ? ? ? ?MasterGlobalResource.getExecutor().execute((JobService)app.getBean("jobService"));
}
?public void WriteFile() {
? ? ? ?try {
? ? ? ?System.out.println("write");
? ? ? ? ? ?FileWriter fw = new FileWriter("c:/WriteData.txt");
? ? ? ? ? ?// 将字符串写入文件
? ? ? ? ? ?fw.write("Hello World!");
? ? ? ? ? ?fw.write("Hello Everyone!");
? ? ? ? ? ?fw.close();
? ? ? ?} catch (IOException e) {
? ? ? ?}
? ?}
?
? ?public void DeleteFile() {
? ? ? ?File f = new File("c:/WriteData.txt");
? ? ? ?// 检查文件是否存在,如果存在,直接删除文件
? ? ? ?if (f.exists()) {
? ? ? ? ? ?f.delete();
? ? ? ?}
? ?}
?
}
web.xml配置监听:<listener>? ? <listener-class>com.autorun.AutoRun</listener-class>? ? </listener>MasterGlobalResource类如下:package com.autorun;
import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;
public ?class MasterGlobalResource {//线程池private static ThreadPoolExecutor ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?executor ? ? ? ? ? ? ? ? = new ThreadPoolExecutor(? ? ? ? ? ? ?10,? ? ? ? ? ? ?10,? ? ? ? ? ? ?0L,? ? ? ? ? ? ?TimeUnit.MILLISECONDS,? ? ? ? ? ? ?new LinkedBlockingQueue<Runnable>(),? ? ? ? ? ? ?new ThreadPoolExecutor.DiscardPolicy());?//队列 private static LinkedBlockingQueue<ZephyrJobWrapper> ? ? ? ? ? ? waitingJobQueue ? ? ? ? ?= new LinkedBlockingQueue<ZephyrJobWrapper>();?? public static ThreadPoolExecutor getExecutor() { ? ? ? ?return executor; ? ?}? public static LinkedBlockingQueue<ZephyrJobWrapper> getWaitingJobQueue() { return waitingJobQueue; }? public static void setWaitingJobQueue(ZephyrJobWrapper job) { waitingJobQueue.add(job); }
}用来轮询的com.autorun.JobService类如下:package com.autorun;
public class JobService implements Runnable {public MasterGlobalResource masterGlobalResource;@Overridepublic void run() {while(true){System.out.println("size="+masterGlobalResource.getWaitingJobQueue().size());for(ZephyrJobWrapper job:masterGlobalResource.getWaitingJobQueue()){System.out.println(job.getJobId()+" ?"+job.getJobName());}try {Thread.sleep(1000);//一秒轮询一次} catch (InterruptedException e) {e.printStackTrace();}}
}
}
写一个controller用来向队列waitingJobQueue写入数据(主要代码如下):public MasterGlobalResource masterGlobalResource;//向队列写入10个对象for(int i=0;i<10;i++){ZephyrJobWrapper job=new ZephyrJobWrapper();job.setJobId(String.valueOf(i));job.setJobName(String.valueOf(i));masterGlobalResource.setWaitingJobQueue(job);try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}
启动服务后,会自动启动轮询队列waitingJobQueue的线程,此时执行 controller,打印结果如下;//启动服务由于还没向队列写数据得到的队列长度为0size=0size=0.....//调用了controller,向队列写入了数据,队列程度为10size=100 ?01 ?12 ?23 ?34 ?45 ?56 ?67 ?78 ?89 ?9......
注意:使用 main函数来向队列写入数据 不可行,轮询得到的结果任然是size=0....public static void main(String[] args) {ApplicationContext app=new ClassPathXmlApplicationContext("classpath*:spring/*.xml");System.out.println("begin");MasterGlobalResource res=(MasterGlobalResource)app.getBean("masterGlobalResource");for(int i=0;i<10;i++){ZephyrJobWrapper job=new ZephyrJobWrapper();job.setJobId(String.valueOf(i));job.setJobName(String.valueOf(i));res.setWaitingJobQueue(job);try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("end");
}