QPID通讯代码03

  • TextMessage
  • ListMessage
  • MapMesage
  • StreamMessage

1、MqConsumerMap.java

package com.neohope.qpid.test;

import org.apache.qpid.client.AMQAnyDestination;
import org.apache.qpid.client.AMQConnection;

import javax.jms.*;
import java.util.Enumeration;

public class MqConsumerMap {
    public static void main(String[] args) throws Exception {
        Connection connection =
                new AMQConnection("amqp://guest:guest@test/?brokerlist='tcp://localhost:5672'");

        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination queue = new AMQAnyDestination("ADDR:message_queue; {create: always}");
        MessageConsumer consumer = session.createConsumer(queue);

        System.out.println("Receiving as MapMessage");
        MapMessage m = (MapMessage) consumer.receive();
        System.out.println(m);
        System.out.println("==========================================");
        System.out.println("Printing map contents:");
        Enumeration keys = m.getMapNames();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            System.out.println(key + " => " + m.getObject(key));
        }
    }
}

2、MqProducerText.java

package comsg.neohope.qpid.test;

import org.apache.qpid.client.AMQAnyDestination;
import org.apache.qpid.client.AMQConnection;

import javax.jms.*;
import java.util.*;

public class MqProducerMap {
    public static void main(String[] args) throws Exception
    {
        Connection connection =
                new AMQConnection("amqp://guest:guest@test/?brokerlist='tcp://localhost:5672'");

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination queue = new AMQAnyDestination("ADDR:message_queue; {create: always}");
        MessageProducer producer = session.createProducer(queue);

        MapMessage msg = session.createMapMessage();
        msg.setIntProperty("IntValue", 99);
        msg.setStringProperty("StringValue", "-=99=-");
        msg.setDoubleProperty("DoubleValue", 0.99);

        List<String> stringlistTest = new ArrayList<String>();
        stringlistTest.add("99");
        stringlistTest.add("-=99=-");
        stringlistTest.add("0.99");
        msg.setObject("stringlistTest", stringlistTest);

        Map<String, String> stringMapTest = new HashMap<String, String>();
        stringMapTest.put("key01", "99");
        stringMapTest.put("key02", "-=99=-");
        stringMapTest.put("key03", "0.99");
        msg.setObject("stringMapTest",stringMapTest);

        List<List<Integer>> listlistTest = new ArrayList<List<Integer>>();
        listlistTest.add(Arrays.asList(new Integer[]{1, 3, 5}));
        listlistTest.add(Arrays.asList(new Integer[]{2, 4,6}));
        msg.setObject("listlistTest", listlistTest);

        Map<String, Object> objectMapTest = new HashMap<String, Object>();
        objectMapTest.put("stringlistTest", stringlistTest);
        objectMapTest.put("stringMapTest", stringMapTest);
        objectMapTest.put("parts", listlistTest);
        msg.setObject("objectMapTest",objectMapTest);

        producer.send(msg);
        connection.close();
    }
}

3、测试

#开启qpid-broker
#运行MqConsumerMap.Main
#运行MqProducerMap.Main

Leave a Reply

Your email address will not be published. Required fields are marked *

*