码迷,mamicode.com
首页 > 编程语言 > 详细

web socket多线程实时监听

时间:2016-07-06 13:04:40      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:

配置文件:

技术分享
web.xml文件:
<!-- socket 监听 -->
   <context-param>
    <param-name>socketPort</param-name>
    <param-value>8888</param-value>
  </context-param>
  <listener>
    <description>Socket</description>
    <listener-class>com.changbei.modules.intf.listener.ServerSocketListener</listener-class>
  </listener>
View Code

代码:

技术分享
package com.demo.modules.intf.listener;

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

import com.demo.intf.util.SocketThread;

public class ServerSocketListener implements ServletContextListener
{
    private SocketThread socketThread;

    public void contextDestroyed(ServletContextEvent e)
    {
        if (socketThread != null && socketThread.isInterrupted())
        {
            socketThread.closeServerSocket();
            socketThread.interrupt();
        }
    }

    public void contextInitialized(ServletContextEvent e)
    {
        ServletContext servletContext = e.getServletContext();
        System.out.println("Server contextInitialized over");
        if (socketThread == null)
        {
            socketThread = new SocketThread(null, servletContext);
            socketThread.start(); 
        }
    }
}


----------------------------


package com.demo.intf.util;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.servlet.ServletContext;

public class SocketThread extends Thread
{
    Integer count = 0;
    private ServletContext servletContext;
    private ServerSocket serverSocket;

    public SocketThread(ServerSocket serverSocket, ServletContext servletContext)
    {
        this.servletContext = servletContext;
        // 从web.xml中context-param节点获取socket端口
        String port = this.servletContext.getInitParameter("socketPort");
        if (serverSocket == null)
        {
            try
            {
                this.serverSocket = new ServerSocket(Integer.parseInt(port));
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    public void run()
    {
        try
        {
            
            Socket client = null;  
            //通过调用Executors类的静态方法,创建一个ExecutorService实例  
            //ExecutorService接口是Executor接口的子接口  
            Executor service = Executors.newCachedThreadPool();  
            Integer count = 0;
            while (!this.isInterrupted())
            {
                client = serverSocket.accept();
                count++;
                System.out.println("Server SocketThread start:"+count);
                if (client != null)
                {
                    //调用execute()方法时,如果必要,会创建一个新的线程来处理任务,但它首先会尝试使用已有的线程,  
                    //如果一个线程空闲60秒以上,则将其移除线程池;  
                    //另外,任务是在Executor的内部排队,而不是在网络中排队  
                    service.execute(new ServerThread(client)); 
                }
            }
        }
        catch (Exception ex)
        {
            System.out.println("SocketThread err:"+ex.getMessage());
        }
    }
    
    public void closeServerSocket()
    {
        try
        {
            if (serverSocket != null && !serverSocket.isClosed())
            {
                serverSocket.close();
                MyServer.destroyedTimer();
            }

        }
        catch (Exception ex)
        {
            System.out.println("SocketThread err:"+ex.getMessage());
        }
    }
}
View Code

 

web socket多线程实时监听

标签:

原文地址:http://www.cnblogs.com/ice-line/p/5646390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!