rocketMq-demo

本文档主要记录了RocketMQ的快速入门,包括前言、jar包的准备,以及Producer和Consumer的简单示例代码。
摘要由CSDN通过智能技术生成

rocketMq-demo

  • 前言
  • jar包预备
  • demo

前言

前面一面博文写了RocketMq的配置安装,这里记录一下自己的demo。

jar包预备

我用的maven来管理项目,加入maven依赖:

<dependency>
  <groupId>com.alibaba.rocketmq</groupId>
  <artifactId>rocketmq-client</artifactId>
  <version>3.2.6</version>
</dependency>

Producer

  package com.yyp.test;

    /**
     * Created by yyp on 15-12-18.
     */

    import com.alibaba.rocketmq.common.message.Message;

    import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
    import com.alibaba.rocketmq.client.producer.SendResult;

    public class Producer {
    public static void main(String[] args) {
    DefaultMQProducer producer = new DefaultMQProducer("Producer");
    producer.setNamesrvAddr("192.168.0.1:9876");
    try {
    producer.start();
    int i = 0;

    while (true) {
    i++;
    Message msg = new Message("PushTopic",
    "push",
    "1",
    ("Just for yyp" + i).getBytes());
    SendResult result = producer.send(msg);

       /* msg = new Message("PushTopic",
    "push",
    "2",
    "Just for test.".getBytes());

    result = producer.send(msg);
    System.out.println("id:" + result.getMsgId() +
    " result:" + result.getSendStatus());*/

    msg = new Message("PullTopic",
    "pull",
    "1",
    "Just for test.".getBytes());
    result = producer.send(msg);
    System.out.println("id:" + result.getMsgId() +
    " result:" + result.getSendStatus());
    if (i % 10 == 0) {
    Thread.sleep(20000);
    }
    }

    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    // producer.shutdown();
    }
    }
    }

Consumer

    package com.yyp.test;

    /**
     * Created by yyp on 15-12-18.
     */

    import com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer;
    import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
    import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
    import com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently;
    import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
    import com.alibaba.rocketmq.client.producer.SendResult;
    import com.alibaba.rocketmq.common.consumer.ConsumeFromWhere;
    import com.alibaba.rocketmq.common.message.Message;
    import com.alibaba.rocketmq.common.message.MessageExt;
    import java.util.List;

    public class Consumer {
    public static void main(String[] args) {
    DefaultMQPushConsumer consumer =
    new DefaultMQPushConsumer("PushConsumer");
    consumer.setNamesrvAddr("192.168.0.1:9876");
    try {
    //订阅PushTopic下Tag为push的消息
    consumer.subscribe("PushTopic", "push");
    //程序第一次启动从消息队列头取数据
    consumer.setConsumeFromWhere(
    ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    consumer.registerMessageListener(
    new MessageListenerConcurrently() {
    public ConsumeConcurrentlyStatus consumeMessage(
    List<MessageExt> list,
    ConsumeConcurrentlyContext Context) {
       /* Message msg = list.get(0);
    System.out.println(msg.toString());*/
    MessageExt messageExt=list.get(0);
    System.out.println("msg:\t"+new String(messageExt.getBody()));
    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
    }
    );
    consumer.start();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值