Stewie Griffin
Stewie Griffin

Reputation: 5608

Listening a port constantly

I want to allow a java app to listen a port. I run the required command from a command prompt and it listens to the port and I see Server is listening on port 7070 as output and command line cannot pass low line. If I close the command prompt, it disconnects and then the port is closed.

Here is relevant code:

import java.io.InputStream;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.IOException;

import org.xml.sax.InputSource;

import de.l3s.boilerpipe.document.TextDocument;
import de.l3s.boilerpipe.extractors.ArticleExtractor;
import de.l3s.boilerpipe.sax.BoilerpipeSAXInput;


// Language detect librarys
import com.cybozu.labs.langdetect.*;

import net.arnx.jsonic.JSON;
import net.arnx.jsonic.JSONException;


import java.io.*;
import java.net.*;


import java.util.concurrent.Executors;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

class ExampleProgram {

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

EveryDetector evr = new EveryDetector();
InetSocketAddress addr = new InetSocketAddress("localhost",7070);
HttpServer server = HttpServer.create(addr, 0);

MyHandler hndl = new MyHandler();
hndl.setDetector(evr);

MyHandlerExtractContent hnd2 = new MyHandlerExtractContent();
hnd2.setDetector(evr);

MyHandlerDetectLanguage hnd3 = new MyHandlerDetectLanguage();
hnd3.setDetector(evr);

server.createContext("/",hndl);
server.createContext("/extractcontent",hnd2);
server.createContext("/detectlanguage",hnd3);
server.setExecutor(Executors.newCachedThreadPool());
server.start();
System.out.println("Server is listening on port 7070" );


 }
}

What should I do?

Upvotes: 1

Views: 554

Answers (1)

C. K. Young
C. K. Young

Reputation: 223023

It sounds like you should run your program as a daemon. Apache Commons Daemon has a jsvc tool that will do that for your program.

Note that your program has to be modified to either implement the Daemon interface or implement the init, start, stop, and destroy methods listed on the jsvc page.

Upvotes: 1

Related Questions