MQTT的学习研究(七)基于HTTP POST MQTT 发布消息服务端使用
参阅官方文档
http://publib.boulder.ibm.com/infocenter/wmqv7/v7r0/topic/com.ibm.mq.csqzau.doc/ts21220_.htm
HTTP POST puts a message to a queue, or a publication to a topic. The HTTPPOST Java sample is an example an HTTP POST request of a message to a queue. Instead of using Java, you could create an HTTPPOST request using a browser form, or an AJAX toolkit instead.
Figure 1 shows an HTTP request to put a message on a queue called myQueue. This request contains the HTTP header x-msg-correlId to set the correlation ID of the WebSphere MQ message.
POST /msg/queue/myQueue/ HTTP/1.1
Host: www.example.org
Content-Type: text/plain
x-msg-correlID: 1234567890
Content-Length: 50 Here's my message body that will appear on the queue.
Figure 2 shows the response sent back to the client. There is no response content.
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: 0
请求的协议格式和请求的响应格式
The HTTP POST operation puts a message on a WebSphere® MQ queue, or publishes a message to a topic.
Syntax
Request >>-POST-- --| Path |-- --HTTP version--CRLF---------------------> .-CRLF---------------. .-CRLF---------------.
V | V |
>----+----------------+-+----+----------------+-+--------------->
'-general-header-' '-request-header-' .-CRLF----------------------------. .-CRLF----.
V | V |
>----+-----------------------------+-+--CRLF----Message-+------><
'-| entity header (Request) |-' Path |--/--contextRoot--/--------------------------------------------> >--msg/--+-queue/--queueName--+-------------+-+--/--------------|
| '-@--qMgrName-' |
'-topic/--topicName------------------' entity-header (Request) |--+----------------------------------------------+-------------|
+-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-require-headers-- --entity header name-+
'-x-msg-usr-- --user properties----------------'
- 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) |-' 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-----------'
- package com.etrip.mqttv3.http;
- /**
- * This sample shows how to post a message. It has the same behaviour as the
- * amqsput command in that it will read in lines from the command line and put
- * them to the queue. It will put non-persistent String messages on to the queue
- * with UNLIMITED expiry and LOW (0) priority. The program is terminated by
- * either EOF being put into the entry line (^Z on windows) or a blank line.
- * usage: java HTTPPOST <Queue (default=SYSTEM.DEFAULT.LOCAL.QUEUE)> <host:port
- * (default localhost:8080> <context-root (the MQ Bridge for HTTP's
- * context-root)>
- */
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- /**
- *
- * 采用HTTP POST发布相关的消息
- * The HTTP POST operation puts a message on a WebSphere® MQ queue, or publishes
- * a message to a topic.
- *
- * 发布消息到主题或者队列的路径:
- *
- *
- *
- *
- *
- * @author longgangbai
- */
- public class HTTPPOST
- {
- 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";
- private static final String CRLF = "\r\n";
- public static int MALFORMED_URL_EXCEPTION_RC = -1;
- public static int END_IOEXCEPTION_RC = -2;
- /**
- * 构建发布主题队列路径
- *
- * @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();
- }
- /**
- *
- * @param host
- * @param port
- * @param context
- * @param queueName
- * @param message
- * @return
- * @throws MalformedURLException
- */
- public static boolean publishTopic(String host,String port,String context,String queueName,String message ){
- boolean response = true;
- HttpURLConnection connection=null;
- try {
- String publishURL=getPublishQueueURL(host, port, context, queueName);
- URL url=new URL(publishURL);
- connection = (HttpURLConnection) url.openConnection();
- /* Build the headers */
- // the verb first
- connection.setRequestMethod("POST");
- // Content type is a string message
- connection.setRequestProperty("content-type", "text/plain");
- // set the message priority to low
- connection.setRequestProperty("x-msg-priority", "LOW");
- // Ensure we can get the output stream from the connection
- connection.setDoOutput(true);
- OutputStream outputStream = connection.getOutputStream();
- // wrapper the outputstream in a writer
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
- outputStream));
- // Now write the actual content.
- // Make sure the CRLF is there in case some HTTP servers don't understand
- // that it's the end of the message
- writer.write(message + CRLF + CRLF);
- writer.flush();
- // now actually send the message
- connection.connect();
- // check the response for errors
- int responseCode = connection.getResponseCode();
- if (responseCode != 200)
- {
- String responseMessage =connection.getResponseMessage();
- System.out.println("responsere sponseCode "+responseCode+" response request ="+responseMessage);
- System.out.println("responsere context ");
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- connection.getErrorStream()));
- String line = null;
- while ((line = reader.readLine()) != null)
- {
- System.out.println(line);
- }
- connection.disconnect();
- response = false;
- }else{
- //获取相应的消息头信息
- String responseQueueName=connection.getHeaderField("x-msg-replyTo");
- System.out.println("responseQueueName="+responseQueueName);
- System.out.println("response successful context :"+connection.getResponseMessage());
- }
- } catch (MalformedURLException e) {
- response = false;
- e.printStackTrace();
- // TODO: handle exception
- } catch (IOException e) {
- response = false;
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- connection.disconnect();
- }
- return response;
- }
- public static void main(String[] args) {
- HTTPPOST.publishTopic("192.168.208.46", "8080", "mq", "java_lover", "this is a message ");
- }
- }
http://publib.boulder.ibm.com/infocenter/wmqv7/v7r0/topic/com.ibm.mq.csqzau.doc/ts21220_.htm
http://publib.boulder.ibm.com/infocenter/wmqv7/v7r0/topic/com.ibm.mq.csqzau.doc/ts21250_.htm
MQTT的学习研究(七)基于HTTP POST MQTT 发布消息服务端使用的更多相关文章
- 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- ...
- Pomelo:网易开源基于 Node.js 的游戏服务端框架
Pomelo:网易开源基于 Node.js 的游戏服务端框架 https://github.com/NetEase/pomelo/wiki/Home-in-Chinese
- 基于JAX-WS的Web Service服务端/客户端 ;JAX-WS + Spring 开发webservice
一.基于JAX-WS的Web Service服务端/客户端 下面描述的是在main函数中使用JAX-WS的Web Service的方法,不是在web工程里访问,在web工程里访问,参加第二节. JAX ...
- MQTT的学习研究(十一) IBM MQTT 简单发布订阅实例
package com.etrip.push; import com.ibm.mqtt.MqttAdvancedCallback; import com.ibm.mqtt.MqttClient; im ...
- MQTT的学习研究(三)moquette-mqtt 的使用之mqtt服务发布主题信息
接着上一篇的moquette-mqtt 的使用之broker启动之后,我们需要启动moquette-mqtt 的服务端发布消息. 在moquette-mqtt 的mqtt-client中三种方式实现发 ...
- MQTT的学习研究(二)moquette-mqtt 的使用之mqtt broker的启动
在MQTT 官网 (http://mqtt.org/software)中有众多MQTT的实现方式.具体参看官网,Moquette是基于Apache Mina 的模型的一个Java MQTT broke ...
- MQTT的学习研究(十)【转】mosquitto——一个开源的mqtt代理
MQTT(MQ Telemetry Transport),消息队列遥测传输协议,轻量级的发布/订阅协议,适用于一些条件比较苛刻的环境,进行低带宽.不可靠或间歇性的通信.值得一提的是mqtt提供三种不同 ...
随机推荐
- 何为中间语言IL?
一直以来,对于.NET与C#之间的关系我都存在着疑惑,为此,今天专门仔细看了一下以前最容易忽略掉的书本“前言”部分,予以澄清:) 首先,c#的结构和方法论反映了.NET的基础方法论,在很多情况下,c# ...
- Android Activity 传递数据
activity中数据的传递方式有2中,一种是使用putExtra()传递,另外一种是传递Bundle对象,使用putExtras()方法. 方法一 发送数据 putExtra()传送的是键值对,第一 ...
- JVM内存模型 小小结
可以看一下我的另一篇总结 JVM运行时数据区与JVM堆内存模型小结 推荐一篇文章,尚学堂的 Java内存模型深度解读 . 不方便全文转载,就摘录下吧. 以往的认知都是以基本类型.引用类型.常量.方法等 ...
- linux -- Ubuntu Server 安装图形界面
1.连接网络,你一定要确保网络通畅,如果你和我一样使用Wireless,那先找根网线插上,因为下面的安装都要通过网络下载组件的. 2.进入图形界面的命令是startX,敲击后会有安装xinit的提示. ...
- CentOS 7拨号上网(ADSL & PPPoE)
步骤概述: 1.搜寻PPPoE相关软件,本人使用的是rp-pppoe yum search pppoe 2.使用yum安装rp-pppoe yum install rp-pppoe -y 3.开始配置 ...
- [CS]C#操作word
近期在做的项目已经改了好几版,近期这一版用到了word,当然不是直接使用word,而是使用第三方的ActiveX控件:dsoframer.ocx.此控件的使用和其它控件的使用流程没有不论什么差别.接下 ...
- sql字符串的拼接 (字符串和二进制,erlang的mysql驱动)
1> list_to_binary(["select * from aa limit","1",",","97"] ...
- Windows 上 GitHub Desktop 的操作[转]
第1章 上传开源代码至GitHub 1 1.1 git Windows 客户端 1 1.2 注册GitHub账户 2 1.3 登录 2 1.4 创建本地代码仓库 2 1. ...
- c++ 用构造函数
1.c++ 用构造函数创建一个对象时,对象里的属性应该是全新的不存在把之前的属性留下的问题.之所以debug里会存在上一个变量的信息,应该是debug里的一个bug,与程序无光.
- 无法在Word中打开MathType怎么办
MathType是一种数学公式编辑器,通常我们都是与Office文档配合使用,但是如果大家在Word中使用MathType编辑公式时,遇到MathType无法打开的情况,我们应该怎么办?下面我们就针对 ...