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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于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>

 

以上是关于关于javaweb 项目 ssm框架 启动tomcat服务器同时启动一个socket服务的主要内容,如果未能解决你的问题,请参考以下文章

基于javaweb基于ssm框架学生信息管理(选课)系统设计与实现(项目源码)

求一个SSH框架的JAVAWEB项目

javaweb基于ssm框架快递代拿管理系统设计与实现(项目源码)

javaweb超级简单网上购物商城系统源码SSM框架结构

javaweb基于ssm框架校园即时服务平台管理系统设计与实现(项目源码)

javaweb基于ssm框架学生信息管理(成绩)系统设计与实现(项目源码+报告设计说明)