TUSCANY-3517: Apply latest GSoC project zip from Florian
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@956851 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ec29a8d708
commit
650382e2bb
10 changed files with 224 additions and 104 deletions
sca-java-2.x/contrib/samples/async-servlet-sca
|
@ -41,6 +41,18 @@
|
|||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring</artifactId>
|
||||
<version>2.5.5</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.12</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
@ -52,43 +64,7 @@
|
|||
|
||||
<build>
|
||||
<finalName>async-servlet</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.mortbay.jetty</groupId>
|
||||
<artifactId>maven-jetty-plugin</artifactId>
|
||||
<version>6.1.18</version>
|
||||
<configuration>
|
||||
<contextPath>helloworld-servlet</contextPath>
|
||||
<stopKey>foo</stopKey>
|
||||
<stopPort>9999</stopPort>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>start-jetty</id>
|
||||
<phase>process-test-classes</phase>
|
||||
<goals>
|
||||
<goal>run</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<scanIntervalSeconds>0</scanIntervalSeconds>
|
||||
<daemon>true</daemon>
|
||||
<connectors>
|
||||
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
|
||||
<port>8085</port>
|
||||
</connector>
|
||||
</connectors>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>stop-jetty</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>stop</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
||||
|
|
|
@ -18,12 +18,9 @@
|
|||
*/
|
||||
package sample;
|
||||
|
||||
import java.util.Observer;
|
||||
|
||||
public interface StockService {
|
||||
|
||||
String getSymbol();
|
||||
Double getValue();
|
||||
void register(Observer observer);
|
||||
|
||||
}
|
||||
|
|
|
@ -18,46 +18,29 @@
|
|||
*/
|
||||
package sample;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
import java.util.Random;
|
||||
|
||||
public class StockServiceImpl extends Observable implements StockService,
|
||||
Runnable {
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class StockServiceImpl implements StockService {
|
||||
|
||||
public static final int MAX_VALUE = 1000;
|
||||
public static final int MAX_SECONDS = 10;
|
||||
public static final int MILLIS_PER_SECOND = 1000;
|
||||
|
||||
private Random random = new Random(new Date().getTime());
|
||||
private Logger logger = Logger.getLogger(StockServiceImpl.class);
|
||||
|
||||
@Override
|
||||
public String getSymbol() {
|
||||
logger.debug("Getting symbol...");
|
||||
return "ASF";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getValue() {
|
||||
return Math.abs(random.nextDouble() * random.nextInt(MAX_VALUE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
setChanged();
|
||||
notifyObservers();
|
||||
Thread.sleep((random.nextInt(MAX_SECONDS - 1) + 1) * MILLIS_PER_SECOND);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(Observer observer) {
|
||||
this.addObserver(observer);
|
||||
logger.debug("Getting value...");
|
||||
Double value = Math.abs(random.nextDouble() * random.nextInt(MAX_VALUE));
|
||||
return Double.valueOf(new DecimalFormat("#.##").format(value));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,42 +20,70 @@ package sample;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.oasisopen.sca.annotation.Reference;
|
||||
|
||||
@WebServlet(asyncSupported = true, urlPatterns = "stock/")
|
||||
public class StockServlet implements Observer {
|
||||
|
||||
@Reference
|
||||
private StockService service;
|
||||
@WebServlet(asyncSupported = true, urlPatterns = "/stock")
|
||||
public class StockServlet extends HttpServlet {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final long MILLIS_PER_SECOND = 1000L;
|
||||
|
||||
// @Reference
|
||||
private StockService service = new StockServiceImpl();
|
||||
private AsyncContext asyncContext;
|
||||
private Timer timer;
|
||||
private long updatePeriod = 1L; // default 1 sec
|
||||
private Logger logger = Logger.getLogger(StockServlet.class);
|
||||
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
asyncContext = req.startAsync(req, resp);
|
||||
service.register(this);
|
||||
throws ServletException {
|
||||
logger.debug("Received request.");
|
||||
logger.debug("Starting async context...");
|
||||
asyncContext = req.startAsync();
|
||||
logger.debug("Service reference: " + service);
|
||||
logger.debug("Setting timer...");
|
||||
timer = new Timer();
|
||||
logger.debug("Setting update period...");
|
||||
this.updatePeriod = Long.parseLong(req.getParameter("period"));
|
||||
timer.scheduleAtFixedRate(new StockTask(), 0, updatePeriod * MILLIS_PER_SECOND);
|
||||
logger.debug("Exiting doGet method...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Observable observable, Object arg) {
|
||||
|
||||
public void updateClient() {
|
||||
try {
|
||||
asyncContext.getResponse().setContentType("text/html");
|
||||
logger.debug("Updating response...");
|
||||
asyncContext.getResponse().setContentType("text/plain");
|
||||
Writer writer = asyncContext.getResponse().getWriter();
|
||||
writer.write("<span>Symbol: " + service.getSymbol() + ", Price "
|
||||
+ service.getValue() + "</span>");
|
||||
writer.write("Symbol: " + service.getSymbol() + ", Price "
|
||||
+ service.getValue() + "\n");
|
||||
writer.flush();
|
||||
asyncContext.getResponse().flushBuffer();
|
||||
logger.debug("Flushed response.");
|
||||
} catch (IOException e) {
|
||||
// ignore
|
||||
logger.debug(e.getMessage(), e);
|
||||
asyncContext.complete();
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------
|
||||
|
||||
public class StockTask extends TimerTask {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
updateClient();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package sample;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
import javax.servlet.AsyncContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
@WebServlet(asyncSupported = true, urlPatterns = "/test")
|
||||
public class TestServlet extends HttpServlet {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private AsyncContext asyncContext;
|
||||
private Logger logger = Logger.getLogger(TestServlet.class);
|
||||
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException {
|
||||
logger.debug("Received test request.");
|
||||
logger.debug("Gathering async context...");
|
||||
asyncContext = req.startAsync();
|
||||
resp.setContentType("text/plain");
|
||||
|
||||
logger.debug("Gathering writer...");
|
||||
Writer writer;
|
||||
try {
|
||||
writer = asyncContext.getResponse().getWriter();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Thread.sleep(1000);
|
||||
logger.debug("Sending " + i + "...");
|
||||
writer.write("" + i + "\n");
|
||||
writer.flush();
|
||||
resp.flushBuffer();
|
||||
}
|
||||
|
||||
logger.debug("Completing response...");
|
||||
asyncContext.complete();
|
||||
writer.close();
|
||||
} catch (IOException e1) {
|
||||
asyncContext.complete();
|
||||
e1.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
asyncContext.complete();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
log4j.rootLogger=DEBUG, CA
|
||||
#Console Appender
|
||||
log4j.appender.CA=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
|
|
@ -0,0 +1,52 @@
|
|||
web.composite
|
||||
~~~~~~~~~~~~~
|
||||
|
||||
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
|
||||
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
|
||||
targetNamespace="http://samples"
|
||||
name="Stock">
|
||||
|
||||
<component name="WebPage">
|
||||
<implementation.?/>
|
||||
<callback>
|
||||
<binding.? url="/stock/**" method="callbackJsMethod"/>
|
||||
</callback>
|
||||
</component>
|
||||
|
||||
<component name="Servlet">
|
||||
<implementation.web web-uri=""/>
|
||||
<reference name="service" target="StockComponent"/>
|
||||
</component>
|
||||
|
||||
<component name="StockComponent">
|
||||
<implementation.java class="sample.StockServiceImpl"/>
|
||||
</component>
|
||||
|
||||
</composite>
|
||||
|
||||
|
||||
stock.html
|
||||
~~~~~~~~~~
|
||||
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>Apache Tuscany Asynchronous Servlet Sample</title>
|
||||
<script type="text/javascript" src="org.apache.tuscany.callbackListener.js"></script>
|
||||
|
||||
<script language="JavaScript">
|
||||
function callbackJsMethod(response) {
|
||||
// process received response
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h2>Apache Tuscany Asynchronous Servlet Sample</h2>
|
||||
|
||||
<h3>Stock Monitor</h3>
|
||||
|
||||
<input type="button" value="Start monitoring" onclick="">
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -22,12 +22,12 @@
|
|||
targetNamespace="http://samples"
|
||||
name="Stock">
|
||||
|
||||
<component name="Servlet">
|
||||
<component name="StockServlet">
|
||||
<implementation.web web-uri=""/>
|
||||
<reference name="service" target="StockComponent"/>
|
||||
<reference name="service" target="StockService"/>
|
||||
</component>
|
||||
|
||||
<component name="StockComponent">
|
||||
<component name="StockService">
|
||||
<implementation.java class="sample.StockServiceImpl"/>
|
||||
</component>
|
||||
|
||||
|
|
|
@ -17,28 +17,31 @@
|
|||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
-->
|
||||
<web-app version="2.4"
|
||||
xmlns="http://java.sun.com/xml/ns/j2ee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
|
||||
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
|
||||
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||
version="3.0"
|
||||
metadata-complete="false">
|
||||
|
||||
<display-name>Apache Tuscany Asynchronous Servlet Sample</display-name>
|
||||
<display-name>Apache Tuscany Asynchronous Servlet Sample</display-name>
|
||||
|
||||
<listener><listener-class>org.apache.tuscany.sca.host.webapp.TuscanyContextListener</listener-class></listener>
|
||||
<listener>
|
||||
<listener-class>org.apache.tuscany.sca.host.webapp.TuscanyContextListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>AsyncServlet</servlet-name>
|
||||
<servlet-class>sample.StockServlet</servlet-class>
|
||||
</servlet>
|
||||
<context-param>
|
||||
<param-name>log4jConfigLocation</param-name>
|
||||
<param-value>WEB-INF/classes/log4j.properties</param-value>
|
||||
</context-param>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>AsyncServlet</servlet-name>
|
||||
<url-pattern>/AsyncServlet</url-pattern>
|
||||
</servlet-mapping>
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<welcome-file-list id="WelcomeFileList">
|
||||
<welcome-file>stock.html</welcome-file>
|
||||
</welcome-file-list>
|
||||
<welcome-file-list id="WelcomeFileList">
|
||||
<welcome-file>stock.html</welcome-file>
|
||||
</welcome-file-list>
|
||||
|
||||
</web-app>
|
||||
|
||||
|
|
|
@ -28,7 +28,12 @@
|
|||
|
||||
<h3>Stock Monitor</h3>
|
||||
|
||||
<input type="button" value="Start monitoring" onclick="">
|
||||
<form action="stock" method="GET">
|
||||
<label>Enter the update period (seconds): </label>
|
||||
<input type="text" name="period" size="2"/>
|
||||
<br/>
|
||||
<button type="submit">Start monitoring</button>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Add table
Reference in a new issue