0

How to listen activemq and pop-up a messagebox while there has a new message in mq?

asked 2014-02-21 11:18:24 +0800

wch10610 gravatar image wch10610
1

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();
        }
    }
}

}

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-04-10 02:28:41 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

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);
}
link publish delete flag offensive edit
0

answered 2014-12-15 23:27:49 +0800

Geomancer gravatar image Geomancer
1

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.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2014-02-21 11:18:24 +0800

Seen: 24 times

Last updated: Dec 15 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More