QPID例子二(DEMO)

首先确保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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牟云飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值