Java Reference
In-Depth Information
javax.jms.MessageListener
interface and registers itself with the JMS client
framework to be notified whenever a new message arrives. The
MessageListener
defines a method that
Subscriber
must implement,
public void onMessage(
Message msg)
.
When a message is received by the JMS client, it in turn calls the
onMessage()
method in
Subscriber
.
ClockUpdater
is a JavaFX class and it also extends
javax.jms.MessageListener
and implements its own
onMessage()
function. The reason we cannot use this
directly with the JMS client framework is that the JMS client framework would
call this function using one of its threads, not the main JavaFX thread. Modifying
JavaFX objects on any other thread than the main JavaFX thread is not supported
and likely will cause a serious exception.
When the JMS client calls the Java
Subscriber.onMessage()
method, it will not
be on the JavaFX main thread. We need to move the message over to the JavaFX
main thread, and then push that message over to the JavaFX object. To do this,
we need to use the
com.sun.javafx.runtime.Entry.deferAction()
method
to invoke the JavaFX function on the main JavaFX thread.
The constructor for
Subscriber
takes two arguments: the first identifies the JMS
topic, and the second is the JavaFX class to notify when a message is detected from
JMS. Because the JavaFX class extends the Java interface
MessageListener
, we
can refer to this class directly. This is shown as Java code in Listing 12.1.
Listing 12.1
Subscriber.java
...
...
private MessageListener
fxListener
;
public Subscriber(String topicName,
MessageListener
fxListener
) throws JMSException {
this.fxListener = fxListener
;
...
...
When the JMS client calls the
Subscriber.onMessage(Message msg)
Java
method, we then use the
Entry.deferAction()
method to call the JavaFX
onMessage(msg:Message)
function from the JavaFX main thread. Notice that
we need to change the message parameter to the Java
onMessage()
method to
final. This is so that it can be visible when the
deferAction Runnable
is
invoked. Listing 12.2 shows the Java
onMessage(Message msg)
implementation.
Search WWH ::
Custom Search