About the Javaweb Project SSM framework start a Tomcat server and start a socket service at the same time

Source: Internet
Author: User
Tags thread class tomcat server

1. Create a Listener class

Import Javax.servlet.ServletContext;
Import javax.servlet.ServletContextEvent;
Import Javax.servlet.ServletContextListener;

/**
* Boot the socket service with Tomcat
* @author Huajian
*/
public class Socketserviceloader implements servletcontextlistener{
Socket Server Thread
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)
{
New Thread class
Socketthread=new socketthread (NULL);
Start thread
Socketthread.start ();
}
}
}

2. Create a production socket thread class:

Import java.io.IOException;
Import Java.net.ServerSocket;
Import Java.net.Socket;

Import Javax.servlet.ServletContext;

/**
* Socket Thread Class
* @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 Creating socket Service error");
E.printstacktrace ();
}

}

public void Run () {
while (!this.isinterrupted ()) {
try {
Socket socket = serversocket.accept ();

if (null! = Socket &&!socket.isclosed ()) {
Processing the accepted data
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. Create a class that handles client-sent messages:

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;

/**
* Multithreading processing the data received by the 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) {
Read the information sent by the client
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 ("Ready to close socket");
Break
}
if ("". Equals (Strxml))
Continue

SYSTEM.OUT.PRINTLN ("Client sent to:" +strxml.tostring ());

Methodhandler MH = new Methodhandler (Readxml.readxml (strxml.tostring ()));
String resultxml = Mh.getresultxml ();
System.out.println ("Return:" +resultxml.tostring ());

if (! "". Equals (Resultxml)) {
Out.print (Resultxml);
Out.flush ();
Out.close ();
// }


}
Socket.close ();
SYSTEM.OUT.PRINTLN ("Socket stop ...");

}catch (IOException ex) {

}finally{

}
}
}

4. Configuring monitoring in Web. config

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

About the Javaweb Project SSM framework start a Tomcat server and start a socket service at the same time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.