Java Reference
In-Depth Information
Listing 12.2
Subscriber.java - onMessage()
public void onMessage( final Message msg) {
try {
// must run this on the JavaFX Main thread
// If you don't you will eventually get
// exceptions in the JavaFX code.
Entry.deferAction( new Runnable() {
@Override
public void run() {
fxListener.onMessage(msg);
}
} );
} catch (Exception ex) {
Logger.getLogger(Subscriber.class.getName()).
log(Level.SEVERE, null, ex);
}
}
On the JavaFX side in ClockUpdater.fx , the implementation is normal JavaFX.
The message is checked to make sure it is a javax.jms.TextMessage , and then
the JSON string is set and parsed. Listing 12.3 shows how this is done.
Listing 12.3
ClockUpdater.fx
public class ClockUpdater extends MessageListener {
public var millis: Number;
...
...
// this is called from the Java class Subscriber
public override function onMessage(msg: Message):Void {
if( msg instanceof TextMessage ) {
var jsonStr = (msg as TextMessage).getText();
var input =
new ByteArrayInputStream(jsonStr.getBytes());
try {
parser.input = input;
parser.parse();
} finally {
input.close();
}
}
}
 
Search WWH ::




Custom Search