Link to home
Start Free TrialLog in
Avatar of pdering
pdering

asked on

java service

When I package this as a service it will start but I cannot manually stop it.  Why?

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class L3 {

      public static ArrayList<Socket> sl = new ArrayList<Socket>();
      
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            ServerSocket ss;
            Socket s;
            while (true) {
                  
                        //Log("Started");
                        
                        try {
                              ss = new ServerSocket (4444);
                              s = ss.accept();
                              ss.close();
                              s.close();
                        } catch (IOException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                        }
                  
      
            }
      }
      public static void stop() {
            
      }

}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Well, for one thing, your stop method is empty. How could it do anything?
Avatar of pdering
pdering

ASKER

So i need to close the sockets in stop()?
Yes
Avatar of pdering

ASKER

How would I do that?
You would make a proper application where the Socket is an instance variable and then call close on it
Avatar of pdering

ASKER

I can't do that from stop() since it is static.
It shouldn't be
Avatar of pdering

ASKER

Do you have an example?
public void stop() {
   	try { ss.close() ; } catch(IOException e) { /* ignore */ }	
}

Open in new window

Avatar of pdering

ASKER

I tried that but ss is not reachable...

package temp1.java;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class L3 {

      public static ArrayList<Socket> sl = new ArrayList<Socket>();
      
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            ServerSocket ss;
            Socket s;
            while (true) {
                  
                        //Log("Started");
                        
                        try {
                              ss = new ServerSocket (4444);
                              s = ss.accept();
                              ss.close();
                              s.close();
                        } catch (IOException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                        }
                  
      
            }
      }
      public void stop() {
    	   	try { ss.close() ; } catch(IOException e) { /* ignore */ }	
    	}
    	                                            
} 

Open in new window

You would make a proper application where the Socket is an instance variable
Avatar of pdering

ASKER

Does this look correct?  Or close?

package temp1.java;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class L3 {

      public static ArrayList<Socket> sl = new ArrayList<Socket>();
      public ServerSocket ss;
      public static void main(String[] args) {
    	  
            // TODO Auto-generated method stub
            L3 x = new L3();
            
            Socket s;
            while (true) {
                  
                        //Log("Started");
            	
                        
                        try {
                              x.ss = new ServerSocket (4444);
                              s = x.ss.accept();
                              x.ss.close();
                              s.close();
                        } catch (IOException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                        }
                  
      
            }
      }
      public void stop() {
    	   	try { ss.close() ; } catch(IOException e) { /* ignore */ }	
    	}
    	                                            
} 

Open in new window

Nope. main should only be used to start the app. No variables should be declared static
Avatar of pdering

ASKER

How about this?  (no static vars)


package temp1.java;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class L3 {

      public ServerSocket ss;
      public static void main(String[] args) {
    	  
            // TODO Auto-generated method stub
            L3 x = new L3();
            
            Socket s;
            while (true) {
                  
                        //Log("Started");
            	
                        
                        try {
                              x.ss = new ServerSocket (4444);
                              s = x.ss.accept();
                              x.ss.close();
                              s.close();
                        } catch (IOException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                        }
                  
      
            }
      }
      public void stop() {
    	   	try { ss.close() ; } catch(IOException e) { /* ignore */ }	
    	}
    	                                            
} 
                                            

Open in new window

No. All the code is still in main
Avatar of pdering

ASKER

How about this?

package temp1.java;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class L3 {

      public ServerSocket ss;
      public Socket s;
      public static void main(String[] args) {
    	  
    	  L3 xxx = new L3();
          xxx.sck();  
          
      }
      public void stop() {
    	   	try { ss.close() ; } catch(IOException e) { /* ignore */ }	
    	}
      public void sck() {
    	  while (true) {
              
              //Log("Started");
  	
              
              try {
                    ss = new ServerSocket (4444);
                    s = ss.accept();
                    ss.close();
                    s.close();
              } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
              }
        

  }
      }
    	                                            
} 
                                            
                                            

Open in new window

Better
Avatar of pdering

ASKER

So, to my original question, when I convert the code to a service why won't it stop when I manually try?
when I convert the code to a service
What are you using to "convert" this to a service? Are you talking about a Windows service? You will need to provide more details!
Avatar of pdering

ASKER

I tried the advancedinstaller.com which converts JARs to a Windows service.  when i dont include code for socket in the Java code it appears to work.  But when I do include socket  code the windows service will not let me manually stop it.  
I thought that might be a symptom that there was  a coding problem.
I  did try with the most recent edit of the code (above).
Any  thoughts on this?
Ok, so it appears that you were on the right idea with your original code, using a "static" method called stop() looking at this information...

http://www.advancedinstaller.com/user-guide/tutorial-java-service.html#preparing

You just need to get a few small things right. CEHJ's suggestion of having most of the code in instance method is still valid though, so try something like this (based off your last code posted)...
package temp1.java;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class L3 {

      public static L3 xxx;            // You should probably name this something better ;)

      public ServerSocket ss;
      public Socket s;
      public static void main(String[] args) {
    	  
          xxx = new L3();
          xxx.sck();  
          
      }
      public static void stop() {
    	   	try { xxx.ss.close() ; } catch(IOException e) { /* ignore */ }	
    	}
      public void sck() {
    	  while (true) {
              
              //Log("Started");
  	
              
              try {
                    ss = new ServerSocket (4444);
                    s = ss.accept();
                    ss.close();
                    s.close();
              } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
              }
        

  }
      }
    	                                            
}

Open in new window

This should be enough to get you going... HOWEVER it is still not the most elegant design. Ideally you would separate this code into two classes, one's that is responsible for handling the service startup/shutdown, ie. it would have the two static methods, main and stop, in it and a static reference to a second class, that is responsible for actually doing whatever you want to do, ie. open a serversocket, etc. Then you would provide a clean public interface between the two, probably just two (non-static) methods, start and stop, that do the work required.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Some points for mccarl were due too ...