Hi, friends, I managed to create simple application that has ListGrid which receives data from active mq. Here is what i have done:
My entry point, that has just one list grid and method onMessage(String message)
public class Test implements EntryPoint {
private ListGrid grid;
private DataSource ds;
private static int count = 0;
@Override
public void onModuleLoad() {
initCallBacks();
final Canvas canvas = new Canvas();
ds = TestDataSource.getInstance();
RootPanel.get("nameFieldContainer").add(canvas);
grid = new ListGrid();
grid.setWidth(500);
grid.setHeight(224);
grid.setAlternateRecordStyles(true);
grid.setEmptyCellValue("-");
grid.setDataSource(ds);
final ListGridField countryCodeField = new ListGridField("price", "price");
grid.setFields(countryCodeField);
grid.setSortField(0);
grid.setDataPageSize(50);
grid.setAutoFetchData(true);
grid.setScrollRedrawDelay(50);
grid.setDragScrollDelay(200);
canvas.addChild(grid);
canvas.draw();
}
public native void initCallBacks() /*-{
var self = this;
$wnd.setupOnMessageCallback(function(a){self.@test.client.Test::onMessage(Ljava/lang/String;)(a);});
}-*/;
public void onMessage(final String message) {
count++;
// <price stock='5309' bid='1.234'/>
final ListGridRecord record = new ListGridRecord();
if (message != null && count == 100) {
count = 0;
record.setAttribute("price", message);
ds.addData(record);
}
}
Notice that onModuleLoad i call initCallBacks method that is native. With it i pass the onMessage method to some js and that js use it when message arrives from the amq.
Simple portfolio.js taken from the amq samples(just place it in the host page dir) :
/**
* 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.
*/
function test() {
this.onMessage = null;
}
var _test = new test();
function setupOnMessageCallback(fn) { _test.onMessage = fn; alert('init');}
var priceHandler =
{
_price: function(message)
{
if (message != null) {
_test.onMessage(message);
var price = parseFloat(message.getAttribute('bid'))
var symbol = message.getAttribute('stock')
var row = document.getElementById(symbol)
if (row) {
// now lets update the HTML DOM
updateCell(find(row, 'price'), price);
}
}
}
}
function updateCell(element, value)
{
var prev = Number(element.innerHTML);
element.innerHTML = fixedDigits(value, 2);
element.className = prev!= null && prev < value ? 'up' : 'down';
}
function debug(message)
{
var div = document.getElementById('debug');
div.innerHTML = div.innerHTML + "<br>" + message;
}
function portfolioPoll(first)
{
if (first)
{
amq.addListener('stocks','topic://STOCK.*',priceHandler._price);
//amq.addListener('stocks','topic://BET.1005301',betHandler._price);
//amq.addListener('stocks','topic://BET.1095301',priceHandler._bet);
}
}
amq.addPollHandler(portfolioPoll);
/// -----------------
// Original code by Joe Walnes
// -----------------
/*** Convenience methods, added as mixins to standard classes (object prototypes) ***/
/**
* Return number as fixed number of digits.
*/
function fixedDigits(t, digits) {
return (t.toFixed) ? t.toFixed(digits) : this
}
/**
* Find direct child of an element, by id.
*/
function find(t, id) {
for (i = 0; i < t.childNodes.length; i++) {
var child = t.childNodes[i]
if (child.id == id) {
return child
}
}
return null
}
/**
* Return the text contents of an element as a floating point number.
*/
function asFloat(t) {
return parseFloat(t.innerHTML)
}
And finally simple host page created with google plugin and modified by me:
Test.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- The HTML 4.01 Transitional DOCTYPE declaration-->
<!-- above set at the top of the file will set -->
<!-- the browser's rendering engine into -->
<!-- "Quirks Mode". Replacing this declaration -->
<!-- with a "Standards Mode" doctype is supported, -->
<!-- but may lead to some differences in layout. -->
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- -->
<!-- Consider inlining CSS to reduce the number of requested files -->
<!-- -->
<script type="text/javascript" src="/demo/amq/amq.js"></script>
<script type="text/javascript">amq.uri='/demo/amq';</script>
<script type="text/javascript" language="javascript" src="portfolio.js"></script>
<link type="text/css" rel="stylesheet" href="Test.css">
<script type="text/javascript">var isomorphicDir="test/sc/"</script></script>
<!-- -->
<!-- Any title is fine -->
<!-- -->
<title>Web Application Starter Project</title>
<!-- -->
<!-- This script loads your compiled module. -->
<!-- If you add any GWT meta tags, they must -->
<!-- be added before this line. -->
<!-- -->
<script type="text/javascript" language="javascript" src="test/test.nocache.js"></script>
</head>
<!-- -->
<!-- The body can have arbitrary html, or -->
<!-- you can leave the body empty if you want -->
<!-- to create a completely dynamic UI. -->
<!-- -->
<body>
<!-- OPTIONAL: include this if you want history support -->
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
<h1>Web Application Starter Project</h1>
<table align="center">
<tr>
<td colspan="2" style="font-weight:bold;">Please enter your name:</td>
</tr>
<tr>
<td id="nameFieldContainer"></td>
</tr>
</table>
</body>
</html>
The idea is to pass the onMessage method reference to the js and it will use it to pass messages to the gwt code.
Note: Active mq and the gwt war must run on the same server. Look for the missing js files that are used in the host page in the active mq samples. (I am using active mq 5.2)
I hope this will help someone.
I am willing also to see how this is done in smart gwt ee, if someone could [point me!