6

I am passing the port via shell script while starting the spring boot application. Would like to know how to get the running port and system ip address in the application to print in log file.

script: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9890

Jessie
  • 963
  • 5
  • 16
  • 26
  • where are you paasing the running port? is it inside script? and lastly when you say would like to know, where you want to know? inside your appllication or outside? – pvpkiran Aug 01 '18 at 08:45
  • For the IP address part : https://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java – Arnaud Aug 01 '18 at 08:47

3 Answers3

8

You can autowire port number in any component class in following way

// Inject which port we were assigned
@Value("${local.server.port}")
int port;

Or with the annotation @LocalServerPort

@LocalServerPort
private int port;

And host address with following

String ip = InetAddress.getLocalHost().getHostAddress()
JimmyB
  • 12,101
  • 2
  • 28
  • 44
Emdadul Sawon
  • 5,730
  • 3
  • 45
  • 48
  • Notice that the OP is asking for the *JMX* port, which is not the `@LocalServerPort`. – JimmyB Aug 01 '18 at 12:40
  • LocalServerPort is a charm :) nothing can be easier than this. please remember this is available only from Spring Boot 2.0.0 onwards. https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/server/LocalServerPort.html – itsraghz Mar 05 '21 at 18:22
4

If you want to get it after application running try with this:

@Component
public class ApplicationLoader implements ApplicationRunner {

    @Autowired
    private Environment environment;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(environment.getProperty("java.rmi.server.hostname"));
        System.out.println(environment.getProperty("local.server.port"));
        System.out.println(InetAddress.getLocalHost().getHostAddress());
    }
}

You can get port many ways:

@Value("${local.server.port}")
private int serverPort;

or

@LocalServerPort
private int serverPort;
GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39
3

When you pass any parameters from the script, you can get this at runtime:

private String jmxRemote = System.getProperty("com.sun.management.jmxremote"); 
private String jmxRemotePort = System.getProperty("com.sun.management.jmxremote.port");

Get properties either the JVM itself or any -D options you may have passed at the command line

Get ip:

// for example 127.0.0.1 is localhost ip
private String ip = InetAddress.getLoopbackAddress().getHostAddress();

Get port:

@Value("${local.server.port}")
private int serverPort;

or by:

@LocalServerPort
private int serverPort;

or by:

@Autowired
private Environment environment;

public void doWork(){
    String serverPort = environment.getProperty("local.server.port");
    // do something
}

Also you can get all properties from Environment - JVM / system / environment / all passed arguments

tsarenkotxt
  • 3,231
  • 4
  • 22
  • 38