ROCKETMQ Producer Sample Program

Source: Internet
Author: User
Tags string tags nameserver

  Reprint Please specify the Source: http://www.cnblogs.com/xiaodf/

This example shows a simple implementation of the ROCKETMQ producer, which obtains input data by parsing a text file and sends the data to ROCKETMQ after Avro serialization.

The program obtains the main parameter value through the Stdin.xml configuration file, the Stdin.xml file contents are as follows:

<?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>

  

The

Producer sample program is as follows:

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 Object must call start initialization before use, initialize once <br> * Note: Remember that you cannot call the Start method */try {Every time you send a message.        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);         /** * The following code indicates that a producer object can send multiple topic, multiple tag messages. * Note: The Send method is a synchronous call, so long as the exception is not thrown, the identity succeeds. However, there can be a variety of states for sending success,<br> * For example, message writing to master succeeds, but Slave is unsuccessful, this condition message is successful, but for individual applications if the reliability of the message requires very high,<br> * need for this situation Do the processing.         Additionally, messages may have failed to send, and failed retries are handled by the app. */for (int i = 0; i < content.size (); i++) {try {string[] Fiel                    ds = 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);         /** * When the app exits, call shutdown to clean up the resource, close the network connection, log off from the Metaq server * Note: We recommend that you call the shutdown method in the exit hooks of a container such as JBoss, Tomcat, etc.        */Producer.shutdown ();    return 0; public static list<string> Readfilebylines (String fileName) {list<string> List = new Arraylist&lt ;        String> ();        File File = new file (fileName);        BufferedReader reader = null;            try {System.out.println ("reads the contents of the file in the behavior unit, reads a whole line at a time:");            reader = new BufferedReader (new FileReader (file));            String tempstring = null;            int line = 1;                Read one line at a time until NULL is read to the end of the file while ((tempstring = Reader.readline ()) = null) {//Display line number                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; The public static void main (string[] args) {if (Args.length < 1) {System.err.println ("required: Parameter configuration file &L            The HDFs directory where the t;stdin.xml> is located ");        System.exit (-1);        } operatorconfiguration conf = new Operatorconfiguration (args[0]);        Rocketproducer trainer = new Rocketproducer ();    System.exit (Trainer.configure (CONF). Run ()); }}

  

Program run output Print to console:

[Email protected] rocketmq]#./produce.sh
Read the contents of the file in the behavior unit, one at a time: line 1:hdfs:///user/xdf/streaming/file-web/file/1.html,1line 2:hdfs:///user/xdf/streaming/ File-web/file/2.html,2line 3:hdfs:///user/xdf/streaming/file-web/file/3.html,3line 4:hdfs:///user/xdf/streaming/ File-web/file/4.html,4line 5:hdfs:///user/xdf/streaming/file-web/file,1line 6:/home/xdf/workflow/file-web/file/ 1.html,1line 7:/home/xdf/workflow/file-web/file/2.html,2line 8:/home/xdf/workflow/file-web/file/3.html,3line 9:/ Home/xdf/workflow/file-web/file/4.html,4line:/home/xdf/workflow/file-web/file,1sendresult [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 Producer Sample Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.