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() {
            
      }

}
pderingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Well, for one thing, your stop method is empty. How could it do anything?
0
pderingAuthor Commented:
So i need to close the sockets in stop()?
0
CEHJCommented:
Yes
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

pderingAuthor Commented:
How would I do that?
0
CEHJCommented:
You would make a proper application where the Socket is an instance variable and then call close on it
0
pderingAuthor Commented:
I can't do that from stop() since it is static.
0
CEHJCommented:
It shouldn't be
0
pderingAuthor Commented:
Do you have an example?
0
CEHJCommented:
public void stop() {
   	try { ss.close() ; } catch(IOException e) { /* ignore */ }	
}

Open in new window

0
pderingAuthor Commented:
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

0
CEHJCommented:
You would make a proper application where the Socket is an instance variable
0
pderingAuthor Commented:
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

0
CEHJCommented:
Nope. main should only be used to start the app. No variables should be declared static
0
pderingAuthor Commented:
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

0
CEHJCommented:
No. All the code is still in main
0
pderingAuthor Commented:
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

0
CEHJCommented:
Better
0
pderingAuthor Commented:
So, to my original question, when I convert the code to a service why won't it stop when I manually try?
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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!
0
pderingAuthor Commented:
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?
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
0
CEHJCommented:
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...
And the answer to my comment about not making static methods should have been "The spec demands static methods"

The last code should work, but afaics there's no need for any variable to be public and only the badly named 'L3' needs to be a static variable.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Some points for mccarl were due too ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.