The thread implementation of timer in Java

  1. js
    $(function(){
                 initTodoInfo(true);
      
                 })

//Get the scheduling command information and assign it to the prompt button findDcmdSize
        function initDcmdInfo(showWindow){
            var url = 'nnmis/view/dcmd/findDcmdSizeDCMD.tg';
             $.post(url, {}, function (data) {
              if(data != ""){
                     //From JSON data, parse quantity and new alarms
                     var response = eval("("+ data +")");
                     var totalCount = response[0].TOTALCNT;
                     //Update the displayed value of the quantity
                     $("#dcmd_info_label").html("Command["+totalCount+"]");
                  
                     
                   
                 }
             });    
        }

//Number of regular updates
        var getRealData = self.setInterval("initTodoInfo(true);", 60000);

2. action

@Autowired
    private DcmdData ddata;

/**
     * Returns the number of unprocessed items for the day
     */
    public void findDcmdSize() {

        String dataGridStr = "";
        // List<Map> dcmdlist = dcmdService.findDcmdListNumByCondtion(dcmd);
        List<Map> dcmdlist = ddata.getDcmdList();
        if (dcmdlist.size() == 0) {
            dcmdlist = dcmdService.findDcmdListNumByCondtion(dcmd);
        }
        dataGridStr = super.autoDMapLst2DgJson(dcmdlist);

        returnJson(dataGridStr, response);

    }


 

3. to do data cache class

package com.casco.csmis.dcmd.data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.casco.csmis.dcmd.service.IDcmdInfoService;
import com.casco.csmis.dcmd.vo.DcmdInfo;

/**
 * <b> To-Do Data Cache Class<b>
 * <p>
 * Created by 60847
 */
@Repository(value = "dcmdData")
@Service
public class DcmdData {
	@Autowired
	private IDcmdInfoService dcmdService;

	private static List<Map> dcmdList = new ArrayList<Map>();

	private DcmdInfo dcmd;

	public static List<Map> getDcmdList() {
		return dcmdList;
	}

	public static void setDcmdList(List<Map> dcmdList) {
		DcmdData.dcmdList = dcmdList;
	}

	public DcmdData() {

	}

	public DcmdData(IDcmdInfoService dcmdService) {
		this.dcmdService = dcmdService;
	}

	/**
	 * Initialization
	 */
	public void init() {
		try {
			dcmdList = dcmdService.findDcmdListNumByCondtion(dcmd);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

4. Call the thread to start the thread when the server starts

 

/**
 * @Title: MemoryListener.java
 * @Package com.casco.csmis.listener
 * @Description:
 * @author ligangying [email protected]
 * @date 2013-10-2 AM 9:56:33
 * @version V1.0
 */

package com.casco.csmis.permissions.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.casco.csm.casco.core.tools.util.ReadProperty;

import com.casco.csmis.dcmd.data.DcmdData;

/**
 * @ClassName: MemoryListener
 * @Description: Initialize global parameters and system dictionary
 * @author sunct [email protected]
 * @date 2013-10-6 下午3:03:16
 */
public class MemoryListener implements ServletContextListener {

	private DcmdData dcmdData;

	Log logger = LogFactory.getLog(MemoryListener.class);

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {

		logger.debug("tomcat Done");
	}

	@Override
    public void contextInitialized(ServletContextEvent arg0) {
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());

       
            this.setDcmdData((DcmdData) ctx.getBean("dcmdData"));
           

            logger.debug("tomcat start running");

            Thread dcmdThread = new Thread(new DcmdThread(this.getDcmdData()));
            dcmdThread.start();

        
     
    }

	public DcmdData getDcmdData() {
		return dcmdData;
	}

	public void setDcmdData(DcmdData dcmdData) {
		this.dcmdData = dcmdData;
	}

}

5. Thread call cache class

package com.casco.csmis.dcmd.util;

import com.casco.csmis.dcmd.data.DcmdData;

/*
 * Receiving server responsible for receiving data sent from C++
 */
public class DcmdThread implements Runnable {

	private DcmdData dcmdData;

	public DcmdData getDcmdData() {
		return dcmdData;
	}

	public void setDcmdData(DcmdData dcmdData) {
		this.dcmdData = dcmdData;
	}

	public DcmdThread(DcmdData dcmdData) {
		this.setDcmdData(dcmdData);
	}

	@Override
	public void run() {
		while (true) {
			// Reading the datas
			try {
				dcmdData.init();
				Thread.sleep(30000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

 

 

 

 

6 server class configuration in web.xml

<listener>
        <listener-class>com.casco.csmis.permissions.listener.MemoryListener</listener-class>
    </listener>

 

 

Read More: