`
longgangbai
  • 浏览: 7378083 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

MQTT的学习研究(八)基于HTTP DELETE MQTT 订阅消息服务端使用

阅读更多

 参看官方文档

 

 


HTTP DELETE 订阅主题请求协议和响应协议
http://publib.boulder.ibm.com/infocenter/wmqv7/v7r0/topic/com.ibm.mq.csqzau.doc/ts21240_.htm

请求响应头各个字段的含义的讲解
http://publib.boulder.ibm.com/infocenter/wmqv7/v7r0/topic/com.ibm.mq.csqzau.doc/ts21250_.htm


响应错误处理
http://publib.boulder.ibm.com/infocenter/wmqv7/v7r0/topic/com.ibm.mq.csqzau.doc/ts21340_.htm

 

 

The HTTP DELETE operation gets a message from a WebSphere® MQ queue, or retrieves a publication from a topic. The message is removed from the queue. If the publication is retained, it is not removed. A response message is sent back to the client including information about the message.

Syntax

Read syntax diagramSkip visual syntax diagram
Request

>>-+-DELETE-+-- --| Path |-- --HTTP version--CRLF--------------->
   '-GET----'                                       

   .-CRLF---------------.  .-CRLF---------------.   
   V                    |  V                    |   
>----+----------------+-+----+----------------+-+--------------->
     '-general-header-'      '-request-header-'     

   .-CRLF----------------------------.   
   V                                 |   
>----+-----------------------------+-+-------------------------><
     '-| Entity-header (Request) |-'     

Path

|--/--contextRoot--/-------------------------------------------->

>--msg/--+-queue/--queueName--+-------------+-+--/--------------|
         |                    '-@--qMgrName-' |      
         '-topic/--topicName------------------'      

entity-header (Request)

|--+----------------------------------------------+-------------|
   +-standard entity-header-- --entity-value------+   
   +-x-msg-correlId - --correlation ID------------+   
   +-x-msg-msgId - --message ID-------------------+   
   +-x-msg-range-- --range------------------------+   
   +-x-msg-require-headers-- --entity header name-+   
   '-x-msg-wait - --wait time---------------------'   

Note:
  1. If a question mark (?) is used it must be substituted with %3f. For example, orange?topic should be specified as orange%3ftopic.
  2. @qMgrName is only valid on an HTTP POST
Read syntax diagramSkip visual syntax diagram
Response

>>-HTTP version-- --HTTP Status-Code-- --HTTP Reason-Phrase--CRLF-->

   .-CRLF---------------.  .-CRLF----------------.   
   V                    |  V                     |   
>----+----------------+-+----+-----------------+-+-------------->
     '-general-header-'      '-response-header-'     

   .-CRLF-----------------------------.                      
   V                                  |                      
>----+------------------------------+-+--+---------------+-----><
     '-| Entity-header (Response) |-'    '-CRLF--Message-'   

entity-header (Response)

|--+-----------------------------------------+------------------|
   +-standard entity-header-- --entity-value-+   
   +-x-msg-class-- --message type------------+   
   +-x-msg-correlId-- --correlation ID-------+   
   +-x-msg-encoding-- --encoding type--------+   
   +-x-msg-expiry-- --duration---------------+   
   +-x-msg-format-- --message format---------+   
   +-x-msg-msgId-- --message ID--------------+   
   +-x-msg-persistence-- --persistence-------+   
   +-x-msg-priority-- --priority class-------+   
   +-x-msg-replyTo-- --reply-to queue--------+   
   +-x-msg-timestamp-- --HTTP-date-----------+   
   '-x-msg-usr-- --user properties-----------'   

Request parameters

Path
See URI Format.
HTTP version
HTTP version; for example, HTTP/1.1
general-header
See HTTP/1.1 - 4.5 General Header Fields.
request-header
See HTTP/1.1 - 5.3 Request Header Fields. The Host field is mandatory on an HTTP/1.1 request. It is often automatically inserted by the tool you use to create a client request.
entity-header (Request)
See HTTP/1.1 - 7.1 Entity Header Fields. One of the entity headers listed in the Request syntax diagram.

Response parameters

Path
See URI Format.
HTTP version
HTTP version; for example, HTTP/1.1
general-header
See HTTP/1.1 - 4.5 General Header Fields.
response-header
See HTTP/1.1 - 6.2 Response Header Fields.
entity-header (Response)
See HTTP/1.1 - 7.1 Entity Header Fields. One of the entity or response headers listed in the Response syntax diagram.The Content–Length is always present in a response. It is set to zero if there is no message body.
Message
Message body.

Description

If the HTTP DELETE request is successful, the response message contains the data retrieved from the WebSphere MQ queue. The number of bytes in the body of the message is returned in the HTTP Content-Length header. The status code for the HTTP response is set to 200 OK. If x-msg-range is specified as 0, or 0-0, then the status code of the HTTP response is 204 No Content.

If the HTTP DELETE request is unsuccessful, the response includes a WebSphere MQ bridge for HTTP error message and an HTTP status code.

HTTP DELETE example

HTTP DELETE gets a message from a queue and deletes the message, or retrieves and deletes a publication. The HTTPDELETE Java sample is an example an HTTP DELETE request reading a message from a queue. Instead of using Java, you could create an HTTP DELETE request using a browser form, or an AJAX toolkit instead.

Figure 1 is an HTTP request to delete the next message on queue called myQueue. In response, the message body is returned to the client. In WebSphere MQ terms, HTTP DELETE is a destructive get.

The request contains the HTTP request header x-msg-wait, which instructs WebSphere MQ bridge for HTTP how long to wait for a message to arrive on the queue. The request also contains the x-msg-require-headers request header, which specifies that the client is to receive the message correlation ID in the response.

Figure 1. Example of an HTTP DELETE request
DELETE /msg/queue/myQueue/ HTTP/1.1
Host: www.example.org
x-msg-wait: 10
x-msg-require-headers: correlID

Figure 2, is the response returned to the client. The correlation ID is returned to the client, as requested in x-msg-require-headers of the request.

Figure 2. Example of an HTTP DELETE response
HTTP/1.1 200 OK
Date: Wed, 2 Jan 2007 22:38:34 GMT
Server: Apache-Coyote/1.1 WMQ-HTTP/1.1 JEE-Bridge/1.1
Content-Length: 50
Content-Type: text/plain; charset=utf-8
x-msg-correlId: 1234567890

Here's my message body that will appear on the queue.

 

 

HTTP DELETE订阅主题信息

package com.etrip.mqttv3.http;
/**
 * This sample shows how to delete a message. It has slightly enhanced function
 * of the amqsget command in that it will print out the timestamp, expiry and
 * persistence of the messages. The program continues until the queue is empty
 * or a request fails. This program can potentially take in three parameters:
 * <queueName>
 * <host:port> <context-root (the MQ Bridge for HTTP's context-root)>
 * defaults are: SYSTEM.DEFAULT.LOCAL.QUEUE localhost:8080 mq 
 * 
 * If there are any exceptions thrown from this program or errors returned from the server then they are 
 * printed to standard output as-is.
 *   
 * No more messages
 * HTTP DELETE Sample end
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
 * 
 * 采用HTTP DELETE方式的订阅相关的MQTT的主题的信息
 * 
 * @author longgangbai
 * 
 * 
 */
public class HTTPDELETE
{
   private static final String DEFAULT_HOST = "localhost";
   private static final String DEFAULT_PORT = "8080";
   private static final String DEFAULT_QUEUE = "SYSTEM.DEFAULT.LOCAL.QUEUE";

   private static final String DEFAULT_CONTEXT_ROOT = "mq";

   public static String newline = System.getProperty("line.separator");

   private static final String MESSAGE_BOUNDARY = "_________________________________________________________________________________________";

   // the maximum length of the message that we want to print to the screen
   private static final int MAX_OUTPUT_MESSAGE_SIZE = 256;


   private static int OK_RC = 200;

   
   /**
    * 构建订阅主题队列路径
    * 
    * @param host
    * @param port
    * @param context
    * @param queueName
    */
	private static String getPublishQueueURL(String host, String port,
			String context, String queueName) {
		StringBuffer urlString =new StringBuffer("http://");
		   if(StringUtils.isEmtry(host)){
			   host=DEFAULT_HOST;
		   }
		   
		   if(StringUtils.isEmtry(port)){
			   port=DEFAULT_PORT;
		   }
		   urlString.append(host).append(":").append(port);
		   if(StringUtils.isEmtry(context)){
			   context=DEFAULT_CONTEXT_ROOT;
		   }
		   urlString.append("/");
		   urlString.append(context);
		   urlString.append("/msg/queue/");
		   if(StringUtils.isEmtry(queueName)){
			   queueName=DEFAULT_QUEUE;
		   }
		   urlString.append(queueName);
		   System.out.println("urlString="+urlString);
		   return urlString.toString();
	}

	
	/**
	 *  通过HTTP POST 订阅主题的具体实现
	 * @param host
	 * @param port
	 * @param context
	 * @param queueName
	 * @return
	 * @throws MalformedURLException
	 */
	   public static boolean subTopic(String host,String port,String context,String queueName ){
		      String publishURL=getPublishQueueURL(host, port, context, queueName);
		      URL url=null;
		      HttpURLConnection connection=null;
				try {
					  url = new URL(publishURL);
					  connection= (HttpURLConnection) url.openConnection();
				      /* Build the headers */
				      // the verb first.
				      connection.setRequestMethod("DELETE");
		
				      // write out what headers we want back
				      // the header names are case-sensitive
				      connection.setRequestProperty("x-msg-require-headers",
				            "timestamp, expiry, persistence");
		
				      // Now actually send the request message. There is no content as this is a
				      // DELETE
				      connection.connect();
				      String formattedMessage = null;
				      // check the response for errors
				      int responseCode = connection.getResponseCode();
					if (responseCode == OK_RC)
				      {
				         // Get the headers first
				         String timestamp = connection.getHeaderField("x-msg-timestamp");
				         String expiry = connection.getHeaderField("x-msg-expiry");
				         String persistence = connection.getHeaderField("x-msg-persistence");
				         // now get the message data
				         BufferedReader reader = new BufferedReader(new InputStreamReader(
				               connection.getInputStream()));
				         String line = null;
				         StringBuffer messageBuffer = new StringBuffer();
				         while ((line = reader.readLine()) != null)
				         {
				            messageBuffer.append(line);
				         }
				         String messageBody = messageBuffer.toString();
						
				         
				         formattedMessage = MESSAGE_BOUNDARY + newline;
				         // Which is greater the max output message size or the message length?
				         int messageSizeToPrint = messageBody.length() > MAX_OUTPUT_MESSAGE_SIZE ? MAX_OUTPUT_MESSAGE_SIZE
				               : messageBody.length();
				         formattedMessage += messageBody.substring(0, messageSizeToPrint)
				               + newline;
				         formattedMessage += "timestamp   = " + timestamp + newline;
				         formattedMessage += "expiry      = " + expiry + newline;
				         formattedMessage += "persistence = " + persistence + newline;
				         
				         System.out.println("formattedMessage "+formattedMessage);
				      }else{
				    	  String responseMessage =connection.getResponseMessage();
				    	  System.out.println("responsere sponseCode "+responseCode+" response request ="+responseMessage);
				      }
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					connection.disconnect();
				}
		   return false;
	   }

        public static void main(String[] args) {
     	   HTTPDELETE.subTopic("192.168.208.46", "8080", "mq", "java_lover");
		}

}

 

 

分享到:
评论

相关推荐

    毕设&课程作业_智能家居管理系统服务端.zip

    比如,可能使用MQTT协议进行设备间的消息传递,或者使用Zigbee、Wi-Fi等无线通信技术进行设备连接。 6. **云平台集成**:为了实现大规模的数据处理和弹性扩展,系统可能与云服务平台如AWS、Azure或阿里云等集成,...

    基于CST软件的三维超材料能带计算及其应用

    内容概要:本文介绍了如何利用CST软件进行三维超材料的能带计算。首先概述了三维超材料的独特性质及其广泛应用前景,接着简要介绍了CST软件的功能特点。随后详细阐述了能带计算的具体步骤,包括模型建立、材料参数设置、网格划分与求解设置以及最终的计算与结果分析。最后给出了一段Python代码示例,展示了如何处理CST输出的数据并绘制能带图。文章强调了计算机模拟技术对于深入了解超材料电子结构和物理性质的重要性。 适合人群:从事材料科学研究的专业人士,尤其是对三维超材料和电磁场模拟感兴趣的科研工作者和技术人员。 使用场景及目标:适用于希望借助CST软件开展三维超材料能带计算的研究项目,旨在提高对超材料的理解,推动相关领域的技术创新和发展。 其他说明:文中提供的Python代码仅为示例,在实际操作时可根据具体情况进行调整优化。同时,掌握CST软件的基本操作和电磁理论基础知识有助于更好地理解和应用本文内容。

    基于FPGA的永磁同步伺服系统设计:电流环及矢量控制实现

    内容概要:本文详细介绍了基于FPGA的永磁同步伺服系统的矢量控制设计,涵盖了从电流环到速度环的关键模块实现。具体包括Clarke和Park变换、PI调节器、AD7606采样、正交编码器反馈以及SVPWM生成等部分。文中提供了详细的Verilog代码片段,展示了各个模块的具体实现方法和技术细节。特别强调了定点数处理、时序设计和跨时钟域处理等方面的技术挑战及其解决方案。 适合人群:具备一定FPGA开发经验和电机控制基础知识的研发人员。 使用场景及目标:适用于希望深入了解FPGA在电机控制应用中的具体实现方式,特别是矢量控制和电流环设计的专业人士。目标是掌握FPGA平台下高效、低延迟的电机控制系统设计方法。 阅读建议:由于涉及大量具体的Verilog代码和硬件设计细节,建议读者在阅读过程中结合实际项目进行实验和调试,以便更好地理解和掌握相关技术。

    飞机大战:从运动方程到战斗系统的全链路设计解析

    经典飞机大战游戏是理解实时交互系统设计的绝佳载体。本文将深入剖析现代空战游戏的核心模块,涵盖刚体运动学、弹道轨迹优化、碰撞检测算法等关键技术,揭示二维游戏背后复杂的三维数学建模过程。

    scratch少儿编程逻辑思维游戏源码-冰塔.zip

    scratch少儿编程逻辑思维游戏源码-冰塔.zip

    scratch少儿编程逻辑思维游戏源码-弹跳(4).zip

    scratch少儿编程逻辑思维游戏源码-弹跳(4).zip

    COMSOL焊接热源模型解析:双椭球、高斯旋转体与柱状体热源的应用与优化

    内容概要:本文详细介绍了COMSOL软件中三种常见的焊接热源模型——双椭球热源、高斯旋转体热源和柱状体热源。双椭球热源适用于模拟移动热源(如激光焊、电弧焊),通过调整轴向系数a1和a2来控制热流分布;高斯旋转体热源适合小范围焊接,采用三维高斯函数描述热流密度;柱状体热源则用于深熔焊场景,特点是计算速度快。文中还提供了每种模型的具体代码实现,并强调了调试时需要注意的关键点,如时间步长、网格加密等。此外,作者分享了一些实用技巧,如将热源参数设置为全局变量并利用参数扫描功能提高调试效率。 适合人群:从事焊接工艺仿真、材料加工领域的研究人员和技术人员,以及对COMSOL建模感兴趣的工程技术人员。 使用场景及目标:帮助用户选择合适的热源模型进行焊接模拟,确保模拟结果的准确性;提供具体的代码实现和调试方法,使用户能够快速掌握并应用于实际项目中。 其他说明:文中提到的热源模型不仅限于理论介绍,还包括实际操作中的注意事项和优化建议,有助于提升模拟效果和工作效率。

    基于RBF神经网络的PID控制器在PMSM转速环中的Simulink模型设计与性能分析

    内容概要:本文介绍了将基于RBF神经网络的PID控制器应用于永磁同步电机(PMSM)转速环控制的方法及其性能优势。传统的PID控制器在面对非线性和时变系统时存在参数整定困难的问题,而引入RBF神经网络可以实现实时在线调参,提高系统的灵活性和鲁棒性。文中详细描述了Simulink模型的设计,特别是Matlab s-function模块中RBF神经网络的具体实现,包括高斯函数激活和带惯性的权值更新机制。实验结果显示,在转速突变情况下,改进后的控制器能够迅速稳定系统,超调量控制在2%以内,调节时间较传统方法缩短约40%,并且在负载变化时表现出色,无需重新整定参数。 适合人群:从事电机控制系统研究和开发的技术人员,尤其是对PID控制器优化感兴趣的工程师。 使用场景及目标:适用于需要提升PMSM转速环控制精度和响应速度的应用场合,如工业自动化设备、机器人等领域。目标是通过引入智能算法解决传统PID控制器参数整定难题,提高系统性能。 阅读建议:关注RBF神经网络与PID控制器结合的具体实现细节,特别是在Matlab s-function模块中的编码技巧以及参数调整策略。同时,注意学习率的选择和动量项的作用,这对于实际应用至关重要。

    scratch少儿编程逻辑思维游戏源码-GTA 6.zip

    scratch少儿编程逻辑思维游戏源码-GTA 6.zip

    scratch少儿编程逻辑思维游戏源码-仓鼠跑酷.zip

    scratch少儿编程逻辑思维游戏源码-仓鼠跑酷.zip

    scratch少儿编程逻辑思维游戏源码-超级麦克世界.zip

    scratch少儿编程逻辑思维游戏源码-超级麦克世界.zip

    scratch少儿编程逻辑思维游戏源码-400年.zip

    scratch少儿编程逻辑思维游戏源码-400年.zip

    少儿编程scratch项目源代码文件案例素材-气球足球.zip

    少儿编程scratch项目源代码文件案例素材-气球足球.zip

    少儿编程scratch项目源代码文件案例素材-沙漠迷城.zip

    少儿编程scratch项目源代码文件案例素材-沙漠迷城.zip

    scratch少儿编程逻辑思维游戏源码-比谁高.zip

    scratch少儿编程逻辑思维游戏源码-比谁高.zip

    少儿编程scratch项目源代码文件案例素材-乾坤大挪移.zip

    少儿编程scratch项目源代码文件案例素材-乾坤大挪移.zip

    scratch少儿编程逻辑思维游戏源码-菜鸟跳跃.zip

    scratch少儿编程逻辑思维游戏源码-菜鸟跳跃.zip

    【C++编程语言】核心特性详解、学习笔记

    内容概要:本文档详细介绍了C++语言的基础知识、高级特性及其应用。首先,文档回顾了C++对C语言的扩展,包括面向对象编程的支持、增强的语法特性(如命名空间、引用、常量处理等)。接着,深入探讨了类和对象的使用,涵盖构造函数、析构函数、拷贝构造函数、深浅拷贝等重要概念。文档还讲解了单例模式的设计与实现、C++面向对象模型的核心要素(如this指针、静态成员、友元函数)、继承与派生的关系及其实现细节、多态性的原理与应用。此外,文档详细介绍了C++的模板机制、类型转换、异常处理机制、输入输出流操作、STL(标准模板库)的容器和算法等内容。每个部分都通过具体的代码示例和解释,帮助读者理解和掌握C++的关键特性和最佳实践。 适合人群:具备一定编程基础,尤其是熟悉C语言的开发者;希望深入了解C++语言特性和面向对象编程思想的程序员;从事C++开发工作的工程师和技术爱好者。 使用场景及目标:①掌握C++语言的核心概念和高级特性;②理解并能够应用面向对象编程的基本原则和模式;③学习如何使用STL容器和算法优化代码性能;④提升C++程序的健壮性和可维护性,特别是在处理复杂数据结构和算法时;⑤掌握异常处理和类型转换的最佳实践,确保程序的稳定性和安全性。 其他说明:本文档不仅提供了理论知识,还结合了大量实例代码,便于读者边学边练。对于每一个知识点,文档都力求做到详尽解释,确保读者能够透彻理解并灵活运用。文档内容全面覆盖了C++编程的各个方面,从基础语法到高级特性,适合不同层次的学习者逐步深入学习。

    电气铁路柔性过分相技术:60°换相波形稳定分析及其实现方法

    内容概要:本文详细探讨了电气铁路柔性过分相技术中的60°换相波形稳定问题。首先介绍了柔性过分相的基本概念及其重要性,接着重点讨论了60°换相波形稳定对系统运行效率和稳定性的影响。文中还阐述了实现换相波形稳定的多种方法,如先进控制算法、实时监测设备以及滤波器的应用。最后,通过引用相关文献和实例,进一步验证了换相波形稳定的重要性和可行性。 适合人群:从事电气铁路系统设计、维护的技术人员,以及对该领域感兴趣的科研人员。 使用场景及目标:适用于希望深入了解电气铁路柔性过分相技术的专业人士,旨在帮助他们掌握60°换相波形稳定的相关理论和技术手段,提升系统性能。 其他说明:文章结合了理论分析与实际案例,提供了丰富的参考资料,有助于读者全面理解并应用于实际工作中。

    scratch少儿编程逻辑思维游戏源码-发动.zip

    scratch少儿编程逻辑思维游戏源码-发动.zip

Global site tag (gtag.js) - Google Analytics