-
-
Save mkarg/a38a68f6025f1ef6ddb4916022bd150d to your computer and use it in GitHub Desktop.
/* | |
* Copyright (c) 2018 Markus KARG. All rights reserved. | |
* | |
* This program and the accompanying materials are made available under the | |
* terms of the Eclipse Public License v. 2.0, which is available at | |
* http://www.eclipse.org/legal/epl-2.0. | |
* | |
* This Source Code may also be made available under the following Secondary | |
* Licenses when the conditions for such availability set forth in the | |
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | |
* version 2 with the GNU Classpath Exception, which is available at | |
* https://www.gnu.org/software/classpath/license.html. | |
* | |
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | |
*/ | |
package jaxrs.examples.bootstrap; | |
import java.io.IOException; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.ExecutionException; | |
import javax.ws.rs.JAXRS; | |
import javax.ws.rs.JAXRS.Instance; | |
/** | |
* Minimum Java SE Bootstrap Example | |
* | |
* @author Markus KARG ([email protected]) | |
*/ | |
public class MinimumSeBootstrapExample { | |
public void main(final String[] args) throws IOException, InterruptedException, ExecutionException { | |
final CompletableFuture<Instance> boot = JAXRS.start(new HelloWorld(), JAXRS.Configuration.builder().build()).toCompletableFuture(); | |
final Instance instance = boot.get(); | |
System.out.println("Press any key to shutdown."); | |
System.in.read(); | |
instance.stop().toCompletableFuture().join(); | |
} | |
} |
I would like to have more separate building blocks in the examples. What do you think ?
/**
* Starts the example.
*
* @param args Command line arguments
*/
public static void main(final String[] args) {
CompletionStage<JAXRS.Instance> startedInstance = JAXRS.start(new HelloWorld(), createConfiguration());
startedInstance.thenCompose((JAXRS.Instance instance) -> {
try {
System.out.println("Press any key to shutdown.");
System.in.read();
return instance.stop();
} catch (final IOException e) {
throw new CompletionException(e);
}
}).toCompletableFuture().join();
}
private static JAXRS.Configuration createConfiguration() {
return JAXRS.Configuration.builder()
.property(ServerProperties.HTTP_SERVER_PROVIDER,
(BiFunction<URI, ResourceConfig, HttpServer>) GrizzlyHttpServerFactory::createHttpServer)
.property(ServerProperties.HTTP_SERVER_ANNIHILATOR, (Consumer<HttpServer>) HttpServer::shutdownNow)
.property(JAXRS.Configuration.PORT, 8080)
.build();
}
On some operating system port 80 can only be used by root; use explicitly 8080 there.
"startedIntance" is semantically wrong; it is the process that currently is starting, not necessarily an instance that was already started.
There is no need to give explicitly "(JAXRS.Instance instance)"; Java already knows the type.
Separate blocks is fine for me, but not necessarily better than inline code, as this is just an example that shall outline the steps to perform.
"startedIntance" is semantically wrong;
Than how is the flow ?
-> stage completes (instance started) -> callback called with (JAXRS.Instance instance))
And the first completed stage joins the main thread ? Or what is the purpose of 'toCompletableFuture().join()' ?
There is no need to give explicitly "(JAXRS.Instance instance)"; Java already knows the type.
I know that, but I like examples to be more explicit. But I can remove it...
Separate blocks is fine for me, but not necessarily better than inline code, as this is just an example that shall outline the steps to perform.
Just judging from the discussion regarding 'startedIntance' it may at least help to understand the concepts more clearly ;-)
BTW: I think this one is wrong;
default Builder port(String port) {
return property(PORT, port);
}
Shouldn't that be an Integer? At least I get : java.lang.String cannot be cast to java.lang.Integer when using the builder.
@mkarg I'm OK with keep stop async, gives it a nice symmetry even if not as useful IMO.
@mkarg Newer versions of example are much nicer. I still find System.in.read() unrealistic; process should stop on a signal.
Ok this one is working now. BTW: The @ApplicationPath("helloworld") seems to be ignored.