关于javaweb 项目 ssm框架 启动tomcat服务器同时启动一个socket服务

1.创建监听类

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

/** 
* 将socket service随tomcat启动 
* @author huajian 
*/ 
public class SocketServiceLoader implements ServletContextListener{ 
//socket server 线程 
private SocketThread socketThread; 

@Override 
public void contextDestroyed(ServletContextEvent arg0) { 
if(null!=socketThread && !socketThread.isInterrupted()) 

socketThread.closeSocketServer(); 
socketThread.interrupt(); 



@Override 
public void contextInitialized(ServletContextEvent arg0) { 
// TODO Auto-generated method stub 
if(null==socketThread) 

//新建线程类 
socketThread=new SocketThread(null); 
//启动线程 
socketThread.start(); 


}

 

2.创建生产socket线程类:

import java.io.IOException;
import java.net.ServerSocket; 
import java.net.Socket; 

import javax.servlet.ServletContext; 

/** 
* socket 线程类 
* @author huajian 
*/ 
public class SocketThread extends Thread{ 
private ServerSocket serverSocket = null; 

public SocketThread(ServerSocket serverScoket){ 
try { 
if(null == serverSocket){ 
this.serverSocket = new ServerSocket(4700); 
System.out.println("socket start"); 

} catch (Exception e) { 
System.out.println("SocketThread创建socket服务出错"); 
e.printStackTrace(); 




public void run(){ 
while(!this.isInterrupted()){ 
try { 
Socket socket = serverSocket.accept(); 

if(null != socket && !socket.isClosed()){ 
//处理接受的数据 
new SocketOperate(socket).start(); 

socket.setSoTimeout(30000); 

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




public void closeSocketServer(){ 
try { 
if(null!=serverSocket && !serverSocket.isClosed()) 

serverSocket.close(); 

} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 


}

3.创建处理客户端发送报文的类:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 

import javax.servlet.ServletContext; 

/** 
* 多线程处理socket接收的数据 
* @author huajian

*/ 
public class SocketOperate extends Thread{ 
private Socket socket; 

public SocketOperate(Socket socket) { 
this.socket=socket; 

@SuppressWarnings("unused") 
public void run() 

try{ 

InputStream in= socket.getInputStream(); 

PrintWriter out=new PrintWriter(socket.getOutputStream()); 

//BufferedReader wt = new BufferedReader(new InputStreamReader(System.in)); 

while(true){ 
//读取客户端发送的信息 
String strXML = ""; 
byte[] temp = new byte[1024]; 
int length = 0; 
while((length = in.read(temp)) != -1){ 
strXML += new String(temp,0,length); 

if("end".equals(strXML)){ 
System.out.println("准备关闭socket"); 
break; 

if("".equals(strXML)) 
continue; 

System.out.println("客户端发来:"+strXML.toString());

// MethodHandler mh = new MethodHandler(ReadXML.readXML(strXML.toString())); 
// String resultXML = mh.getResultXML(); 
// System.out.println("返回:"+resultXML.toString()); 

// if(!"".equals(resultXML)){ 
// out.print(resultXML); 
out.flush(); 
out.close(); 
// } 



socket.close(); 
System.out.println("socket stop....."); 

}catch(IOException ex){ 

}finally{ 



}

4.配置web.xml中配置监听

<listener> 
  <listener-class>com.novolion.wateriot.server.SocketServiceLoader</listener-class> 
</listener>

转载于:https://www.cnblogs.com/Im-Victor/p/11068280.html

参与评论 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值