rocketmq学习笔记 二 官方实例<生产者操作>
2016-12-08 22:16
357 查看
本文讲一些简单的producer,同步发消息和异步发消息,具体源码 后面会详细分析
1.同步发消息
/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.rocketmq.example.simple; import com.alibaba.rocketmq.client.exception.MQClientException; import com.alibaba.rocketmq.client.producer.DefaultMQProducer; import com.alibaba.rocketmq.client.producer.SendResult; import com.alibaba.rocketmq.common.message.Message; import com.alibaba.rocketmq.remoting.common.RemotingHelper; public class Producer { public static void main(String[] args) throws MQClientException, InterruptedException { DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName"); producer.start(); for (int i = 0; i < 10000000; i++) try { { Message msg = new Message("TopicTest",// topic "TagA",// tag "OrderID188",// key ("Hello MetaQ").getBytes(RemotingHelper.DEFAULT_CHARSET));// body SendResult sendResult = producer.send(msg); System.out.println(sendResult); } } catch (Exception e) { e.printStackTrace(); } producer.shutdown(); } }
2.异步发消息
/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.rocketmq.example.simple; import com.alibaba.rocketmq.client.exception.MQClientException; import com.alibaba.rocketmq.client.producer.DefaultMQProducer; import com.alibaba.rocketmq.client.producer.SendCallback; import com.alibaba.rocketmq.client.producer.SendResult; import com.alibaba.rocketmq.common.message.Message; import com.alibaba.rocketmq.remoting.common.RemotingHelper; import java.io.UnsupportedEncodingException; import com.alibaba.rocketmq.remoting.common.RemotingHelper; public class AsyncProducer { public static void main(String[] args) throws MQClientException, InterruptedException, UnsupportedEncodingException { DefaultMQProducer producer = new DefaultMQProducer("Jodie_Daily_test"); producer.start(); producer.setRetryTimesWhenSendAsyncFailed(0); for (int i = 0; i < 10000000; i++) { try { final int index = i; Message msg = new Message("Jodie_topic_1023",// topic "TagA",// tag "OrderID188",// key ("Hello MetaQ").getBytes(RemotingHelper.DEFAULT_CHARSET));// body producer.send(msg, new SendCallback() { @Override public void onSuccess(SendResult sendResult) { System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId()); } @Override public void onException(Throwable e) { System.out.printf("%-10d Exception %s %n", index, e); e.printStackTrace(); } }); } catch (Exception e) { e.printStackTrace(); } // TimeUnit.MILLISECONDS.sleep(1); } producer.shutdown(); } }
相关文章推荐
- rocketmq学习笔记 二 官方实例<命令行操作>
- rocketmq学习笔记 二 官方实例<消费者操作>
- rocketmq学习笔记 二 官方实例<消息顺序>
- hibernate操作mysql无法实例到库中<mysql SQL Error: 1064, SQLState: 42000>
- C#中List<object>.Clear()方法和实例化new List<object>()操作的结果分析
- <Head First Java>学习笔记--第四章:方法操作实例变量 对象的行为
- rocketmq学习笔记 二 官方实例<广播>
- rocketmq学习笔记 二 官方实例<分布式事务>
- <C语言>结构体的各种定义、初始化、操作代码实例----注释详解
- rocketmq学习笔记 二 官方实例<消息过滤>
- struts 2 标签之<s:tree/>和<s:treenode/>简单实例
- linux服务器配置实例<1>---Linux网络基础
- 大话西游之Office应用实例系列! <6>
- 大话西游之Office应用实例系列! <2.5>
- 大话西游之Office应用实例系列! <1>
- linux服务器配置实例<11>---配置邮件服务器
- linux服务器配置实例<8>---配置NFS文件系统
- linux服务器配置实例<9>---配置samba服务
- linux服务器配置实例<5>---配置Linux防火墙