Setting the port without using jvm argument -Dratpack.port=5555

classic Classic list List threaded Threaded
19 messages Options
Reply | Threaded
Open this post in threaded view
|

Setting the port without using jvm argument -Dratpack.port=5555

Skab
Hi all,

I'm looking for a "dynamic" way to set ratpack's port without using the jvm argument (see subject).

I've a small application written in groovy using the handers dsl. I'm packaging it using the shadowjar plugin for gradle which works just fine. My goal is that people can just doubleclick the jarfile to start the application. Unfortunately the standard port 5050 is used then.

My last option would be a small script to run the jar but I would rather avoid that. I'd like to use a custom configuration file for my application.

Btw Ratpack is really great, thanks for your work!
rus
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

rus

You can set the port to use in ratpack.properties using ratpack.port, does that help?

On 1 Nov 2014 17:04, "Skab [via Ratpack Forum]" <[hidden email]> wrote:
Hi all,

I'm looking for a "dynamic" way to set ratpack's port without using the jvm argument (see subject).

I've a small application written in groovy using the handers dsl. I'm packaging it using the shadowjar plugin for gradle which works just fine. My goal is that people can just doubleclick the jarfile to start the application. Unfortunately the standard port 5050 is used then.

My last option would be a small script to run the jar but I would rather avoid that. I'd like to use a custom configuration file for my application.

Btw Ratpack is really great, thanks for your work!


If you reply to this email, your message will be added to the discussion below:
http://forum.ratpack.io/Setting-the-port-without-using-jvm-argument-Dratpack-port-5555-tp759.html
To start a new topic under Ratpack Forum, email [hidden email]
To unsubscribe from Ratpack Forum, click here.
NAML
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

danthegoodman
In reply to this post by Skab
You could create a extend GroovyRatpackMain and add a default port value. From my understanding, this will allow you to still override the default port with the 'ratpack.port' system property.

Once you have a custom main class, you will need to change the 'mainClassName' in build.gradle.

--- MyMain.groovy ---

package com.foo.myproject

class MyMain extends GroovyRatpackMain{
    public static void main(String[] args){
        new MyMain().startOrExit();
    }
    @Override
    protected void addImpliedDefaults(Properties properties) {
        super.addImpliedDefaults(properties)
        properties[LaunchConfigs.Property.PORT] = readPortFromConfigFile();
    }
    String readPortFromConfigFile(){
        // ...
    }
}


--- build.gradle ---

project.mainClassName = "com.foo.myproject.MyMain"
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Luke Daley
Administrator
As Rus pointed out, the simplest way is to have a `ratpack.properties` file inside the JAR containing ‘port=1234’. 
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Skab
Works fine, thank you!
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Skab
Hi again,

even though this is not the right place for the following question maybe you can also help me with this.

When I use launch4j to build a windows exefile from the jar file I get the following error when I try to run the exefile.

[main] ERROR ratpack.launch.RatpackMain -
ratpack.launch.LaunchException: Could not read config file 'ratpack.properties'

I think it can be configured somehow (classpath option?) in the launch4j xml configuration file but I didn't find out how yet. When I unzip the exefile the groovy.properties is included just like in the jarfile (which works fine).

Do you have any ideas?
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Luke Daley
Administrator
There should be a nested exception with the real cause of the problem. 
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Skab
Hi Luke,

the full message is as follows:

