QPID例子一(DEMO)

首先确保打开了QPID


1、发送端MapSender.java

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package com;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;

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


public class MapSender {

    public static void main(String[] args) throws Exception 
    {
    	//建立连接
        Connection connection = 
            new AMQConnection("amqp://guest:guest@test/?brokerlist='tcp://localhost:5672'");
        //获取session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //create: always 表示如果队列不存在 则创建
        Destination queue = new AMQAnyDestination("ADDR:message_queue; {create: always}");
        //创建一个生产者,用来发送消息
        MessageProducer producer = session.createProducer(queue);
        //创建一个map消息,还有textMessage等
        MapMessage m = session.createMapMessage();
        //消息中放入各种键——值信息
        m.setIntProperty("Id", 987654321);
        m.setStringProperty("name", "Widget");
        m.setDoubleProperty("price", 0.99);
        
        List<String> colors = new ArrayList<String>();
        colors.add("red");
        colors.add("green");
        colors.add("white");        
        m.setObject("colours", colors);
        
        Map<String,Double> dimensions = new HashMap<String,Double>();
        dimensions.put("length",10.2);
        dimensions.put("width",5.1);
        dimensions.put("depth",2.0);
        m.setObject("dimensions",dimensions);
        
        List<List<Integer>> parts = new ArrayList<List<Integer>>();
        parts.add(Arrays.asList(new Integer[] {1,2,5}));
        parts.add(Arrays.asList(new Integer[] {8,2,5}));
        m.setObject("parts", parts);
        
        Map<String,Object> specs = new HashMap<String,Object>();
        specs.put("colours", colors);
        specs.put("dimensions", dimensions);
        specs.put("parts", parts);
        m.setObject("specs",specs);
        //发送消息
        producer.send(m);
        //关闭连接
        connection.close();
    }

}

2、信息接受端MapReceive.java

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package com;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.MessageConsumer;
import javax.jms.Session;

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


public class MapReceive {

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

        connection.start();
        //获取session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //获取队列
        Destination queue = new AMQAnyDestination("ADDR:message_queue; {create: always}");
        MessageConsumer consumer = session.createConsumer(queue);
        //接受消息
        MapMessage m = (MapMessage)consumer.receive();
        //输出消息
        System.out.println(m);     
        //关闭连接
        connection.close();
    }

}


---------接受到信息的效果图:




       如果出现Unsupported major.minor version 51.0异常,那是可能你下载的qpid jar包的编译版本太高,一个方法反编译jar包重新编译,另一个方法提高jdk版本,例如apache-qpid-jms-0.5.0下载下来则是jdk1.7编译的


JAR包下载地址:http://download.csdn.net/detail/myfmyfmyfmyf/9244513

你可能感兴趣的:(QPID例子一(DEMO))