QPID例子二(DEMO)

来源:互联网 发布:windows redstone 2 编辑:程序博客网 时间:2024/06/05 07:13

首先确保QPID breaker已经正常运行




目录结构如下


1、创建一个QpidHelloWorld.java

package com;import java.io.InputStream;import java.util.Properties;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.MessageConsumer;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;import javax.naming.Context;import javax.naming.InitialContext;public class QpidHelloWorld {public static void main(String[] args) {QpidHelloWorld hello = new QpidHelloWorld();hello.runTest();}private void runTest()     {        try        {        InputStream resourceAsStream = this.getClass().getResourceAsStream("hello.properties");            Properties properties = new Properties();            properties.load(resourceAsStream);            //使用配置文件创建JNDI的上下文  这里指的是PropertiesFileInitialContextFactory            Context context = new InitialContext(properties);                //从JNDI中获取连接工厂 qpidConnectionfactory              ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("qpidConnectionfactory");            //使用连接工厂创建连接  amqp://guest:guest@test/?brokerlist='tcp://localhost:5672'            //格式如下 amqp://[<user>:<pass>@][<clientid>]<virtualhost>[?<option>='<value>'[&<option>='<value>']]            //brokerlist的格式 如下 brokerlist=<transport>://<host>[:<port>](?<param>='<value>')(&<param>='<value>')*            Connection connection = connectionFactory.createConnection();            connection.start();                //在连接内创建会话            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);            //从JNDI中获取目的地址  这里的目的地址是 amq.topic            //This exchange type is used to support the classic publish/subscribe paradigm.            //amp.topic 类似于 jms的 发布/订阅  模式            Destination destination = (Destination) context.lookup("topicExchange");            //从会话中产生生产者与消费者            MessageProducer messageProducer = session.createProducer(destination);            MessageConsumer messageConsumer = session.createConsumer(destination);            //产生文本消息            TextMessage message = session.createTextMessage("Hello world!");            //发送文本消息            messageProducer.send(message);            //接收消息            //This call blocks indefinitely until a message is produced or until this message consumer is closed.            message = (TextMessage)messageConsumer.receive();            System.out.println(message.getText());            //关闭资源连接            connection.close();            context.close();        }        catch (Exception exp)         {            exp.printStackTrace();        }    }}
2、同级目录下,创建hello.properties

## 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.#java.naming.factory.initial = org.apache.qpid.jndi.PropertiesFileInitialContextFactory# register some connection factories# connectionfactory.[jndiname] = [ConnectionURL]connectionfactory.qpidConnectionfactory = amqp://guest:guest@test/?brokerlist='tcp://172.20.184.218:5672'# Register an AMQP destination in JNDI# destination.[jniName] = [Address Format]destination.topicExchange = amq.topic


       如果出现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


0 0
原创粉丝点击