public interface RatpackServer
The of(Action)
static method is used to create a server.
import ratpack.server.RatpackServer;
import ratpack.server.ServerConfig;
import ratpack.test.ApplicationUnderTest;
import static org.junit.Assert.*;
public class Example {
public static void main(String... args) throws Exception {
RatpackServer server = RatpackServer.of(s -> s
.serverConfig(ServerConfig.embedded()) // base server configuration (e.g. port) - optional
.registryOf(r -> r.add(String.class, "foo")) // registry of supporting objects - optional
.handlers(chain -> chain // request handlers - required
.get("a", ctx -> ctx.render(ctx.get(String.class) + " 1"))
.get("b", ctx -> ctx.render(ctx.get(String.class) + " 2"))
)
);
// this method starts the server, via server.start() and then calls server.stop()
ApplicationUnderTest.of(server).test(httpClient -> {
assertEquals("foo 1", httpClient.getText("a"));
assertEquals("foo 2", httpClient.getText("b"));
});
}
}
Server objects are thread safe in that every instance method is synchronized
.RatpackServerSpec
,
of(Action)
Modifier and Type | Method and Description |
---|---|
String |
getBindHost()
The actual host/ip that the application is bound to.
|
int |
getBindPort()
The actual port that the application is bound to.
|
String |
getScheme()
The URL scheme the server uses.
|
boolean |
isRunning()
Returns
true if the server is running. |
static RatpackServer |
of(Action<? super RatpackServerSpec> definition)
Creates a new, unstarted, Ratpack server from the given definition.
|
RatpackServer |
reload()
Reloads the server from its definition function.
|
void |
start()
Starts the server, returning as soon as the server is up and ready to receive requests.
|
static void |
start(Action<? super RatpackServerSpec> definition)
|
void |
stop()
Stops the server, returning as soon as the server has stopped receiving requests.
|
static RatpackServer of(Action<? super RatpackServerSpec> definition) throws Exception
The action argument effectively serves as the definition of the server.
It receives a mutable server builder style object, a RatpackServerSpec
.
The action is retained internally by the server, and invoked again if the reload()
method is called.
definition
- the server definitionException
- any thrown by creating the serverRatpackServerSpec
static void start(Action<? super RatpackServerSpec> definition) throws Exception
definition
- the server definitionException
- any thrown by of(Action)
or start()
String getScheme()
int getBindPort()
@Nullable String getBindHost()
boolean isRunning()
true
if the server is running.true
if the server is runningvoid start() throws Exception
This will create new threads that are not daemonized.
That is, a running Ratpack server will prevent the JVM from shutting down unless System.exit(int)
is called.
It is generally advisable to stop the server
instead of forcefully killing the JVM.
Calling this method while the server is running
has no effect.
Exception
- if the server could not be startedvoid stop() throws Exception
This method will terminate all threads started by the server.
Exception
- if the server could not be stopped cleanlyRatpackServer reload() throws Exception
The server definition will be rebuilt by executing the function used to create the server. Depending on the function's implementation, a different definition may result. This is effectively configuration reloading mechanism.
import ratpack.server.RatpackServer;
import ratpack.test.ApplicationUnderTest;
import static org.junit.Assert.*;
public class Example {
public static void main(String... args) throws Exception {
String[] holder = new String[]{"foo"};
RatpackServer server = RatpackServer.of(s -> s
.serverConfig(ServerConfig.embedded())
.registry(r -> r.add(String.class, holder[0]))
.handler(registry -> (ctx) -> ctx.render(ctx.get(String.class)))
);
ApplicationUnderTest.of(server).test(httpClient -> {
assertEquals("foo", httpClient.getText());
// change data read in definition function…
holder[0] = "bar";
// value unchanged…
assertEquals("foo", httpClient.getText());
// reload server…
server.reload();
// value has changed…
assertEquals("bar", httpClient.getText());
});
}
}
If the server is running, it will be stopped
and then started
.
If it is not running, the definition function will still be executed straight away and the new definition used next time the server is started.