QPID例子二(DEMO)
2015-11-05 16:37
351 查看
首先确保QPID breaker已经正常运行
目录结构如下
1、创建一个QpidHelloWorld.java
如果出现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
目录结构如下
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
相关文章推荐
- 深入浅出RxJava(一:基础篇)
- Xcode快捷键
- 使用maven运行Java main的2种方式
- Java-URL类详解
- WampSever 远程访问出现Forbidden You don’t have permission to access / on this server.解决方法
- hdu1978 记忆化搜索
- 【05】emmet系列之各种缩写
- 详解Java中多进程编程的实现
- Oralce drop all tables
- ActiveDirectory之忘记管理员密码时的重置方法
- Android适配之适配不同的系统版本
- java同步代码块 实现的买票功能
- Express重定向
- 怎么判断win7中svchost.exe程序是否是病毒?
- L-BFGS
- Socket类的getInputStream方法与getOutputStream方法的使用
- [转]FastCGI模式下PHP占用大量内存的解决办法
- PHP实现快排
- JSP到Servlet的编译过程初探
- mongoDB的基本操作