使用REST Web服务停止套接字-JAVA
我又带着另一个问题回来了,我试着搜索一个问题/答案,但到目前为止没有一个有效,所以我决定发布另一个问题/答案。 我在Java/Tomcat中运行了一个小型REST JAXRS Web服务,我希望从JSP页面发送命令来启动和停止套接字客户端。到目前为止,我实现了启动插座,但我无法停止它,我做错了什么 编辑1: 我用一个JFrame接口测试了相同的套接字功能(使用相同的类PLC),该接口有两个按钮,一个是连接,另一个是断开,代表其余功能,它可以工作 我的休息功能使用REST Web服务停止套接字-JAVA,java,multithreading,sockets,client,Java,Multithreading,Sockets,Client,我又带着另一个问题回来了,我试着搜索一个问题/答案,但到目前为止没有一个有效,所以我决定发布另一个问题/答案。 我在Java/Tomcat中运行了一个小型REST JAXRS Web服务,我希望从JSP页面发送命令来启动和停止套接字客户端。到目前为止,我实现了启动插座,但我无法停止它,我做错了什么 编辑1: 我用一个JFrame接口测试了相同的套接字功能(使用相同的类PLC),该接口有两个按钮,一个是连接,另一个是断开,代表其余功能,它可以工作 我的休息功能 NewTen_PLC m_plc =
NewTen_PLC m_plc = new NewTen_PLC(0x01, "Central Park", "10.80.4.10", 5001);
Thread m_thread;
@PUT
@Path("/connect")
public void Connect(InputStream is)
{
System.out.println("CONNECT COMMAND RECEIVED");
try
{
System.out.println( "Starting Executor" );
//Using thread manager
//ExecutorService threadExecutor = Executors.newCachedThreadPool();
//Start Thread
//threadExecutor.execute(m_plc);
//Shutdown working threads when task is complete
//threadExecutor.shutdown();
//Using Thread
m_thread = new Thread(m_plc);
m_thread.start();
System.out.println( "Tasks started, main ends.\n" );
}
catch(Exception ex)
{
}
}
@PUT
@Path("/disconnect")
public void Disconnect(InputStream is)
{
System.out.println("DISCONNECT COMMAND RECEIVED");
try
{
//m_plc.closeConnection();
m_plc.m_bConnected = false;
}
catch(Exception ex)
{
System.out.println("Error while trying to close socket");
ex.printStackTrace();
}
}
这是包含套接字函数和线程运行方法的类PLC
//--------------------------------------------------------------------------------------------------------
// Run (Thread)
//--------------------------------------------------------------------------------------------------------
@Override
public void run()
{
try
{
//DEBUG
System.out.printf("\n Thread Started and Running in %s \n", getPLCName());
//Connect to remote site
connectToRemoteServer();
//Set streams
getStreams();
//Process connection
processConnection();
}
catch(EOFException eofException)
{
System.out.println( "\nClient terminated connection" );
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
finally
{
closeConnection(); // close connection
}
}
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
// Connect to Server/Remote Site
//--------------------------------------------------------------------------------------------------------
private void connectToRemoteServer() throws IOException
{
//DEBUG
System.out.println("Attempting connection to site");
//Get IP Address
InetAddress ipAddress = InetAddress.getByName(this.getIPAddress());
//Create and Open Socket to server
m_clientSocket = new Socket(ipAddress, this.getPort(), null, this.getLocalPort());
//Set connected
if(m_clientSocket != null)
m_bConnected = true;
//Set read timeout to infinite
m_clientSocket.setSoTimeout(0);
//DEBUG
System.out.println("Connected to site");
}
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
// Get in and out Streams
//--------------------------------------------------------------------------------------------------------
private void getStreams() throws IOException
{
//DEBUG
System.out.println("Setting Streams");
//Input data stream
m_inStream = new DataInputStream(m_clientSocket.getInputStream());
//Output data stream
m_outStream = new DataOutputStream(m_clientSocket.getOutputStream());
}
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
// Process connection
//--------------------------------------------------------------------------------------------------------
private void processConnection() throws IOException
{
NewTen_Buffer inBuffer = new NewTen_Buffer(8192);
try // read message and display it
{
while(m_inStream.read() != -1 && m_bConnected == true)
{
//Read
inBuffer.m_iSize = m_inStream.read(inBuffer.m_bBuffer);
//DEBUG
System.out.printf("Data read has %d bytes \n", inBuffer.m_iSize);
//Decode
newData(inBuffer);
}
//DEBUG
System.out.println("OUT OF READING LOOP");
}
finally
{
}
}
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
// Close Connection
//--------------------------------------------------------------------------------------------------------
public void closeConnection()
{
System.out.println( "\nClosing connection" );
try
{
System.out.println( "\nIn Try closing" );
m_bConnected = false;
m_clientSocket.close(); // close socket
//System.out.println( "\nClosing Data input streams" );
//m_inStream.close(); // close output stream
//m_outStream.close(); // close input stream
//System.out.println( "\nShutdown ip op" );
//m_clientSocket.shutdownInput();
//m_clientSocket.shutdownOutput();
//System.out.println( "\nClosing socket" );
//m_clientSocket.close(); // close socket
System.out.println( "\nEnd of Try closing" );
}
catch ( IOException ioException )
{
ioException.printStackTrace();
} // end catch
}
//--------------------------------------------------------------------------------------------------------
有什么建议可以阻止它吗
干杯,
Leo我还没有完全阅读所有服务器代码,但我猜您的服务器线程会阻塞等待来自流的连接/输入,因此您无法停止它
如果必须使用REST,可以强制终止线程以停止它(我不能保证其含义)。或者另一种方法是实现一个套接字协议来停止服务器——与使用REST相反Hey mate,感谢您的快速回复,我使用JFrame接口测试了相同的功能,它可以工作,所以我可以停止它(上面的编辑1)。我真的很想让它和REST一起工作,但我会用SOCKET试试。干杯强制杀戮也无济于事。它会抛出一个异常,但不会停止。我想我会遇到连接问题,而不是断开连接:)你是怎么强迫杀人的?您是否尝试在
m_线程上调用interrupt()
?是的,我在m_线程上强制执行了一个中断(),并导致此异常严重:在路径为[/NewWebScada]的上下文中,Servlet[NewScada]的Servlet.service()抛出异常java.lang.NullPointerException,位于com.networkten.webscada.NewTen_PLC.closeConnection(NewTen_PLC.java:287)也许强制杀戮真的有效——只是你没有正确处理NPE。尝试在线程代码上实现一个catch-all来处理它。毕竟,如果你选择不优雅地停止,你必须处理它:)