QPID通讯代码02

  • TextMessage
  • ListMessage
  • MapMesage
  • StreamMessage

1、MqConsumerList.java

package com.neohope.qpid.test;

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

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

public class MqConsumerList {
    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 ListMessage");
        ListMessage m = (ListMessage) consumer.receive();
        System.out.println(m);
        System.out.println("==========================================");
        System.out.println("Printing list contents:");
        Iterator i = m.iterator();
        while (i.hasNext())
            System.out.println(i.next());

        connection.close();
    }
}

2、MqProducerList.java

package com.neohope.qpid.test;

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

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

public class MqProducerList {
    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);

        ListMessage msg = ((org.apache.qpid.jms.Session) session).createListMessage();
        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.add(stringlistTest);

        Map<String, String> stringMapTest = new HashMap<String, String>();
        stringMapTest.put("key01", "99");
        stringMapTest.put("key02", "-=99=-");
        stringMapTest.put("key03", "0.99");
        msg.add(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.add(listlistTest);

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

        producer.send((Message) msg);
        System.out.println("Sent: " + msg);
        connection.close();
    }
}

3、测试

#开启qpid-broker
#运行MqConsumerList.Main
#运行MqProducerList.Main

Comments are closed.