multithreading - Stopping a socket using a REST WebService - JAVA -
i again issue, tried searching question/answer, none of them worked far, decided post one. have small rest jaxrs webservice in java/tomcat running, , jsp page hope send commands start , stop socket client. far achieved starting socket, not able stop it, doing wrong?
edit 1: tested same socket functions (using same class plc) jframe interface 2 buttons, 1 connect , other disconnect representing rest functions, , works
my rest functions
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 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 close socket"); ex.printstacktrace(); } }
and class plc contains socket functions , thread run method.
//-------------------------------------------------------------------------------------------------------- // run (thread) //-------------------------------------------------------------------------------------------------------- @override public void run() { try { //debug system.out.printf("\n thread started , running in %s \n", getplcname()); //connect remote site connecttoremoteserver(); //set streams getstreams(); //process connection processconnection(); } catch(eofexception eofexception) { system.out.println( "\nclient terminated connection" ); } catch(ioexception ioexception) { ioexception.printstacktrace(); } { closeconnection(); // close connection } } //-------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------- // connect server/remote site //-------------------------------------------------------------------------------------------------------- private void connecttoremoteserver() throws ioexception { //debug system.out.println("attempting connection site"); //get ip address inetaddress ipaddress = inetaddress.getbyname(this.getipaddress()); //create , open socket server m_clientsocket = new socket(ipaddress, this.getport(), null, this.getlocalport()); //set connected if(m_clientsocket != null) m_bconnected = true; //set read timeout infinite m_clientsocket.setsotimeout(0); //debug system.out.println("connected site"); } //-------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------- // in , 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 , display { 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"); } { } } //-------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------- // 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 } //--------------------------------------------------------------------------------------------------------
any suggestions how stop it?
cheers, leo
i haven't read server code, i'm guessing server thread blocks waiting connection / input stream, hence can't stop it.
if have use rest, can force kill thread stop (i can't guarantee implication). or other approach implement socket protocol stop server -- opposed of using rest
Comments
Post a Comment