Z:\git\ratpack\RatpackService\build>RatpackService-0.1-all.exe
[main] ERROR ratpack.launch.RatpackMain -
ratpack.launch.LaunchException: Could not read config file 'ratpack.properties'
        at ratpack.launch.internal.LaunchConfigsInternal.createFromFile(LaunchConfigsInternal.java:95)
        at ratpack.launch.internal.LaunchConfigsInternal.createFromProperties(LaunchConfigsInternal.java:86)
        at ratpack.launch.internal.LaunchConfigsInternal.createFromGlobalProperties(LaunchConfigsInternal.java:57)
        at ratpack.launch.internal.LaunchConfigsInternal.createFromGlobalProperties(LaunchConfigsInternal.java:51)
        at ratpack.launch.LaunchConfigs.createFromGlobalProperties(LaunchConfigs.java:66)
        at ratpack.launch.RatpackMain.server(RatpackMain.java:56)
        at ratpack.launch.RatpackMain.start(RatpackMain.java:68)
        at ratpack.groovy.launch.GroovyRatpackMain.start(GroovyRatpackMain.java:38)
        at ratpack.launch.RatpackMain.startOrExit(RatpackMain.java:76)
        at ratpack.groovy.launch.GroovyRatpackMain.main(GroovyRatpackMain.java:32)
Caused by: java.util.zip.ZipException: invalid block type
        at java.util.zip.InflaterInputStream.read(Unknown Source)
        at java.io.FilterInputStream.read(Unknown Source)
        at java.util.Properties$LineReader.readLine(Unknown Source)
        at java.util.Properties.load0(Unknown Source)
        at java.util.Properties.load(Unknown Source)
        at ratpack.launch.internal.LaunchConfigsInternal.createFromFile(LaunchConfigsInternal.java:93)
        ... 9 more

I'm currently using Ratpack version 0.9.8. I'm quite new to JVM/Java/Groovy and gradle so I really appreciate your help :-)
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Luke Daley
Administrator
Ratpack doesn’t know how to read the .exe file. How was it created? What is it?
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Skab
It's an exefile I created from the runnable jar file using launch4j (http://launch4j.sourceforge.net/).
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Luke Daley
Administrator
That’s not going to work then. I’m not even sure if we can make it work as that exe looks completely opaque to me.

You might be better off using your own main class and using the LaunchConfigs class to bootstrap the app. Should be pretty easy.


Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Skab
Thanks for your hint(s).  I'll try that.
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Craig Schlegelmilch
In reply to this post by Luke Daley
My apologies for bumping this thread but I'm having a dog of a time trying to get this to work.  I've put 'port-6060' in src/ratpack/ratpack.properties and I've been unable to get my groovy-based (i.e. DSL) application to pick up on the change.  I even dug around the source hoping to find some clues and can only find references to setting ports from either the defined default of 5050 or picking the values off the system environment  variables/properties.  

I must be missing something obvious.  Hopefully someone can point me in the right direction.

Thanks in advance,

Craig
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Luke Daley
Administrator
Hi Craig,

Can you share a bit more about what version you are using and how you are starting the app? This has all changed a lot in recent versions with the improved support for externalised configuration.
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Craig Schlegelmilch
Hi Luke,

We're using  0.9.16 and am starting via the gradle run task (it also seems to behave the same way when using an IDEA run config on ratpack.groovy.GroovyRatpackMain).  If there is a better approach, please let me know.

Thanks,

Craig
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

davidmc24
If I'm understanding what you're trying to do right, you can change the default port for a Ratpack app as shown in this example.

http://ratpack.io/manual/current/api/ratpack/config/ConfigDataSpec.html#props-java.util.Map-
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Craig Schlegelmilch
Ah yes.  That helps. That was enough for me to figure out the Groovy DSL too.  Turns out, this works as well:

ratpack {
        serverConfig {
                port(6060)
        }

. . .

Thanks again for your help.

Craig
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

paulhsu
In reply to this post by Skab
it works on 1.4


you have a file on your classpath, ex. ratpack.properties

server.port=5555

and

in your code, say

RatpackServer
                .start(s-> s
                .serverConfig(b->{
                    b.props("ratpack.properties");
                })
Reply | Threaded
Open this post in threaded view
|

Re: Setting the port without using jvm argument -Dratpack.port=5555

Denys
May be it is too old thread, but ... IMHO it is even easier to use environment variable, e.g.:

RATPACK_SERVER__PORT=6060

and you can spawn instances from the same jar on different ports just passing env var to the process (or container ... )

https://ratpack.io/manual/current/config.html#environment_variables