Solved

Want to use this code in my main function

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
stringclean challenge 26 57
strCopies  challenge 17 73
Printing to a specific printer tray (HP 806dn printer) 3 64
recursion example 16 70
This exercise is about for the following scenario: Dmgr and One node with 2 application server. Each application server contains it owns application. Application server name as follows server1 contains app1 server2 contains app1 Prereq…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now