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
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---------------------'
- If a question mark (?) is used it must be substituted with %3f. For example, orange?topic should be specified as orange%3ftopic.
- @qMgrName is only valid on an HTTP POST
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
PathSee URI Format.HTTP versionHTTP version; for example, HTTP/1.1general-headerSee HTTP/1.1 - 4.5 General Header Fields.request-headerSee 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
PathSee URI Format.HTTP versionHTTP version; for example, HTTP/1.1general-headerSee HTTP/1.1 - 4.5 General Header Fields.response-headerSee 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.MessageMessage 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, or0-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-headersrequest header, which specifies that the client is to receive the message correlation ID in the response.
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.
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");
- }
- }
MQTT的学习研究(八)基于HTTP DELETE MQTT 订阅消息服务端使用的更多相关文章
- .net平台 基于 XMPP协议的即时消息服务端简单实现
.net平台 基于 XMPP协议的即时消息服务端简单实现 昨天抽空学习了一下XMPP,在网上找了好久,中文的资料太少了所以做这个简单的例子,今天才完成.公司也正在准备开发基于XMPP协议的即时通讯工具 ...
- MQTT的学习研究(十四) MQTT moquette 的 Callback API 消息发布订阅的实现
在moquette-mqtt中提供了回调callback模式的发布和订阅但是在订阅之后没有发现有消息接收的方法,参看moquette-mqtt中Block,Future式的发布订阅基础是callbac ...
- MQTT的学习研究(十二) MQTT moquette 的 Future API 消息发布订阅的实现
MQTT moquette 的Server发布主题 package com.etrip.mqtt.future; import java.net.URISyntaxException; import ...
- MQTT的学习研究(十六) MQTT的Mosquitto的window安装部署
在mqtt的官方网站,有许多mqtt,其中:MosquittoAn Open Source MQTT server with C, C++, Python and Javascript clients ...
- MQTT的学习研究(十五) MQTT 和android整合文章
详细参考: How to Implement Push Notifications for Android http://tokudu.com/2010/how-to-implement-push- ...
- 基于开源SuperSocket实现客户端和服务端通信项目实战
一.课程介绍 本期带给大家分享的是基于SuperSocket的项目实战,阿笨在实际工作中遇到的真实业务场景,请跟随阿笨的视角去如何实现打通B/S与C/S网络通讯,如果您对本期的<基于开源Supe ...
- 基于TCP协议套接字,服务端实现接收客户端的连接并发
基于TCP协议套接字,服务端实现接收客户端的连接并发 服务端 import socket from multiprocessing import Process server=socket.socke ...
- 写一个基于TCP协议套接字,服务端实现接收客户端的连接并发
''' 写一个基于TCP协议套接字,服务端实现接收客户端的连接并发 ''' client import socket import time client = socket.socket() clie ...
- MQTT的学习研究(十一) IBM MQTT 简单发布订阅实例
package com.etrip.push; import com.ibm.mqtt.MqttAdvancedCallback; import com.ibm.mqtt.MqttClient; im ...
随机推荐
- 分散/聚集IO(scatter/gather)及iovec结构体
分散/聚集 I/O是一种可以在单次系统调用中对多个缓冲区输入输出的方法,可以把多个缓冲区的数据写到单个数据流,也可以把单个数据流读到多个缓冲区中.其命名的原因在于数据会被分散到指定缓冲区向量,或者从指 ...
- 机器学习:Mean Shift聚类算法
本文由ChardLau原创,转载请添加原文链接https://www.chardlau.com/mean-shift/ 今天的文章介绍如何利用Mean Shift算法的基本形式对数据进行聚类操作.而有 ...
- C#操作word类文件
最近频繁操作Word文档,写了很多word的操作代码及方法,虽然已经有很多关于word的操作类了,自己还是进行了一下整合: 1.通过模板创建新文件 2.在书签处插入值 3.插入表格 4.合并单元格 5 ...
- log4j日志pattern配置
c category的名称,可使用{n}限制输出的精度.例如:logger名为"a.b.c",%c{2}将输出"b.c". C 产生log事件的java完全限定 ...
- Dropwizard与Spring Boot比较
在这篇文章中我们将讨论的Java轻量级框架Dropwizard和Spring Boot的相似性和差异. 首先,这是一个选择自由和速度需要,无论你在Dropwizard和Spring Boot选择哪个, ...
- js学习笔记16----父节点的操作
1.元素.parentNode : 只读属性,获取当前元素的父节点. 2.元素.offsetParent : 只读属性,获取离当前元素最近的一个有定位属性(position为relative或者abs ...
- 统一建模语言 UML
目录 统一建模语言 UML UML定义了5类10种模型图 一用例图用于建立需求模型 二静态图主要描述系统的静态表示和关系包括类图包图对象图 三行为图描述系统动态模型和对象组成的交换关系包括状态图和活动 ...
- CentOS下rpm指令和yum指令详解
centos的软件安装大致可以分为两种类型: [centos]rpm文件安装,使用rpm指令 类似[ubuntu]deb文件安装,使用dpkg指令 [centos]yum安装 类似[ubuntu]ap ...
- chrome调试技巧--持续更新
1.开始调试:右键审查元素 2.按钮功能: 调出控制台: 切换开发环境全屏还是嵌入: 清空当前显示: 将压缩 js 文件格式化缩进规整的文件: 3.常用页面功能: 查看.编辑(双击)HTML: 查看选 ...
- PL/SQL中decode函数简介
今天看别人的SQL时看这里面还有decode()函数,以前从来没接触到,上网查了一下,还挺好用的一个函数,写下来希望对朋友们有帮助哈! decode()函数简介: 主要作用:将查询结果翻译成其他值(即 ...