zoukankan      html  css  js  c++  java
  • Struts+Spring+Hibernate项目的启动线程

    在Java Web项目中,经常要在项目开始运行时启动一个线程,每隔一定的时间就运行一定的代码,比如扫描数据库的变化等等。要实现这个功能,可以现在web.xml文件中定义一个Listener,然后在这个Listener中启动一个线程,在线程里面实现功能。

    1. 自定义Listener

    在Struts+Spring+Hibernate的Web项目中,web.xml里面一般都会有这样的代码:

        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

    这几句代码使得Web项目的容器(也就是Web服务器,比如Tomcat)在项目启动时实例化了一个org.springframework.web.context.ContextLoaderListener类。

    类似的,我们也可以在web.xml里面自己定义一个Listener,让Web服务器去实例化:

    <?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        </filter>
      
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
      
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
      
        <listener>
            <listener-class>com.XXX.listener.WSListener</listener-class>
        </listener>
      
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
      
    </web-app>

    在以上的web.xml文件中,我们就让Web服务器在启动时实例化我们自己定义的com.XXX.listener.WSListener类(一般自己定义的Listener类要写在org.springframework.web.context.ContextLoaderListener的后面),然后在该类中去启动线程:

    public class WSListener implements ServletContextListener{
        
        private WSThread wsThread;
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            // TODO Auto-generated method stub
            if (wsThread != null && wsThread.isRunning){
                wsThread.stopThread();
            }
        }
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            // TODO Auto-generated method stub
            if (wsThread == null){
                wsThread = new WSThread(event);
                wsThread.start();
            }
        }
    
    }

    Listener类是由Web服务器管理的,当Web服务器启动时,将Listener类实例化并调用其contextInitialized(ServletContextEvent event)方法,当Web服务器关闭时,调用其contextDestroyed(ServletContextEvent event)方法,因此我们可以分别在这两个方法里面实现线程的启动和结束。

    2. 在Spring容器以外获得其内部的Bean的实例的引用

    被启动的线程用于间隔一定的时间扫描一次数据库,找出新增加的数据。在一般的Struts+Spring+Hibernate的Web项目中,Spring容器中的Bean是由Spring容器管理的,而我们这里启动的线程并不在Spring容器中,那么怎样获得Spring容器中Bean的实例的引用进而访问数据库呢?可以使用Spring的WebApplicationContextUtils工具类,该工具类获得Spring容器的引用,再获得其内部的Bean的实例的引用。

    线程的代码:

    public class WSThread extends Thread{
        public volatile boolean isRunning = true;
        // 两次扫描之间休眠的时间
        public long s_time;
        private WebApplicationContext context;
        private PropService propService;
        private Prop prop;
        private Temp2Service temp2Service;
        private Temp2 temp2;
        
        private TempService tempService;
    
        
        ServletContextEvent event;
        public WSThread(ServletContextEvent e){
            this.event = e;
            this.context = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
            this.propService = (PropService) context.getBean("propService");
            this.temp2Service = (Temp2Service) context.getBean("temp2Service");
        }
        
        public void run(){
            while (isRunning){
                try {
                    this.prop = propService.findByName("scan_time");
                    this.s_time = Integer.parseInt(prop.getValue())*1000;
                    sleep(s_time);    
                    System.out.println("Run!!!!!!");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        public void stopThread(){
            isRunning = false;
        }
        
        public boolean isRunning(){
            return isRunning;
        }
        
    }

    在该线程的构造函数中,使用了从Listener传过来的ServletContextEvent变量,用该变量的getServletContext()方法,获取Web项目的servletContext,然后再以这个ServletContext作为参数,使用WebApplicationContextUtils的getRequiredWebApplicationContext()方法获取ApplicationContext对象,最后再通过ApplicationContext的getBean()方法获取到Bean的实例,实现对数据库的访问。

  • 相关阅读:
    Maven核心简析
    块/文件/对象存储对比性概述
    Java SE-基本数据类型对应包装类
    Maven+eclipse快速入门
    IaaS、PaaS、SaaS、CaaS、MaaS五者的区别
    Collections.shuffle()源码分析
    java集合继承关系图
    ArrayList和LinkedList的区别
    ArrayList的实现原理
    session以及分布式服务器session共享
  • 原文地址:https://www.cnblogs.com/mstk/p/6027104.html
Copyright © 2011-2022 走看看