Solved

Want to use this code in my main function

Posted on 2011-09-25
4
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

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 article is about some of the basic and important steps to be used to improve the performance in web-sphere commerce application development. 1) Always leverage the Dyna-caching facility provided by the product 2) Remove the unwanted code …
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

624 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