转载请注明出处:http://www.cnblogs.com/xiaodf/

  本示例展示了一个RocketMQ producer的简单实现,通过解析文本文件获取输入数据,将数据经过Avro序列化后发送到RocketMQ。

  程序通过stdin.xml配置文件获取主要参数值,stdin.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<operator>
<parameters>
<parameter>
<key>rocketmq.nameserver.list</key>
<value>172.16.8.106:9876</value>
</parameter>
<parameter>
<key>rocketmq.group.id</key>
<value>test006</value>
</parameter>
<parameter>
<key>rocketmq.topic</key>
<value>TopicTest2</value>
</parameter>
<parameter>
<key>rocketmq.tags</key>
<value>*</value>
</parameter>
<parameter>
<key>rocketmq.message.key</key>
<value>OrderID0034</value>
</parameter>
<parameter>
<key>schemaStr</key>
<value>col1:string,col2:double</value>
</parameter>
<parameter>
<key>filePath</key>
<value>/home/test/rocketmq/input.txt</value>
</parameter>
</parameters>
</operator>

  

生产者示例程序如下:

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.scistor.datavision.operator.common.AvroUtils;
import com.scistor.datavision.operator.common.OperatorConfiguration;
import org.apache.avro.Schema;
import org.apache.hive.hcatalog.common.HCatException;
import org.apache.hive.hcatalog.data.schema.HCatSchema; import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; public class RocketProducer { // parameters
private String nameserver;
private String rocketmqTopic;
private String tags;
private String key;
private String schemaStr;
private String filePath; public RocketProducer configure(OperatorConfiguration conf) {
this.nameserver = conf.get("rocketmq.nameserver.list");
this.rocketmqTopic = conf.get("rocketmq.topic");
this.tags = conf.get("rocketmq.tags");
this.key = conf.get("rocketmq.message.key");
this.schemaStr = conf.get("schemaStr");
this.filePath = conf.get("filePath");
return this;
} public int run() {
DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
producer.setNamesrvAddr(nameserver);
producer.setInstanceName("RocketProducer");
/**
* Producer对象在使用之前必须要调用start初始化,初始化一次即可<br>
* 注意:切记不可以在每次发送消息时,都调用start方法
*/
try {
producer.start();
} catch (MQClientException e) {
e.printStackTrace();
} HCatSchema hcatSchema = null;
Schema schema = null;
SchemaUtil schemaUtil = new SchemaUtil();
try {
hcatSchema = schemaUtil.createHCatSchema(schemaStr);
schema = schemaUtil.createSchema("com.scistor.rocketmq.producer", rocketmqTopic, hcatSchema);
} catch (HCatException e) {
e.printStackTrace();
} List<String> content = RocketProducer.readFileByLines(filePath); /**
* 下面这段代码表明一个Producer对象可以发送多个topic,多个tag的消息。
* 注意:send方法是同步调用,只要不抛异常就标识成功。但是发送成功也可会有多种状态,<br>
* 例如消息写入Master成功,但是Slave不成功,这种情况消息属于成功,但是对于个别应用如果对消息可靠性要求极高,<br>
* 需要对这种情况做处理。另外,消息可能会存在发送失败的情况,失败重试由应用来处理。
*/
for (int i = 0; i < content.size(); i++) {
try {
{
String[] fields = content.get(i).split(",");
Object[] record = AvroUtils.convert(schema, fields);
byte[] bytes = AvroUtils.serialize(schema, record);
Message msg = new Message(rocketmqTopic,// topic
tags,// tag
key,// key
bytes);// body
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
}
} catch (Exception e) {
e.printStackTrace();
}
//TimeUnit.MILLISECONDS.sleep(10);
} /**
* 应用退出时,要调用shutdown来清理资源,关闭网络连接,从MetaQ服务器上注销自己
* 注意:我们建议应用在JBOSS、Tomcat等容器的退出钩子里调用shutdown方法
*/
producer.shutdown();
return 0;
} public static List<String> readFileByLines(String fileName) {
List<String> list = new ArrayList<String>();
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
list.add(tempString);
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return list;
} public static void main(String[] args) {
if (args.length < 1) {
System.err.println("需要: 参数配置文件<stdin.xml>所在的hdfs目录");
System.exit(-1);
}
OperatorConfiguration conf = new OperatorConfiguration(args[0]);
RocketProducer trainer = new RocketProducer();
System.exit(trainer.configure(conf).run());
}
}

  

