Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Want to use this code in my main function

Posted on 2011-09-25
4
Medium Priority
?
282 Views
Last Modified: 2012-05-12
Hi Experts,  

I have this code snippet file 1 and file 2 (see below) which start's and stop's jetty server. The code works fine when executed stand alone. I would want start and stop the jetty server from file 3 and remove the main functions from file1 and file 2. Please help

+++++++
//File 1
+++++++

package com.manu.example.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;

public class Start {

    private static Server server;

      public static void main(String[] args) throws Exception {
            
            server = new Server();
            SocketConnector connector = new SocketConnector();
            connector.setPort(8080);
            server.setConnectors(new Connector[] { connector });
            WebAppContext context = new WebAppContext();
            context.setServer(server);
            context.setContextPath("/");
            context.setWar("src/main/java/com/manu/test/example/web");
            server.addHandler(context);
        Thread monitor = new MonitorThread();
        monitor.start();
        server.start();
        server.join();
       
      }

    private static class MonitorThread extends Thread {

        private ServerSocket socket;

        public MonitorThread() {
            setDaemon(true);
            setName("StopMonitor");
            try {
                socket = new ServerSocket(8079, 1, InetAddress.getByName("127.0.0.1"));
            } catch(Exception e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void run() {
            System.out.println("*** running jetty 'stop' thread");
            Socket accept;
            try {
                accept = socket.accept();
                BufferedReader reader = new BufferedReader(new InputStreamReader(accept.getInputStream()));
                reader.readLine();
                System.out.println("*** stopping jetty embedded server");
                server.stop();
                accept.close();
                socket.close();
            } catch(Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

}


+++++++
//File 2
+++++++

package com.manu.example.test;

import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;



public class Stop {

    public static void main(String[] args) throws Exception {
       
          
          Socket s = new Socket(InetAddress.getByName("127.0.0.1"), 8079);
        OutputStream out = s.getOutputStream();
        System.out.println("*** sending jetty stop request");
        out.write(("\r\n").getBytes());
        out.flush();
        s.close();
       
    }

}

0
Comment
Question by:manugeek
  • 2
4 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36597266
If your soecond file does not contain anynthing else then it should wioerk this way:


Add contents of main method in file 2 to the main method
in file 1 and when starting on command line

add argument like that:

java Start start  - it sghould start

java Start stop - it should stop


  public static void main(String[] args) throws Exception {

   if(args[0].equals("start")){
            
            server = new Server();
            SocketConnector connector = new SocketConnector();
            connector.setPort(8080);
            server.setConnectors(new Connector[] { connector });
            WebAppContext context = new WebAppContext();
            context.setServer(server);
            context.setContextPath("/");
            context.setWar("src/main/java/com/manu/test/example/web");
            server.addHandler(context);
        Thread monitor = new MonitorThread();
        monitor.start();
        server.start();
        server.join();
    }
  else 
 {

   Socket s = new Socket(InetAddress.getByName("127.0.0.1"), 8079);
        OutputStream out = s.getOutputStream();
        System.out.println("*** sending jetty stop request");
        out.write(("\r\n").getBytes());
        out.flush();
        s.close();
      }     


        
      }

Open in new window

0
 
LVL 20

Expert Comment

by:a_b
ID: 36597267
Move the code from the main methods to proper non static methods as part of the classes.
Create objects of classes in file1 and file2 and invoke those methods.
0
 

Author Comment

by:manugeek
ID: 36597281
Thanks for the quick answer @for_yan:
where should the MonitorThread() be placed ?
0
 

Author Closing Comment

by:manugeek
ID: 36597304
Thanks i figured it out
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Verbose logging is used to diagnose garbage collector problems. By default, -verbose:gc output is written to either native_stderr.log or native_stdout.log.   It is also possible to redirect the logs to a user-specified file. This article will de…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question