JMS(主题) - 如何将客户端实现为生产者和消费者

时间:2012-11-16 15:20:27

标签: swing jms activemq jms-topic

我必须使用activeMQ设计JMS应用程序。我有2个客户应该兼任发布者和订阅者。例如。如果第一个客户端绘制了smth。,则第二个客户端应该能够在他的GUI上看到它,反之亦然。

我很清楚如何“绑定”到服务器以及如何运行activeMQ,我只是不知道如何设计客户端,在哪里运行SWING以及如何发送绘图。例如。制片人看起来像这样......

public class Producer {

private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private static String subject = "DRAWINGS";

public static void main(String[] args) throws JMSException {
    ConnectionFactory connectionFactory =
        new ActiveMQConnectionFactory(url);
    Connection connection = connectionFactory.createConnection();
    connection.start();

    Session session = connection.createSession(false,
        Session.AUTO_ACKNOWLEDGE);

    Destination destination = session.createTopic(subject);

    MessageProducer producer = session.createProducer(destination);


   // how to implement producer as consumer as well and where to create the SWING

   // and bind it to the client?


    connection.close();
}

}

1 个答案:

答案 0 :(得分:0)

您创建MessageConsumer的方式与创建MessageProducer的方式相同:

    MessageConsumer consumer = session.createConsumer(destination);
    consumer.setMessageListener(new MessageListener() {
        public void onMessage(Message m) {
            // Do something.
        }
    });

你当然需要让你通过阻塞循环运行主方法,或者只是为了这个例子的目的,但这是基础知识。

相关问题