qpid之简单实例

原创 2014年11月10日 16:17:17

  qpid的官方教程

    http://qpid.apache.org/releases/qpid-0.28/programming/book/index.html

   http://qpid.apache.org/releases/qpid-0.28/java-broker/book/index.html

客户端api

  http://people.apache.org/~grkvlt/qpid-site/qpid-java/qpid-client/apidocs/overview-summary.html

JMS的API

https://docs.oracle.com/javaee/1.4/api/overview-summary.html

 示例代码 在这可以下载

 https://svn.apache.org/repos/asf/qpid/trunk/qpid/java/client/example


好了 上面即是qpid的参考手册  很多有用的资料都在上面结果链接里


还是看看  示例代码吧  HelloWorld开始

package com.undergrowth.qpid;

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();
	        }
	    }
}


配置文件 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://localhost:5672'

# Register an AMQP destination in JNDI
# destination.[jniName] = [Address Format]
destination.topicExchange = amq.topic


上面的代码 都加了必要的注释  就不多少了  详情在上面的参考网址上都可以找到




当然 上面的代码运行时 需要先运行qpid-server  脚本  启动qpid代理才行




启动完 qpid-server 后 控制台即可访问  http://localhost:8080/management


2、传递map消息

MapSender

/*
 *
 * 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.undergrowth.qpid;

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 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //create: always 表示如果队列不存在 则创建
        Destination queue = new AMQAnyDestination("ADDR:message_queue; {create: always}");
        MessageProducer producer = session.createProducer(queue);

        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();
    }

}


MapReceive

</pre></p><p><pre name="code" class="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.undergrowth.qpid;

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 MapReceiver {

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

        connection.start();
        
        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();
    }

}



对于上面的例子 方式与helloworld很想 只不过传递的是MapMessage

对于地址参数 附加一张图



  在上面的源码上 还有另外几个例子 如传递ListMessage 还有带参数的传递之类的




对于broker的概念 下面几张图 比较好 就截出来了







    记录学习的脚步    

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Centos 7.2 安装 Ambari 2.2.2 + HDP 2.4.2 搭建Hadoop集群

Centos 7.2 安装 Ambari 2.2.2 + HDP 2.4.2 搭建Hadoop集群

qpid 记录

最近在折腾qpid,遇到一些问题,记录一下 1)qpid-proton pqid broker的关系 qpid-proton的核心是Messenger,也就是一个AMQP兼容的开发包(可以作为...

消息机制qpid学习(下)

引子 前一部分介绍了 Qpid 的概括,希望您已经了解什么是 Qpid,可以下载安装它,并自己亲手实验几个小例子。在第二部分,我想稍微深入 Qpid 内部,澄清一些概念,加深理解,并介绍 Qpi...

qpid入门

Apache Qpid (Open Source AMQP Messaging) 是一个跨平台的企业通讯解决方案,实现了高级消息队列协议。提供了 Java、C++ 两种服务端版本以及 Java、C++...

消息机制Qpid学习(上)

简介: Qpid 是 Apache 开发的一款面向对象的消息中间件,它是一个 AMQP 的实现,可以和其他符合 AMQP 协议的系统进行通信。Qpid 提供了 C++/Python/Java/C# 等...

spring 集成apache qpid 简单demo

spring 提供spring-jms模块,用以集成JMS到spring 容器中,并提供jmsTemplate模版类来操作jms,类似集成jdbc数据库操作一样。首先,我们新建一个applicatio...

qpid java broker的安装和配置

apache qpid 是一款实现amqp协议的消息中间件,qpid的简介参考https://www.baidu.com/link?url=JT6nIIVFn78HyQeLhOoJu4xcyrrnUl...

QPID例子二(DEMO)

首先确保QPID breaker已经正常运行 目录结构如下 1、创建一个QpidHelloWorld.java package com; import java.io.InputSt...

[2]AMQP(高级消息队列协议) ----QPID不得不说的事

如果说到AMQP协议,则不得不提的就是QPID。QPID的论坛现在十分的活跃,基本上白天提的问题,晚上马上就能得到回复。由此可见QPID的活跃程度。 大家可以到http://qpid.2158936....

[4]AMQP(高级消息队列协议) ----改造QPID JMS-0.9 API 对 SSLContext的 支持

默认情况下QPID JMS-0.9 API对SSLConext没有提供相应的接口. 但是在笔者项目中,又需要其对SSLContext对象的支持。这个问问深深的难住了笔者2~3天时间,笔者通过深入的研究...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)