Solved

Want to use this code in my main function

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 80
ejb example issues 3 47
java mysql insert application 14 41
Java array sort 10 27
Configure Web Service (server application) I. Configure security for Web Services methods First, we need to protect Session bean which implements the service: 1. Open EJB deployment descriptor (ejb-jar.xml) in the EJB project that contains you…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

685 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