Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving port number in Play Framework 2 app

The question is: how do I retrieve the port number that play is listening on, no matter how it was defined (configuration, command line argument, or not at all).

This answer Retrieving port number in-application using Play Framework must be for Play v1 because that code doesn't compile.

In Play 2 Java there is:

Integer port = Play.application().configuration().getInt("http.port");

That doesn't return the actual port number, at least not in all cases. If Play is run as:

run 80 -Dhttp.address=example.com

Then it returns null.

If Play is run as

run -Dhttp.port 80 -Dhttp.address=example.com

Then Play starts on the default port (9000).

As biesior pointed out it works by mentioning the port twice:

play -Dhttp.port=80 "run 80"

Which of course is not optimal since one is what Play actually uses, and the other is what it reports.

But it would answer my question for dev mode. However, as the documentation says, Play should never be run using run in prod. So what's the equivalent for the start command, and is there no better, safer way? (I'm interested in the Java version but others might like to know about Scala too.)

like image 875
Gonfi den Tschal Avatar asked Feb 20 '13 01:02

Gonfi den Tschal


People also ask

How do I change the port number in play framework?

For changing the port, go to your project root folder and hit: activator "run 8008" in command prompt / terminal. and that's it.

What is the default port for Play application?

Overriding configuration with system properties The default is to listen on port 9000 at the 0.0. 0.0 address (all addresses).

How do I run Playframework?

You can run the created application and view the result in the default browser http://localhost:9000. To run a Play application: Create a new Run Configuration – From the main menu, select Run -> Edit Configurations. Click on the + to add a new configuration.

How do I run a debug in play framework?

menu (compile your project first). To debug, start your application with activator -jvm-debug 9999 run and in Eclipse right-click on the project and select Debug As, Debug Configurations. In the Debug Configurations dialog, right-click on Remote Java Application and select New. Change Port to 9999 and click Apply.


1 Answers

Running in dev mode you need to use doubled notation

play -Dhttp.port=80 "run 80"

Unfortunately you have to do the same for default port

play -Dhttp.port=9000 "run 9000"

So I just suggest to write shell script (or .bat in Windows) for easy starting with all required params.

like image 159
biesior Avatar answered Nov 12 '22 01:11

biesior