Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

more examples

  • Loading branch information...
commit a436856b8e16d2bd1c335c96467e596e0579be1a 1 parent 70a1a49
Bill Burke patriot1burke authored
9 jaxrs/examples/jaxrs-2.0/server-async-http/README.txt
View
@@ -0,0 +1,9 @@
+Simple JAX-RS 2.0 example of server-side async HTTP. This is a chat application.
+
+$ mvn jetty:run
+
+Open your browser to http://localhost:8080/index.html
+Open another tab or browser to same and chat.
+
+FYI, some messages are lost because we are queuing response streams, and not the actual posted messages, but you get
+the idea I hope.
108 jaxrs/examples/jaxrs-2.0/server-async-http/pom.xml
View
@@ -0,0 +1,108 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <groupId>org.jboss.resteasy</groupId>
+ <artifactId>resteasy-jaxrs-all</artifactId>
+ <version>3.0-beta-1</version>
+ <relativePath>../../../pom.xml</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.jboss.resteasy.test</groupId>
+ <artifactId>server-async-http</artifactId>
+ <packaging>war</packaging>
+ <name>chat</name>
+ <description/>
+ <url>http://maven.apache.org</url>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.resteasy</groupId>
+ <artifactId>resteasy-jaxrs</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.resteasy</groupId>
+ <artifactId>async-http-servlet-3.0</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <finalName>chat</finalName>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-deploy-plugin</artifactId>
+ <configuration>
+ <skip>true</skip>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.mortbay.jetty</groupId>
+ <artifactId>jetty-maven-plugin</artifactId>
+ <version>8.1.4.v20120524</version>
+ <configuration>
+ <webApp>
+ <contextPath>/</contextPath>
+ </webApp>
+ <scanIntervalSeconds>10</scanIntervalSeconds>
+ <stopKey>foo</stopKey>
+ <stopPort>9999</stopPort>
+ </configuration>
+ <executions>
+ <execution>
+ <id>start-jetty</id>
+ <phase>pre-integration-test</phase>
+ <goals>
+ <goal>run</goal>
+ </goals>
+ <configuration>
+ <scanIntervalSeconds>0</scanIntervalSeconds>
+ <daemon>true</daemon>
+ </configuration>
+ </execution>
+ <execution>
+ <id>stop-jetty</id>
+ <phase>post-integration-test</phase>
+ <goals>
+ <goal>stop</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <skip>true</skip>
+ </configuration>
+ <executions>
+ <execution>
+ <id>surefire-it</id>
+ <phase>integration-test</phase>
+ <goals>
+ <goal>test</goal>
+ </goals>
+ <configuration>
+ <skip>false</skip>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
38 ...s/examples/jaxrs-2.0/server-async-http/src/main/java/org/jboss/example/jaxrs2/async/ChatApplication.java
View
@@ -0,0 +1,38 @@
+package org.jboss.example.jaxrs2.async;
+
+import javax.ws.rs.container.AsyncResponse;
+import javax.ws.rs.core.Application;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
+ * @version $Revision: 1 $
+ */
+public class ChatApplication extends Application
+{
+ private Set<Object> singletons = new HashSet<Object>();
+ private Set<Class<?>> empty = new HashSet<Class<?>>();
+
+ public ChatApplication()
+ {
+ List<AsyncResponse> listeners = new ArrayList<AsyncResponse>();
+ singletons.add(new ChatListener(listeners));
+ singletons.add(new ChatSpeaker(listeners));
+ }
+
+ @Override
+ public Set<Class<?>> getClasses()
+ {
+ return empty;
+ }
+
+ @Override
+ public Set<Object> getSingletons()
+ {
+ return singletons;
+ }
+
+}
39 jaxrs/examples/jaxrs-2.0/server-async-http/src/main/java/org/jboss/example/jaxrs2/async/ChatListener.java
View
@@ -0,0 +1,39 @@
+package org.jboss.example.jaxrs2.async;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.container.AsyncResponse;
+import javax.ws.rs.container.Suspended;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
+ * @version $Revision: 1 $
+ */
+@Path("/listener")
+public class ChatListener {
+
+ public ChatListener(List<AsyncResponse> listeners)
+ {
+ this.listeners = listeners;
+ }
+
+ List<AsyncResponse> listeners;
+
+ @GET
+ @Path("ping")
+ @Produces("text/plain")
+ public String ping()
+ {
+ return "PONG";
+ }
+
+ @GET
+ public void listen(@Suspended AsyncResponse res) {
+ System.out.println("******* LISTENING *************");
+ res.setTimeout(100, TimeUnit.SECONDS);
+ synchronized(listeners) { listeners.add(res); }
+ }
+}
39 jaxrs/examples/jaxrs-2.0/server-async-http/src/main/java/org/jboss/example/jaxrs2/async/ChatSpeaker.java
View
@@ -0,0 +1,39 @@
+package org.jboss.example.jaxrs2.async;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.container.AsyncResponse;
+import javax.ws.rs.core.Response;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
+ * @version $Revision: 1 $
+ */
+@Path("/speaker")
+public class ChatSpeaker {
+
+ public ChatSpeaker(List<AsyncResponse> listeners)
+ {
+ this.listeners = listeners;
+ }
+
+ List<AsyncResponse> listeners;
+
+ @POST
+ @Consumes("text/plain")
+ public void speak(String speech) {
+ System.out.println("******* SPEAKING *************");
+ synchronized (listeners)
+ {
+ Iterator<AsyncResponse> it = listeners.iterator();
+ while (it.hasNext())
+ {
+ it.next().resume(Response.ok(speech, "text/plain").build());
+ it.remove();
+ }
+ }
+ }
+}
25 jaxrs/examples/jaxrs-2.0/server-async-http/src/main/webapp/WEB-INF/web.xml
View
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
+ "http://java.sun.com/dtd/web-app_2_3.dtd">
+<web-app>
+ <display-name>Archetype RestEasy Web Application</display-name>
+ <context-param>
+ <param-name>resteasy.servlet.mapping.prefix</param-name>
+ <param-value>/chat</param-value>
+ </context-param>
+
+ <servlet>
+ <servlet-name>Resteasy</servlet-name>
+ <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher</servlet-class>
+ <init-param>
+ <param-name>javax.ws.rs.Application</param-name>
+ <param-value>org.jboss.example.jaxrs2.async.ChatApplication</param-value>
+ </init-param>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>Resteasy</servlet-name>
+ <url-pattern>/chat/*</url-pattern>
+ </servlet-mapping>
+
+</web-app>
103 jaxrs/examples/jaxrs-2.0/server-async-http/src/main/webapp/index.html
View
@@ -0,0 +1,103 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+ "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <script type="text/javascript">
+ <!--
+
+ function createXHR()
+ {
+ var request = false;
+ try
+ {
+ request = new ActiveXObject('Msxml2.XMLHTTP');
+ }
+ catch (err2)
+ {
+ try
+ {
+ request = new ActiveXObject('Microsoft.XMLHTTP');
+ }
+ catch (err3)
+ {
+ try
+ {
+ request = new XMLHttpRequest();
+ }
+ catch (err1)
+ {
+ request = false;
+ }
+ }
+ }
+ return request;
+ }
+
+ var speaker;
+ var count = 0;
+ var reconnect = true;
+
+ function postMessage(user, message)
+ {
+ var xhr = createXHR();
+ xhr.open("POST", "http://localhost:8080/chat/speaker", false);
+ xhr.setRequestHeader("Content-Type", "text/plain");
+ xhr.send(user + ": " + message);
+ }
+
+ function receiveMessage()
+ {
+ var xhr = createXHR();
+ xhr.open("GET", "http://localhost:8080/chat/listener", true);
+ xhr.onreadystatechange = function()
+ {
+ if (xhr.readyState == 4)
+ {
+ var status = xhr.status;
+ if (status == 200)
+ {
+ document.getElementById("next").innerHTML = xhr.responseText + "\n" + document.getElementById("next").innerHTML;
+ }
+ setTimeout("receiveMessage()", 800);
+ }
+ }
+ xhr.send(null);
+ }
+
+ setTimeout("receiveMessage()", 800);
+
+
+ // -->
+ </script>
+ <title>Ajax Chat Demo</title></head>
+
+
+<body bgcolor="#FFFFFF">
+
+<p><font size="+3">Ajax Chat Demo</font></p>
+<hr>
+<FORM name="ajax" method="POST" action="">
+
+ <p>
+ Username: <input type="text" name="user" value="Billy" size="10"><br/>
+ <input type="text" name="message">
+ <INPUT type="BUTTON" value=" Click to send message "
+ ONCLICK="postMessage(this.form.user.value, this.form.message.value)">
+ </p>
+
+</FORM>
+<div id="connection"></div>
+<br>
+
+<div id="errors"></div>
+
+<p>
+
+<h2>Messages:</h2></p>
+<pre>
+<div id="next"></div>
+</pre>
+
+</body>
+</html>
62 ...ava/org/jboss/example/jaxrs2/simple/Customer.java → ...java/org/jboss/example/jaxrs2/async/Customer.java
View
@@ -1,31 +1,31 @@
-package org.jboss.example.jaxrs2.simple;
-
-import java.io.Serializable;
-
-/**
- * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
- * @version $Revision: 1 $
- */
-public class Customer implements Serializable
-{
- private String name;
-
- public Customer()
- {
- }
-
- public Customer(String name)
- {
- this.name = name;
- }
-
- public String getName()
- {
- return name;
- }
-
- public void setName(String name)
- {
- this.name = name;
- }
-}
+package org.jboss.example.jaxrs2.async;
+
+import java.io.Serializable;
+
+/**
+ * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
+ * @version $Revision: 1 $
+ */
+public class Customer implements Serializable
+{
+ private String name;
+
+ public Customer()
+ {
+ }
+
+ public Customer(String name)
+ {
+ this.name = name;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+}
66 ...ss/example/jaxrs2/simple/CustomerApplication.java → ...oss/example/jaxrs2/async/CustomerApplication.java
View
@@ -1,33 +1,33 @@
-package org.jboss.example.jaxrs2.simple;
-
-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 CustomerApplication extends Application
-{
- private Set<Object> singletons = new HashSet<Object>();
- private Set<Class<?>> empty = new HashSet<Class<?>>();
-
- public CustomerApplication()
- {
- singletons.add(new CustomerResource());
- }
-
- @Override
- public Set<Class<?>> getClasses()
- {
- return empty;
- }
-
- @Override
- public Set<Object> getSingletons()
- {
- return singletons;
- }
-
-}
+package org.jboss.example.jaxrs2.async;
+
+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 CustomerApplication extends Application
+{
+ private Set<Object> singletons = new HashSet<Object>();
+ private Set<Class<?>> empty = new HashSet<Class<?>>();
+
+ public CustomerApplication()
+ {
+ singletons.add(new CustomerResource());
+ }
+
+ @Override
+ public Set<Class<?>> getClasses()
+ {
+ return empty;
+ }
+
+ @Override
+ public Set<Object> getSingletons()
+ {
+ return singletons;
+ }
+
+}
62 ...jboss/example/jaxrs2/simple/CustomerResource.java → .../jboss/example/jaxrs2/async/CustomerResource.java
View
@@ -1,31 +1,31 @@
-package org.jboss.example.jaxrs2.simple;
-
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-
-/**
- * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
- * @version $Revision: 1 $
- */
-@Path("/customers")
-public class CustomerResource
-{
- @GET
- @Produces("application/json")
- public Customer getByName(@QueryParam("name") String name)
- {
- return new Customer(name);
- }
-
- @GET
- @Path("{id}")
- @Produces("application/json")
- public Customer getById(@PathParam("id") String id)
- {
- return new Customer("Bill");
- }
-
-}
+package org.jboss.example.jaxrs2.async;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+
+/**
+ * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
+ * @version $Revision: 1 $
+ */
+@Path("/customers")
+public class CustomerResource
+{
+ @GET
+ @Produces("application/json")
+ public Customer getByName(@QueryParam("name") String name)
+ {
+ return new Customer(name);
+ }
+
+ @GET
+ @Path("{id}")
+ @Produces("application/json")
+ public Customer getById(@PathParam("id") String id)
+ {
+ return new Customer("Bill");
+ }
+
+}
2  jaxrs/examples/jaxrs-2.0/simple-client/src/main/webapp/WEB-INF/web.xml
View
@@ -8,7 +8,7 @@
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
- <param-value>org.jboss.example.jaxrs2.simple.CustomerApplication</param-value>
+ <param-value>org.jboss.example.jaxrs2.async.CustomerApplication</param-value>
</init-param>
</servlet>
2  jaxrs/examples/jaxrs-2.0/simple-client/src/test/java/org/jboss/test/SimpleClientTest.java
View
@@ -1,7 +1,7 @@
package org.jboss.test;
import junit.framework.Assert;
-import org.jboss.example.jaxrs2.simple.Customer;
+import org.jboss.example.jaxrs2.async.Customer;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.junit.Test;
1  jaxrs/examples/pom.xml
View
@@ -38,5 +38,6 @@
<module>smime</module>
<module>digital-signatures</module>
<module>jaxrs-2.0/simple-client</module>
+ <module>jaxrs-2.0/server-async-http</module>
</modules>
</project>
Please sign in to comment.
Something went wrong with that request. Please try again.