There are applications for flash AS3 client - socket - Java EJB module on GlassFish server.

If the connection with one or several clients is active and I do undeploying this EJB module, the connections somehow fail badly and the server port remains open. And I don’t like this situation because the port remains busy until the restart of GlassFish or the entire virtual machine.

Well, the question is, of course, how to forcibly close all ports and sockets when the module is minimized?

I don't even know which piece of code to post, well, for example:

in EJB class:

@PreRemove @PreDestroy @PostRemove private void Destroy() throws InterruptedException{ Server.stopServer(); } 

In class Server

 public static void stopServer(){ IsActiveS = false; for(Server e : Connections){ e.drop(); } try { if(SeSocket!=null){ if(!SeSocket.isClosed()) SeSocket.close(); SeSocket = null; } } catch (IOException ex) { if(Settings.DEBUG)System.out.println("Error occured while closing socket: " + ex.getMessage()); } } 

if the variable IsActiveS = false, then all the cycles that are in the class are completed
Connections - list of active connections
IsActive is the same as IsActiveS for only one instance of the connection.

 public void drop(){ isActive = false; try { if(socket!=null){ socket.shutdownInput(); socket.shutdownOutput(); socket.setSoTimeout(1); socket.close(); } } catch (IOException ex) {} if(outStreamWriter!=null){ try{ outStreamWriter.close(); }catch (Exception ex){} outStreamWriter = null; } if(inStreamReader!=null){ try{ inStreamReader.close(); }catch (Exception ex){} inStreamReader = null; } if(socket!=null)socket = null; Connections.remove(this); } 
  • So is your "Destroy" being called or not? Try to secure his call and see And correct the code convention, and then the confusion is obtained. And deal with the methods of closure, and then some macaroni is obtained and obviously you are doing a lot of unnecessary actions. - cy6erGn0m
  • Regarding unnecessary actions - of course, then I’ll remove - this is from despair :) EJB annotations @PreRemove, @PreDestroy, @PostRemove (well, at least one of them :-D) oblige this method to be called before destroying the bean. And when the application is minimized, the bin is destroyed, isn't it? - artem
  • Just checked: the method is called once. Probably by @PreDestroy - artem
  • And what is your bin? Are you sure the pointers to the socket! = Null? What is your bin: stateless or stateful? - cy6erGn0m
  • An EJB class in which I call destroy () on my @ Singleton, and by the way, @Startup. Question about socket - where exactly? I seem to be checking everywhere. Each connection in the Connections list corresponds to a Server with its socket, there is also a single SeSocket - a static variable ServerSocket, created from the very beginning - artem

1 answer 1

In general, I found my mistake.
It is of course impossible to close ports from the outside without destroying GlassFish itself.
And as it turned out, of course, the trouble was that Connections threads were destroyed before they managed to free their sockets. Therefore, I made the main thread wait for the rest by the join () method.