Inspired by the now removed {@code org.springframework.util.SocketUtils} and is only used in a testing capacity.
31 | *
32 | * @author Chris Bono
33 | * @deprecated will soon be removed or consolidated - do not use further
34 | */
35 | @Deprecated
36 | public final class SocketUtils {
37 |
38 | private static final Random random = new Random(System.nanoTime());
39 |
40 | /**
41 | * Find an available TCP port randomly selected from the range {@code 1024-65535}.
42 | *
43 | * @return an available TCP port number
44 | * @throws IllegalStateException if no available port could be found
45 | */
46 | public static int findAvailableTcpPort() {
47 | int minPort = 1024;
48 | int maxPort = 65535;
49 | int portRange = maxPort - minPort;
50 | int candidatePort;
51 | int searchCounter = 0;
52 | do {
53 | if (searchCounter > portRange) {
54 | throw new IllegalStateException(String.format(
55 | "Could not find an available TCP port after %d attempts", searchCounter));
56 | }
57 | candidatePort = minPort + random.nextInt(portRange + 1);
58 | searchCounter++;
59 | }
60 | while (!isPortAvailable(candidatePort));
61 |
62 | return candidatePort;
63 | }
64 |
65 | private static boolean isPortAvailable(int port) {
66 | try {
67 | ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(
68 | port, 1, InetAddress.getByName("localhost"));
69 | serverSocket.close();
70 | return true;
71 | } catch (Exception ex) {
72 | return false;
73 | }
74 | }
75 | }
--------------------------------------------------------------------------------
/mds/USAGE.md:
--------------------------------------------------------------------------------
1 | # How to use RESTEasy Spring Boot Starter
2 |
3 | #### Adding POM dependency
4 |
5 | Add one of the following Maven dependencies to your Spring Boot application pom file.
6 |
7 | **Servlet**
8 |
9 | ``` xml
10 |
`resteasy.scan.providers`
`resteasy.scan.resources`
`resteasy.providers`
`resteasy.use.builtin.providers`
`resteasy.resources`
`resteasy.jndi.resources`|All JAX-RS resources and providers are always supposed to be Spring beans, and they are automatically discovered|
100 |
101 |
102 | #### Reactor Netty Configuration
103 |
104 | The following parameters can be set in `application.properties` to configure Reactor Netty:
105 |
106 | | Parameter | Description |
107 | |---|---|
108 | |`server.port` | Port to use for running reactor netty server. Default is `8080`. Set to `0` to let Reactor Netty choose any available|
109 |
110 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 | ## Starting the application
6 |
7 | You can start the application as you for any other regular Spring Boot application. For example:
8 |
9 | 1. From the command line, under the sample application project, run `mvn spring-boot:run`
10 | 1. From your favorite IDE, run class `com.test.Application`
11 |
12 | ## Testing it
13 |
14 | Send a `POST` request message, containing the payload below, to [http://localhost:8080/sample-app/echo](http://localhost:8080/sample-app/echo).
15 |
16 | ```
17 | is there anybody out there?
18 | ```
19 |
20 | You can use the following command to send the request:
21 |
22 | ```bash
23 | $ curl -k --location --request POST 'https://localhost:8443/sample-app/echo' \
24 | --header 'Content-Type: text/plain' \
25 | --data-raw 'is there anybody out there?'
26 | ```
27 |
28 | You should receive a response message with a payload similar to this as result:
29 |
30 | ``` json
31 | {
32 | "timestamp": "1484775122357",
33 | "echoText": "is there anybody out there?"
34 | }
35 | ```
36 |
37 | The request message payload can be anything as plain text.
38 | The response message is supposed to echo that, plus a timestamp of the moment the echo response was created on the server side. The response message will be in JSON format.
39 |
--------------------------------------------------------------------------------
/reactor-netty/resteasy-reactor-netty-spring-boot-sample-app/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
85 | * Similar to:
86 | * https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java
87 | *
88 | * @param server - The Reactor Netty server to start.
89 | * @param shutdownLatch - The countdown latch to use in order to wait for the server to shutdown.
90 | * @throws InterruptedException - If thread is interrupted while waiting for the server to start.
91 | */
92 | @PostConstruct
93 | private void startServer() throws InterruptedException {
94 | final CountDownLatch startupLatch = new CountDownLatch(1);
95 | final Thread awaitThread = new Thread("server") {
96 | @Override
97 | public void run() {
98 | server.start();
99 | logger.info("Reactor Netty server started on port: {}", server.getPort());
100 | startupLatch.countDown();
101 | try {
102 | shutdownLatch.await();
103 | } catch (final InterruptedException ie) {
104 | logger.error("Exception caught while waiting for the Reactor Netty server to stop", ie);
105 | Thread.currentThread().interrupt();
106 | }
107 | }
108 | };
109 | awaitThread.setContextClassLoader(MethodHandles.lookup().lookupClass().getClassLoader());
110 | awaitThread.setDaemon(false);
111 | awaitThread.start();
112 | startupLatch.await();
113 | }
114 |
115 | @PreDestroy
116 | private void stopServer() {
117 | logger.info("Stopping the JAX-RS+Reactor-Netty server.");
118 | server.stop();
119 | shutdownLatch.countDown();
120 | }
121 |
122 | public ReactorNettyJaxrsServer getServer() {
123 | return server;
124 | }
125 |
126 | }
127 |
128 |
129 | private void configureServerAndDeployment(
130 | final ReactorNettyJaxrsServer server,
131 | final Set
4 |
5 | ## Starting the application
6 |
7 | You can start the application as you for any other regular Spring Boot application. For example:
8 |
9 | 1. From the command line, under the sample application project, run `mvn spring-boot:run`
10 | 1. From your favorite IDE, run class `com.test.Application`
11 |
12 | ## Testing it
13 |
14 | Send a `POST` request message, containing the payload below, to [http://localhost:8080/sample-app/echo](http://localhost:8080/sample-app/echo).
15 |
16 | ```
17 | is there anybody out there?
18 | ```
19 |
20 | You can use the following command to send the request:
21 |
22 | ```bash
23 | $ curl --location --request POST 'http://localhost:8080/sample-app/echo' \
24 | --header 'Content-Type: text/plain' \
25 | --data-raw 'is there anybody out there?'
26 | ```
27 |
28 | You should receive a response message with a payload similar to this as result:
29 |
30 | ``` json
31 | {
32 | "timestamp": "1484775122357",
33 | "echoText": "is there anybody out there?"
34 | }
35 | ```
36 |
37 | The request message payload can be anything as plain text.
38 | The response message is supposed to echo that, plus a timestamp of the moment the echo response was created on the server side. The response message will be in JSON format.
39 |
--------------------------------------------------------------------------------
/servlet/resteasy-servlet-spring-boot-sample-app/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |