1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
<chapter id="RESTEasy_Embedded_Container">
<title>Embedded Containers</title>
<para>RESTEasy has a few different plugins for different embedabble HTTP and/or Servlet containers if use RESTEasy in
a test environment, or within an environment where you do not want a Servlet engine dependency.</para>
<section>
<title>Undertow</title>
<para>
Undertow is a new Servlet Container that is used by WildFly (JBoss Community Server). You can embed
Undertow as you wish. Here's a a test that shows it in action.
<programlisting><![CDATA[
import io.undertow.servlet.api.DeploymentInfo;
import org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer;
import org.jboss.resteasy.test.TestPortProvider;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public class UndertowTest
{
private static UndertowJaxrsServer server;
@Path("/test")
public static class Resource
{
@GET
@Produces("text/plain")
public String get()
{
return "hello world";
}
}
@ApplicationPath("/base")
public static class MyApp extends Application
{
@Override
public Set<Class<?>> getClasses()
{
HashSet<Class<?>> classes = new HashSet<Class<?>>();
classes.add(Resource.class);
return classes;
}
}
@BeforeClass
public static void init() throws Exception
{
server = new UndertowJaxrsServer().start();
}
@AfterClass
public static void stop() throws Exception
{
server.stop();
}
@Test
public void testApplicationPath() throws Exception
{
server.deploy(MyApp.class);
Client client = ClientBuilder.newClient();
String val = client.target(TestPortProvider.generateURL("/base/test"))
.request().get(String.class);
Assert.assertEquals("hello world", val);
client.close();
}
@Test
public void testApplicationContext() throws Exception
{
server.deploy(MyApp.class, "/root");
Client client = ClientBuilder.newClient();
String val = client.target(TestPortProvider.generateURL("/root/test"))
.request().get(String.class);
Assert.assertEquals("hello world", val);
client.close();
}
@Test
public void testDeploymentInfo() throws Exception
{
DeploymentInfo di = server.undertowDeployment(MyApp.class);
di.setContextPath("/di");
di.setDeploymentName("DI");
server.deploy(di);
Client client = ClientBuilder.newClient();
String val = client.target(TestPortProvider.generateURL("/di/base/test"))
.request().get(String.class);
Assert.assertEquals("hello world", val);
client.close();
}
}
]]></programlisting>
</para>
</section>
<section>
<title>Sun JDK HTTP Server</title>
<para>
The Sun JDK comes with a simple HTTP server implementation (com.sun.net.httpserver.HttpServer) which you
can run RESTEasy on top of.
</para>
<programlisting> <![CDATA[
HttpServer httpServer = HttpServer.create(new InetSocketAddress(port), 10);
contextBuilder = new HttpContextBuilder();
contextBuilder.getDeployment().getActualResourceClasses().add(SimpleResource.class);
HttpContext context = contextBuilder.bind(httpServer);
context.getAttributes().put("some.config.info", "42");
httpServer.start();
contextBuilder.cleanup();
httpServer.stop(0);
]]></programlisting>
<para>
Create your HttpServer the way you want then use the org.jboss.resteasy.plugins.server.sun.http.HttpContextBuilder to initialize Resteasy
and bind it to an HttpContext. The HttpContext attributes are available by injecting in a org.jboss.resteasy.spi.ResteasyConfiguration
interface using @Context within your provider and resource classes.
</para>
<para>Maven project you must include is:</para>
<programlisting> <![CDATA[
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jdk-http</artifactId>
<version>3.6.2.Final</version>
</dependency>
]]></programlisting>
</section>
<section>
<title>TJWS Embeddable Servlet Container</title>
<para>
RESTEasy integrates with the TJWS Embeddable Servlet container. It comes with this distribution, or you can reference the Maven artifact. You must also provide
a servlet API dependency as well.
</para>
<programlisting> <![CDATA[
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>tjws</artifactId>
<version>3.6.2.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
]]></programlisting>
<para>
From the distribution, move the jars in resteasy-jaxrs.war/WEB-INF/lib into your classpath. You must both programmatically register your JAX-RS beans using the embedded server's Registry. Here's an example:
</para>
<para>
<programlisting>
@Path("/")
public class MyResource {
@GET
public String get() { return "hello world"; }
public static void main(String[] args) throws Exception
{
TJWSEmbeddedJaxrsServer tjws = new TJWSEmbeddedJaxrsServer();
tjws.setPort(8080);
tjws.start();
tjws.getRegistry().addPerRequestResource(RestEasy485Resource.class);
}
}
</programlisting>
</para>
<para>
The server can either host non-encrypted or SSL based resources, but not both. See the Javadoc for TJWSEmbeddedJaxrsServer as well as its superclass TJWSServletServer. The TJWS website is also a good place for information.
</para>
<para>
</para>
<para>
If you want to use Spring, see the SpringBeanProcessor. Here's a pseudo-code example
</para>
<para>
<programlisting>
public static void main(String[] args) throws Exception
{
final TJWSEmbeddedJaxrsServer tjws = new TJWSEmbeddedJaxrsServer();
tjws.setPort(8081);
tjws.start();
org.jboss.resteasy.plugins.server.servlet.SpringBeanProcessor processor = new SpringBeanProcessor(tjws.getDeployment().getRegistry(), tjws.getDeployment().getFactory();
ConfigurableBeanFactory factory = new XmlBeanFactory(...);
factory.addBeanPostProcessor(processor);
}
</programlisting>
</para>
<para><emphasis role="bold">NOTE:</emphasis> TJWS is now deprecated. Consider using the more modern Undertow.</para>
</section>
<section>
<title>Netty</title>
<para>
RESTEasy has integration with the popular Netty project as well..
</para>
<programlisting> <![CDATA[
public static void start(ResteasyDeployment deployment) throws Exception
{
netty = new NettyJaxrsServer();
netty.setDeployment(deployment);
netty.setPort(TestPortProvider.getPort());
netty.setRootResourcePath("");
netty.setSecurityDomain(null);
netty.start();
}
]]></programlisting>
<para>Maven project you must include is:</para>
<programlisting> <![CDATA[
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-netty</artifactId>
<version>3.6.2.Final</version>
</dependency>
]]></programlisting>
</section>
<section>
<title>Vert.x</title>
<para>
RESTEasy has integration with the popular Vert.x project as well..
</para>
<programlisting> <![CDATA[
public static void start(VertxResteasyDeployment deployment) throws Exception
{
VertxJaxrsServer server = new VertxJaxrsServer();
server.setDeployment(deployment);
server.setPort(TestPortProvider.getPort());
server.setRootResourcePath("");
server.setSecurityDomain(null);
server.start();
}
]]></programlisting>
<para>Maven project you must include is:</para>
<programlisting> <![CDATA[
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-vertx</artifactId>
<version>3.6.2.Final</version>
</dependency>
]]></programlisting>
<para>The server will bootstrap its own Vert.x instance and Http server.</para>
<para>When a resource is called, it is done with the Vert.x Event Loop thread, keep in mind to
not block this thread and respect the Vert.x programming model, see the related Vert.x <ulink url="http://vertx.io/docs/vertx-core/java/#_don_t_block_me">manual page</ulink>.</para>
<para>Vert.x extends the RESTEasy registry to provide a new binding scope that creates resources per Event Loop:</para>
<programlisting> <![CDATA[
VertxResteasyDeployment deployment = new VertxResteasyDeployment();
// Create an instance of resource per Event Loop
deployment.getRegistry().addPerInstanceResource(Resource.class);
]]></programlisting>
<para>The per instance binding scope caches the same resource instance for each event loop providing the same
concurrency model than a verticle deployed multiple times.</para>
<para>Vert.x can also embed a RESTEasy deployment, making easy to use Jax-RS annotated controller in Vert.x applications: </para>
<programlisting> <![CDATA[
Vertx vertx = Vertx.vertx();
HttpServer server = vertx.createHttpServer();
// Set an handler calling Resteasy
server.requestHandler(new VertxRequestHandler(vertx, deployment));
// Start the server
server.listen(8080, "localhost");
]]></programlisting>
<para>Vert.x objects can be injected in annotated resources:</para>
<programlisting> <![CDATA[
@GET
@Path("/somepath")
@Produces("text/plain")
public String context(
@Context io.vertx.core.Context context,
@Context io.vertx.core.Vertx vertx,
@Context io.vertx.core.http.HttpServerRequest req,
@Context io.vertx.core.http.HttpServerResponse resp) {
return "the-response";
}
]]></programlisting>
</section>
</chapter>
|