6

I have the need to check at runtime if the server is running in http://localhost Searching on the net I haven't found any good solution. Some advice?

Thank you

salgaf
  • 181
  • 2
  • 7
  • In a separate app it is easy to check localhost at port (80? 8080?). In the web application (re-)starts can be detected by context listeners. Maybe explain a bit more concrete. – Joop Eggen Sep 30 '15 at 12:08
  • What problem are you trying to solve? In general it's better to configure your build to produce an app based on target (e.g. local or remote deployment). I suspect there's not a completely reliable (Java-only) way of determining if Tomcat is running locally. localhost is only one way to access a local app server...there's also 127.0.0.1, the actual IP address, and the hostname. – Paul Sep 30 '15 at 12:38
  • My use case is to determine if my server is running in localhost that means is on my developing machine so the service will connect dynamically to the test JPA mysql database, instead when the service is running on the target will connect to the production database. – salgaf Sep 30 '15 at 12:51
  • When the application runs on the production, it will find itself running on localhost and will try to connect to test database, right? Your production server too has localhost reference of its own. Why don't you set some property on the server config, read it and connect to production? If the property goes null or empty, connect to test? – James Jithin Sep 30 '15 at 13:39
  • Nice suggestion... I'm pretty new to tomcat, there is you some example to how to use config file! – salgaf Sep 30 '15 at 13:52

4 Answers4

3

We have a running setup where the following configuration is being done:

  1. In catalina.properties, define active_profile=prod for production, active_profile=stage for stage or active_profile=dev for developer machine
  2. Have different connection properties files matching to the profile key - jdbc_prod.properties, jdbc_stage.properties, jdbc_dev.properties
  3. Refer to the connection properties as jdbc_${active_profile}.properties in your configuration.
James Jithin
  • 10,183
  • 5
  • 36
  • 51
  • So far I have used persistence.xml to configure JPA connection parameters statically. ...... ...... What I need is to configure the connection url parameter. How can I realize using the technique that you suggested. Sorry but I'm pretty new in tomcat. – salgaf Sep 30 '15 at 14:24
  • You may try to set `db_server=localhost` in your catalina.properties and refer as `jdbc:mysql://${db_server}:3306/test` to check if it works. – James Jithin Sep 30 '15 at 14:34
  • After try trow this exception: java.net.UnknownHostException: ${jpa_url} – salgaf Sep 30 '15 at 15:01
  • Could you try configuring using the `properties` file as mentioned in http://stackoverflow.com/questions/1834954/loading-properties-in-spring-context-xml-and-persistence-xml – James Jithin Sep 30 '15 at 15:14
2

You can try open a connection to localhost, if no exception was throw, then your server is running.

from doc: https://docs.oracle.com/javase/tutorial/networking/urls/connecting.html

try {
    URL myURL = new URL("http://localhost");
    // also you can put a port 
   //  URL myURL = new URL("http://localhost:8080");
    URLConnection myURLConnection = myURL.openConnection();
    myURLConnection.connect();
} 
catch (MalformedURLException e) { 
    // new URL() failed
    // ...
} 
catch (IOException e) {   
    // openConnection() failed
    // ...
}

Another way is to get all process name and check if one matches what you expect, the problem with this solution is that is not platform independent. In Java 9 it will be independent.

             try {                                      
                //linux 
                //Process process = Runtime.getRuntime().exec("ps -few");

                //windows
                Process process = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe /fo csv /nh");                                

                BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));                             
                boolean isRunning = input.lines().anyMatch(p -> p.contains("your process name"));

                input.close();

            } catch (Exception err) {
                err.printStackTrace();
            }
Johnny Willer
  • 3,717
  • 3
  • 27
  • 51
-3
  1. You could check by visiting http://localhost in the browser.
  2. If you are on linux, you could check if the process is running by

    ps aux | grep tomcat

  3. On Linux, you could also check status of tomcat service.

    sudo service tomcat7 status

  4. If you are on windows, check task manager. It should appear as a javaw.exe process.

Faheem Sohail
  • 806
  • 8
  • 21
-3

If you want to perform some logic on Tomcat initialization, you can write code within a ServletContextListener.

https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html

Simply write a class that implements ServletContextListener and implement the contextInitialized method.

Next, add the listener entry to web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <listener>
    <listener-class>
        example.ServletContextExample
    </listener-class>
  </listener>
</web-app>
Faheem Sohail
  • 806
  • 8
  • 21