程序运行输出打印到控制台:

[root@m106 rocketmq]# ./produce.sh

以行为单位读取文件内容,一次读一整行:
line 1: hdfs:///user/xdf/streaming/file-web/file/1.html,1
line 2: hdfs:///user/xdf/streaming/file-web/file/2.html,2
line 3: hdfs:///user/xdf/streaming/file-web/file/3.html,3
line 4: hdfs:///user/xdf/streaming/file-web/file/4.html,4
line 5: hdfs:///user/xdf/streaming/file-web/file,1
line 6: /home/xdf/workflow/file-web/file/1.html,1
line 7: /home/xdf/workflow/file-web/file/2.html,2
line 8: /home/xdf/workflow/file-web/file/3.html,3
line 9: /home/xdf/workflow/file-web/file/4.html,4
line 10: /home/xdf/workflow/file-web/file,1
SendResult [sendStatus=SEND_OK, msgId=AC10086A00002A9F000000001FB00A36, messageQueue=MessageQueue [topic=TopicTest2, brokerName=broker-a, queueId=0], queueOffset=18710]
SendResult [sendStatus=SEND_OK, msgId=AC10086A00002A9F000000001FB00AED, messageQueue=MessageQueue [topic=TopicTest2, brokerName=broker-a, queueId=1], queueOffset=18700]
SendResult [sendStatus=SEND_OK, msgId=AC10086A00002A9F000000001FB00BA4, messageQueue=MessageQueue [topic=TopicTest2, brokerName=broker-a, queueId=2], queueOffset=18668]
SendResult [sendStatus=SEND_OK, msgId=AC10086A00002A9F000000001FB00C5B, messageQueue=MessageQueue [topic=TopicTest2, brokerName=broker-a, queueId=3], queueOffset=18663]
SendResult [sendStatus=SEND_OK, msgId=AC10086B00002A9F000000001E197504, messageQueue=MessageQueue [topic=TopicTest2, brokerName=broker-b, queueId=0], queueOffset=18649]
SendResult [sendStatus=SEND_OK, msgId=AC10086B00002A9F000000001E1975B4, messageQueue=MessageQueue [topic=TopicTest2, brokerName=broker-b, queueId=1], queueOffset=18633]
SendResult [sendStatus=SEND_OK, msgId=AC10086B00002A9F000000001E197663, messageQueue=MessageQueue [topic=TopicTest2, brokerName=broker-b, queueId=2], queueOffset=18629]
SendResult [sendStatus=SEND_OK, msgId=AC10086B00002A9F000000001E197712, messageQueue=MessageQueue [topic=TopicTest2, brokerName=broker-b, queueId=3], queueOffset=18626]
SendResult [sendStatus=SEND_OK, msgId=AC10086A00002A9F000000001FB00D12, messageQueue=MessageQueue [topic=TopicTest2, brokerName=broker-a, queueId=0], queueOffset=18711]
SendResult [sendStatus=SEND_OK, msgId=AC10086A00002A9F000000001FB00DC1, messageQueue=MessageQueue [topic=TopicTest2, brokerName=broker-a, queueId=1], queueOffset=18701]

  

