消费者接收MapMessage什么都没显示

时间:2015-11-29 07:53:28

标签: jms activemq message-queue

您好我在ActiveMQ中遇到MapMessage的一个奇怪的(对我来说)问题。我的代码没有产生错误,但没有显示任何错误。

制片人代码:

public void sendMapMessageTopic(String topicName) throws Exception {
        try {
            initConnectionTopic(topicName);
            mapMessage = session.createMapMessage();
            mapMessage.setIntProperty("Age", 24);
            mapMessage.setStringProperty("Full Name", "Konstantinos Drakonakis");
            mapMessage.setStringProperty("Height", "178cm");

            List<String> data = new ArrayList<String>();
            data.add("Company");
            data.add("Project");
            mapMessage.setObject("data", data);

            Map<String, Object> specs = new HashMap<String, Object>();
            specs.put("data", data);
            mapMessage.setObject("specs", specs);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            producer.send(mapMessage);
        } catch (Exception e) {
            System.out.println("Exception while sending map message to the queue" + e.getMessage());
            throw e;
        } finally {
            if (connection != null) {
                connection.close();
                if (session != null) {
                    session.close();
                }
            }
        }
    }

消费者代码:

public void startReceivingMapMessageTopic(String topicName) throws Exception {
        try {
            //get connection factory
            connectionFactory = new ActiveMQConnectionFactory(username, password, brokerUrl);
            //create a connection
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //create destination(unless it already exists)
            queue = session.createTopic(topicName);
            consumer = session.createConsumer(queue);
            messageMap = (MapMessage) consumer.receive(1000);
            if (messageMap instanceof MapMessage) {
                MapMessage m = messageMap;
                System.out.println("The contents of MapMessage is: " + m.getStringProperty("Age"));
            }

            System.in.read();
            consumer.close();
            connection.close();
            session.close();
        } catch (Exception e) {
            System.out.println("Exception while sending message to the queue" + e.getMessage());
            throw e;
        }
    }

制作人的主要方法:

public static void main(String[] args) {
        //connect to the default broker url
        ActiveMQQueueSender sender = new ActiveMQQueueSender("tcp://localhost:61616", "admin", "admin");
        try {
            sender.sendMapMessageTopic("Map Message Topic");
        } catch (Exception ex) {
            System.out.println("Exception during" + ex.getMessage());
        }
    }

消费者的主要方法:

public static void main(String[] args) {
        ActiveMQQueueReceiver receiver = new ActiveMQQueueReceiver("tcp://localhost:61616", "admin", "admin");
        try {
            receiver.startReceivingMapMessageTopic("Map Message Topic");
        } catch (Exception ex) {
            System.out.println("Exception during receival in main class" + ex.getMessage());
        }
    }

但我什么都没得到。有什么想法吗?

0 个答案:

没有答案