落叶 通过本文主要向大家介绍了http://mq.nisco.cn,http://mq.qq.com,java mq消息队列,java mq,java中mq是什么等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
1. 准备环境
在工程 POM 文件添加 HTTP Java 客户端的依赖。
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-client</artifactId> <version>9.3.4.RC1</version> </dependency> <dependency> <groupId>com.aliyun.openservices</groupId> <artifactId>ons-client</artifactId> <version>1.1.11</version> </dependency>
</div>
2. 运行代码配置(user.properties)
您需要设置配置文件(user.properties)的相关内容,具体请参考申请 MQ 资源 。
#您在控制台创建的Topic Topic=xxx #公测url URL=http://publictest-rest.ons.aliyun.com #阿里云身份验证码 Ak=xxx #阿里云身份验证密钥 Sk=xxx #MQ控制台创建的Producer ID ProducerID=xxx #MQ控制台创建的Consumer ID ConsumerID=xxx
</div>
说明:URL 中的 Key,Tag以及 POST Content-Type 没有任何的限制,只要确保Key 和 Tag 相同唯一即可,可以放在 user.properties 里面。
3. HTTP 发送消息示例代码
您可以按以下说明设置相应参数并测试 HTTP 消息发送功能。 package com.aliyun.openservice.ons.http.demo; import java.nio.charset.Charset; import java.util.Date; import java.util.Properties; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentProvider; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.util.StringContentProvider; import com.aliyun.openservices.ons.api.impl.authority.AuthUtil; public class HttpProducer { public static String SIGNATURE="Signature"; public static String NUM="num"; public static String CONSUMERID="ConsumerID"; public static String PRODUCERID="ProducerID"; public static String TIMEOUT="timeout"; public static String TOPIC="Topic"; public static String AK="AccessKey"; public static String BODY="body"; public static String MSGHANDLE="msgHandle"; public static String TIME="time"; public static void main(String[] args) throws Exception { HttpClient httpClient=new HttpClient(); httpClient.setMaxConnectionsPerDestination(1); httpClient.start(); Properties properties=new Properties(); properties.load(HttpProducer.class.getClassLoader().getResourceAsStream("user.properties")); String topic=properties.getProperty("Topic"); //请在user.properties配置您的Topic String url=properties.getProperty("URL");//公测集群配置为http://publictest-rest.ons.aliyun.com/ String ak=properties.getProperty("Ak");//请在user.properties配置您的Ak String sk=properties.getProperty("Sk");//请在user.properties配置您的Sk String pid=properties.getProperty("ProducerID");//请在user.properties配置您的Producer ID String date=String.valueOf(new Date().getTime()); String sign=null; String body="hello ons http"; String NEWLINE="\n"; String signString; for (int i = 0; i < 10; i++) { date=String.valueOf(new Date().getTime()); Request req=httpClient.POST(url+"message/?topic="+topic+"&time="+date+"&tag=http"+"&key=http"); ContentProvider content=new StringContentProvider(body); req.content(content); signString=topic+NEWLINE+pid+NEWLINE+MD5.getInstance().getMD5String(body)+NEWLINE+date; System.out.println(signString); sign=AuthUtil.calSignature(signString.getBytes(Charset.forName("UTF-8")), sk); req.header(SIGNATURE, sign); req.header(AK, ak); req.header(PRODUCERID, pid); ContentResponse response; response=req.send(); System.out.println("send msg:"+response.getStatus()+response.getContentAsString()); } } }</div>
4. HTTP接收消息示例代码
请按以下说明设置相应参数并测试 HTTP 消息接收功能。
package com.aliyun.openservice.ons.http.demo; import java.nio.charset.Charset; import java.util.Date; import java.util.List; import java.util.Properties; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentProvider; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.util.StringContentProvider; import org.eclipse.jetty.http.HttpMethod; import com.alibaba.fastjson.JSON; import com.aliyun.openservice.ons.mqtt.demo.MqttProducer; import com.aliyun.openservices.ons.api.impl.authority.AuthUtil; public class HttpConsumer { public static String SIGNATURE="Signature"; public static String NUM="num"; public static String CONSUMERID="ConsumerID"; public static String PRODUCERID="ProducerID"; public static String TIMEOUT="timeout"; public static String TOPIC="Topic"; public static String AK="AccessKey"; public static String BODY="body"; public static String MSGHANDLE="msgHandle"; public static String TIME="time"; public static void main(String[] args) throws Exception { HttpClient httpClient=new HttpClient(); httpClient.setMaxConnectionsPerDestination(1); httpClient.start(); Properties properties=new Properties(); properties.load(HttpConsumer.class.getClassLoader().getResourceAsStream("user.properties")); String topic=properties.getProperty("Topic"); //请在user.properties配置您的topic String url=properties.getProperty("URL");//公测集群配置为http://publictest-rest.ons.aliyun.com/ String ak=properties.getProperty("Ak");//请在user.properties配置您的Ak String sk=properties.getProperty("Sk");//请在user.properties配置您的Sk String cid=properties.getProperty("ConsumerID");//请在user.properties配置您的Consumer ID String date=String.valueOf(new Date().getTime()); String sign=null; String NEWLINE="\n"; String signString; System.out.println(NEWLINE+NEWLINE); while (true) { try { date=String.valueOf(new Date().getTime()); Request req=httpClient.POST(url+"message/?topic="+topic+"&time="+date+"&num="+32); req.method(HttpMethod.GET); ContentResponse response; signString=topic+NEWLINE+cid+NEWLINE+date; sign=AuthUtil.calSignature(signString.getBytes(Charset.forName("UTF-8")), sk); req.header(SIGNATURE, sign); req.header(AK, ak); req.header(CONSUMERID, cid); long start=System.currentTimeMillis(); response=req.send(); System.out.println("get cost:"+(System.currentTimeMillis()-start)/1000 +" "+response.getStatus()+" "+response.getContentAsString()); List<SimpleMessage> list = null; if (response.getContentAsString()!=null&&!response.getContentAsString().isEmpty()) { list=JSON.parseArray(response.getContentAsString(), SimpleMessage.class); } if (list==null||list.size()==0) { Thread.sleep(100); continue; } System.out.println("size is :"+list.size()); for (SimpleMessage simpleMessage : list) { date=String.valueOf(new Date().getTime()); System.out.println("receive msg:"+simpleMessage.getBody()+" born time "+simpleMessage.getBornTime()); req=httpClient.POST(url+"message/?msgHandle="+simpleMessage.getMsgHandle()+"&topic="+topic+"&time="+date); req.method(HttpMethod.DELETE); signString=topic+NEWLINE+cid+NEWLINE+simpleMessage.getMsgHandle()+NEWLINE+date; sign=AuthUtil.calSignature(signString.getBytes(Charset.forName("UTF-8")), sk); req.header(SIGNATURE, sign); req.header(AK, ak); req.header(CONSUMERID, cid);