RocketMQ生产者示例程序的更多相关文章

  1. RocketMQ消费者示例程序

    转载请注明出处:http://www.cnblogs.com/xiaodf/ 本博客实现了一个简单的RocketMQ消费者的示例,MQ里存储的是经过Avro序列化的消息数据,程序读取数据并反序列化后, ...

  2. 【rabbitmq】rabbitmq概念解析--消息确认--示例程序

    概述 本示例程序全部来自rabbitmq官方示例程序,rabbitmq-demo: 官方共有6个demo,针对不同的语言(如 C#,Java,Spring-AMQP等),都有不同的示例程序: 本示例程 ...

  3. 通过Jexus 部署 dotnetcore版本MusicStore 示例程序

    ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...

  4. .NET跨平台:在Ubuntu上用自己编译的dnx运行ASP.NET 5示例程序

    在 Linux Ubuntu 上成功编译 dnx 之后,会在 artifacts/build/ 文件夹中生成 dnx-coreclr-linux-x64/ 与 dnx-mono/ 这2个文件夹,前者是 ...

  5. .NET跨平台:在CentOS上编译dnx并运行ASP.NET 5示例程序

    在之前的博文中我们在 Ubuntu 上成功编译出了 dnx ,并且用它成功运行了 ASP.NET 5 示例程序.在这篇博文中我们将 Ubuntu 换成 CentOS. 目前 dnx 的编译需要用到 m ...

  6. Salesforce Apex 使用JSON数据的示例程序

    本文介绍了一个在Salesforce Apex中使用JSON数据的示例程序, 该示例程序由以下几部分组成: 1) Album.cls, 定了了封装相关字段的数据Model类 2) RestClient ...

  7. osg 示例程序解析之osgdelaunay

    osg 示例程序解析之osgdelaunay 转自:http://lzchenheng.blog.163.com/blog/static/838335362010821103038928/ 本示例程序 ...

  8. DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版原创发布-带官方示例程序版

    关于 DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版------------- ...

  9. DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版原创发布-带官方示例程序版

    关于 DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...

随机推荐

  1. Oracle软件开发分析

    软件开发的步骤可大致分为: 1.需求分析 2.系统设计 3.编码实现 4.系统测试 5.运行维护 student class     多对一 sno name age gender cid id  c ...

  2. JQ写下拉列表项目移动(内附效果图和源代码)

    效果图如下: 实现功能:点击第一个按钮,让选中的对象从左边移动到右边: 点击第二个按钮,让左边的所有对象移动到右边: 点击第三个按钮,让选中的对象从右边边移动到左边:   点击第四个按钮,让右边的所有 ...

  3. UVa 12299 RMQ with Shifts(移位RMQ)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...

  4. 浅析C#深拷贝与浅拷贝

    1.深拷贝与浅拷贝   拷贝即是通常所说的复制(Copy)或克隆(Clone),对象的拷贝也就是从现有对象复制一个“一模一样”的新对象出来.虽然都是复制对象,但是不同的 复制方法,复制出来的新对象却并 ...

  5. C语言中const的正确用法

    今天看<Linux内核编程>(Claudia Salzberg Podriguez等著)时,文中(p39)有一个错误,就是关于const的用法. 原文中举例说明:const int *x中 ...

  6. poj2778DNA Sequence(AC自动机+矩阵乘法)

    链接 看此题前先看一下matrix67大神写的关于十个矩阵的题目中的一个,如下: 经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值    把给定的图转为邻 ...

  7. iOS 解惑

    (1)ARC下IBOutlet用weak还是strong http://blog.csdn.net/yongyinmg/article/details/20623605 苹果也没有完全建议用weak ...

  8. K-邻近算法

    K-邻近算法 采用测量不同特征值之间的距离来进行分类 Ad:精度高,对异常值不敏感,无数据输入假定 Na:计算复杂度高,空间复杂度高 KNN原理 存在样本集,每个数据都存在标签,输入无标签的新数据后, ...

  9. Android属性动画完全解析(上),初识属性动画的基本用法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43536355 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系 ...

  10. 渲染引擎之Camera

    Camera, 在游戏渲染引擎里面, 如果摄影师的眼睛, 它决定了观众可以看到的游戏内容.从3D技术角度讲,Camera就是如何计算视图矩阵view matrix的模块(这里不讨论透视的方法),无论是 ...