Skip to main content

Reading from JMS, updating table

3 replies [Last post]
shahbazc
Offline
Joined: 2008-04-06

Hi, I would like to read JMS topics in JavaFX and push them into a Table view (which will updated as new messages come in).

I am using the Qpid implementation of JMS have the following code is some scratch work I've done. This compiles correctly, the application starts up, but I get no updates. A fairly similar Java program does read the JMS topic. (in the example below, I'm just trying to print incoming messages to the console, as well as updating a text field):

----------------------------------------------------
package javafxtest;

import javafx.ui.*;
import org.apache.qpid.client.AMQConnection;
import javax.jms.Session;
import org.apache.qpid.client.AMQQueue;
import javax.jms.MessageListener;
import javax.jms.Message;
import java.lang.System;

class JMSService{

public function startConnection(myMessageListener:MessageListener):Void{
var prop = new java.util.Properties();
prop.setProperty("java.naming.factory.initial","org.apache.qpid.jndi.PropertiesFileInitialContextFactory");
prop.setProperty("connectionfactory.qpidConnectionfactory","amqp://guest:guest@1/test?brokerlist='tcp://xxx.xxx.xxx.xxx:xxxx'");
prop.setProperty("topic.bids","md.bids");
var ctx = new javax.naming.InitialContext(prop);

var conFac:javax.jms.ConnectionFactory = ctx.lookup("qpidConnectionfactory") as javax.jms.ConnectionFactory;
var connection = conFac.createConnection() as javax.jms.TopicConnection;
var session = connection.createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
var destinationQ = new AMQQueue("amq.direct","md.bids");
var consumerQ = session.createConsumer(destinationQ);

consumerQ.setMessageListener(myMessageListener);

}
}

class MsgListener extends MessageListener{

public function onMessage(m:Message){
debugText = m.toString();
System.out.println("yo");
}
}

var jms = JMSService{};
jms.startConnection(MsgListener{});

var debugText:String = "No updates";
Frame {
height: 120
width:500
title:"Fill Test"
content: TextArea {text: bind debugText }
visible: true
}

//session.close();
//connection.close();

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
shahbazc
Offline
Joined: 2008-04-06

One potential simplification:
I was using Qpid specific:

var destinationQ = new AMQQueue("amq.direct","md.bids");
var consumerQ = session.createConsumer(destinationQ);

I've now switched to JMS style:

var consumerQ = session.createSubscriber(ctx.lookup("bids") as javax.jms.Topic);

("bids" is a JMS topic which publishes stock market data).

Doesn't seem to make a difference though

shahbazc
Offline
Joined: 2008-04-06

Added a "connection.start();" just after "consumerQ.setMessageListener(myMessageListener);"

Works :)

Let's see how difficult it is to wire it up correctly to a Table :)

rabalder
Offline
Joined: 2009-05-06

This helped me out a lot! Thnx I did the same with ActiveMQ connections... Figured I would post How that turned out...

package ActiveMQ;

import javax.jms.MessageListener;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.ActiveMQTopicSession;
import org.apache.activemq.ActiveMQTopicSubscriber;

public class ActiveMQCon {

public function startConnection(meldingsLytter:MessageListener):Void {

var connection:ActiveMQConnection = ActiveMQConnection.makeConnection("tcp://localhost:61616");

var session:ActiveMQTopicSession = connection.createTopicSession(false,1) as ActiveMQTopicSession; // 1 = AUTO_ACKNOWLEDGE

var topic = session.createTopic("SystemMeldinger");

var subscriber:ActiveMQTopicSubscriber = session.createSubscriber(topic) as ActiveMQTopicSubscriber;

subscriber.setMessageListener(meldingsLytter);
connection.start();
}
}

MessageListener
package ActiveMQ;

import javax.jms.MessageListener;
import javax.jms.Message;

public class TopicListener extends MessageListener {

public override function onMessage(m:Message){

println("Message Recieved");

}
}

var amq = ActiveMQCon{};
amq.startConnection(TopicListener{});