MQTT的学习研究(八)基于HTTP DELETE MQTT 订阅消息服务端使用
来源:互联网 发布:javascript 线程等待 编辑:程序博客网 时间:2024/06/04 17:54
参看官方文档
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
Response parameters
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-headersrequest header, which specifies that the client is to receive the message correlation ID in the response.
DELETE /msg/queue/myQueue/ HTTP/1.1Host: www.example.orgx-msg-wait: 10x-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 OKDate: Wed, 2 Jan 2007 22:38:34 GMTServer: Apache-Coyote/1.1 WMQ-HTTP/1.1 JEE-Bridge/1.1Content-Length: 50Content-Type: text/plain; charset=utf-8x-msg-correlId: 1234567890Here'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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 订阅消息服务端使用
- MQTT的学习研究(八)基于HTTP DELETE MQTT 订阅消息服务端使用
- MQTT的学习研究(七)基于HTTP POST MQTT 发布消息服务端使用
- MQTT的学习研究(九)基于HTTP GET MQTT 抓取消息服务端使用
- MQTT的学习研究(七)基于HTTP POST MQTT 发布消息服务端使用
- MQTT的学习研究(九)基于HTTP GET MQTT 抓取消息服务端使用
- MQTT的学习研究(六) MQTT moquette 的 Blocking API 订阅消息客户端使用
- MQTT的学习研究(六) MQTT moquette 的 Blocking API 订阅消息客户端使用
- MQTT的学习研究(6)MQTT 订阅消息客户端使用
- MQTT的学习研究(五) MQTT moquette 的 Blocking API 发布消息服务端使用
- MQTT的学习研究(5) MQTT moquette 的 Blocking API 发布消息服务端使用
- MQTT的学习研究(十二) MQTT moquette 的 Future API 消息发布订阅的实现
- MQTT的学习研究(十四) MQTT moquette 的 Callback API 消息发布订阅的实现
- MQTT的学习研究(十二) MQTT moquette 的 Future API 消息发布订阅的实现
- MQTT的学习研究(十四) MQTT moquette 的 Callback API 消息发布订阅的实现
- MQTT的学习研究(四)moquette-mqtt 的使用之mqtt Blocking API客户端订阅并接收主题信息
- MQTT的学习研究(4)moquette-mqtt 的使用之mqtt Blocking API客户端订阅并接收主题信息
- MQTT的学习研究(二)moquette-mqtt 的使用之mqtt broker的启动
- 有没有大神级存在啊, 帮忙搞定一下
- boost socket通讯实例
- 盖洛普Q12
- Linux 中文乱码解决!
- wp8开发基础(2)-webbrowser之js通讯交互(ObjectForScripting,ScriptNotify)
- MQTT的学习研究(八)基于HTTP DELETE MQTT 订阅消息服务端使用
- 半边数据结构
- oracle内存结构
- 如何利用Excel数据有效性制作多级联动下拉列表
- MAC上搭建Windows Phone 8 开发环境——VMware Fusion下Win8 “无法安装Hyper-V, 某个虚拟机监控程序正在运行”问题解决的办法
- Mac OS下文件夹(带linux部分)
- 黑马程序员之7k面试题之银行业务调度系统
- setup time & hold time
- LeetCode 之 Letter Combinations of a Phone Number