-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I'm trying to listen activemq and pop-up a messagbox while there has a new message in MQ. Although It shows there has a new message in MQ and my controller can print that message on log, the messagebox doesn't show up. How can I fix this probelm.
bellow is my code:
public class A0102Controller extends SelectorComposer<component> implements MessageListener{
private static final long serialVersionUID = -7823022704116547799L;
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
System.out.println("doAfterCompose...");
startListening();
}
public void startListening() {
System.out.println("Subscriber.startListening()");
try {
ConnectionFactory factory = getJmsConnectionFactory();
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("ServerMsg");
MessageConsumer consumer = session.createConsumer(topic);
consumer.setMessageListener(this);
connection.start();
} catch (JMSException e) {
System.err.println("Exception occurred: " + e.toString());
}
}
/**
* Use the ActiveMQConnectionFactory to get a JMS ConnectionFactory. In an
* enterprise application this would normally be accessed through JNDI.
*/
public static ConnectionFactory getJmsConnectionFactory()
throws JMSException {
String user = ActiveMQConnection.DEFAULT_USER;
String password = ActiveMQConnection.DEFAULT_PASSWORD;
String url = ActiveMQConnection.DEFAULT_BROKER_URL;
return new ActiveMQConnectionFactory(user, password, url);
}
@Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
try {
String msg = txtMsg.getText();
System.out.println("Server Msg : " + msg);
Messagebox.show("Time is up!!", "Warning", Messagebox.OK, Messagebox.EXCLAMATION);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
Hi you have to use ZK event queue to show the message http://books.zkoss.org/wiki/ZKDeveloper'sReference/MVC/Controller/SubscribetoEventQueues
EventQueue<Event> eq = EventQueues.lookup("activemq", EventQueues.DESKTOP, true);
@Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
try {
String msg = txtMsg.getText();
System.out.println("Server Msg : " + msg);
eq.publish(new Event("onMyEvent", null, msg ));
} catch (JMSException e) {
e.printStackTrace();
}
}
}
@Subscribe("activemq")
public void method1(Event event) {
System.out.println("Server Msg : " + event.getData());
Messagebox.show("Time is up!!", "Warning", Messagebox.OK, Messagebox.EXCLAMATION);
}
Hi, I'm trying to achieve the same as the original poster, I copied the exact code with Jimmy suggestion, but it's giving the following error "publish() can be called only in an event listener" (scope DESKTOP), and changing the scope to APPLICATION, it doesn't gives any error, so I assume it's working but it's never executing method1.
What else could I check? I'm using zk 7.0.3.
Asked: 2014-02-21 11:18:24 +0800
Seen: 24 times
Last updated: Dec 15 '14