diff options
author | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2011-05-24 14:28:46 +0000 |
---|---|---|
committer | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2011-05-24 14:28:46 +0000 |
commit | 2f83f5e8e091074cbd4e3d0d7cc15af22411314c (patch) | |
tree | 478a38c939ac9bfa46cb03ca3c8ea7d87ffe5562 /sca-java-2.x/contrib | |
parent | e03b437442335d33ad52d31a750701933fb021d0 (diff) |
Update sample to reflect latest upgrades in the comet module.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1127081 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/contrib')
5 files changed, 53 insertions, 40 deletions
diff --git a/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/HumidityService.java b/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/HumidityService.java index 1eae6802cd..7145e30e03 100644 --- a/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/HumidityService.java +++ b/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/HumidityService.java @@ -22,12 +22,14 @@ package org.apache.tuscany.sca.sample.comet; import org.apache.tuscany.sca.binding.comet.runtime.callback.CometCallback; import org.apache.tuscany.sca.sample.comet.model.Location; import org.oasisopen.sca.annotation.Callback; +import org.oasisopen.sca.annotation.OneWay; import org.oasisopen.sca.annotation.Remotable; @Remotable @Callback(CometCallback.class) public interface HumidityService { + @OneWay void getHumidity(Location location); }
\ No newline at end of file diff --git a/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationServiceImpl.java b/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationServiceImpl.java index 4798a3457e..2c064a3ae6 100644 --- a/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationServiceImpl.java +++ b/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/PrecipitationServiceImpl.java @@ -20,8 +20,11 @@ package org.apache.tuscany.sca.sample.comet; import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; import org.apache.tuscany.sca.binding.comet.runtime.callback.CometCallback; +import org.apache.tuscany.sca.binding.comet.runtime.callback.Status; import org.apache.tuscany.sca.sample.comet.model.Location; import org.apache.tuscany.sca.sample.comet.model.Response; import org.oasisopen.sca.annotation.Callback; @@ -31,22 +34,23 @@ import org.oasisopen.sca.annotation.Service; public class PrecipitationServiceImpl implements PrecipitationService { @Callback - protected CometCallback callback; + protected CometCallback client; @Override public void getPrecipitation(final Location location) { - while (callback.isClientConnected()) { - Response response = new Response(); - response.setDate(new Date()); - response.setData(Helper.randomInt(100) + "%"); - - callback.sendResponse(response); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); + new Timer().scheduleAtFixedRate(new TimerTask() { + + @Override + public void run() { + Response response = new Response(); + response.setDate(new Date()); + response.setData(Helper.randomInt(100) + "%"); + Status status = client.sendMessage(response); + if (status == Status.CLIENT_DISCONNECTED) { + System.out.println("Client disconnected from PrecipitationService."); + this.cancel(); + } } - } + }, 0L, 1000L); } - } diff --git a/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureHumidityServiceImpl.java b/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureHumidityServiceImpl.java index 41654a417b..4227667e7f 100644 --- a/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureHumidityServiceImpl.java +++ b/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureHumidityServiceImpl.java @@ -20,51 +20,57 @@ package org.apache.tuscany.sca.sample.comet; import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; import org.apache.tuscany.sca.binding.comet.runtime.callback.CometCallback; +import org.apache.tuscany.sca.binding.comet.runtime.callback.Status; import org.apache.tuscany.sca.sample.comet.model.Location; import org.apache.tuscany.sca.sample.comet.model.Response; import org.oasisopen.sca.annotation.Callback; import org.oasisopen.sca.annotation.Service; @Service({ TemperatureService.class, HumidityService.class }) -public class TemperatureHumidityServiceImpl implements TemperatureService, - HumidityService { +public class TemperatureHumidityServiceImpl implements TemperatureService, HumidityService { @Callback protected CometCallback callback; @Override public void getHumidity(final Location location) { - while (callback.isClientConnected()) { - final Response response = new Response(); - response.setDate(new Date()); - response.setData(Helper.randomInt(90) + "%"); - callback.sendResponse(response); - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); + new Timer().scheduleAtFixedRate(new TimerTask() { + + @Override + public void run() { + final Response response = new Response(); + response.setDate(new Date()); + response.setData(Helper.randomInt(90) + "%"); + Status status = callback.sendMessage(response); + if (status == Status.CLIENT_DISCONNECTED) { + System.out.println("Client disconnected from HumidityService."); + this.cancel(); + } } - } + }, 0L, 5000L); } @Override public void getTemperature(final Location location, final int scale) { - while (callback.isClientConnected()) { - final Response response = new Response(); - response.setDate(new Date()); - final String data = "" - + Helper.randomInt(scale == TemperatureService.CELSIUS ? 40 - : 150); - response.setData(data); - callback.sendResponse(response); - try { - Thread.sleep(3000); - } catch (InterruptedException e) { - e.printStackTrace(); + new Timer().scheduleAtFixedRate(new TimerTask() { + + @Override + public void run() { + final Response response = new Response(); + response.setDate(new Date()); + final String data = "" + Helper.randomInt(scale == TemperatureService.CELSIUS ? 40 : 150); + response.setData(data); + Status status = callback.sendMessage(response); + if (status == Status.CLIENT_DISCONNECTED) { + System.out.println("Client disconnected from TemperatureService."); + this.cancel(); + } } - } + }, 0L, 3000L); } }
\ No newline at end of file diff --git a/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureService.java b/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureService.java index 385107c6ac..83afae696f 100644 --- a/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureService.java +++ b/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/java/org/apache/tuscany/sca/sample/comet/TemperatureService.java @@ -22,6 +22,7 @@ package org.apache.tuscany.sca.sample.comet; import org.apache.tuscany.sca.binding.comet.runtime.callback.CometCallback; import org.apache.tuscany.sca.sample.comet.model.Location; import org.oasisopen.sca.annotation.Callback; +import org.oasisopen.sca.annotation.OneWay; import org.oasisopen.sca.annotation.Remotable; @Remotable @@ -31,6 +32,7 @@ public interface TemperatureService { public static final int CELSIUS = 1; public static final int FAHRENHEIT = 2; + @OneWay void getTemperature(Location location, int scale); }
\ No newline at end of file diff --git a/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/webapp/index.html b/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/webapp/index.html index 43dcb607a2..92cfbee6f0 100644 --- a/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/webapp/index.html +++ b/sca-java-2.x/contrib/samples/learning-more/binding-comet/weather-webapp/src/main/webapp/index.html @@ -59,8 +59,7 @@ document.getElementById('connect').onclick = function(event) { /* transport can be : long-polling or streaming */ SCA.TuscanyComet.connect(document.getElementById('transport').value); - document.getElementById('connect').disabled = true; - document.getElementById('transport').disabled = true; + document.getElementById('connect').value = 'Switch'; document.getElementById('locationButton').disabled = false; } |