Solved

Want to use this code in my main function

Posted on 2011-09-25
4
256 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 52
ejb entity bean example issue 2 17
printf performancy 11 71
how to debug htl and js pages 8 39
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

828 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