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
- QPID例子二(DEMO)
- QPID例子一(DEMO)
- spring 集成apache qpid 简单demo
- Qpid
- DEMO:通讯录(二)
- 消息机制Qpid学习(上)
- 消息机制qpid学习(下)
- ibatis 例子(二)
- 多态(二)--例子
- CefSharp之二--如何看懂demo中的例子,以及按照例子进行开发
- SlidingMenu使用(二) Demo
- RabbitMQ(二):实例demo
- android demo(二):霓虹灯
- words Demo总结(二)
- qpid入门
- qpid 记录
- qpid初学
- moootools的demo例子
- 使用数据库版本同步工具phinx
- Scala开发环境配置
- TPM Software Stack
- 日期及时间选择对话框
- [前端] chrome浏览器定义滚动条样式及字体中英对照
- QPID例子二(DEMO)
- ARM+Linux系统启动流程分析----ARM处理器的启动流程
- JAVA学习_00
- win7中如何解决“该项不适用于在指定状态下使用”
- Junit使用教程(二)
- (转)C语言函数sscanf()的用法
- Java面向对象基础
- jstl中empty
- phpexcel基本属性及应用