Java操作RocketMQ

一、目录展示

  Java操作RocketMQ_第1张图片

二、导入依赖

<dependency>
      <groupId>com.alibaba.rocketmqgroupId>
      <artifactId>rocketmq-clientartifactId>
      <version>3.0.10version>
    dependency>
    <dependency>
      <groupId>com.alibaba.rocketmqgroupId>
      <artifactId>rocketmq-allartifactId>
      <version>3.0.10version>
      <type>pomtype>
    dependency>
    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-classicartifactId>
      <version>1.1.1version>
    dependency>
    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-coreartifactId>
      <version>1.1.1version>
    dependency>

三、提供者

package com.zn.tests;

import com.alibaba.rocketmq.client.exception.MQBrokerException;
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.exception.RemotingException;

/**
 * 生产者
 */
public class Provider {
    public static void main(String[] args) throws MQClientException {
        //创建一个生产者
        DefaultMQProducer producer=new DefaultMQProducer("rmq-group");
        //设置NameServer地址
        producer.setNamesrvAddr("192.168.33.135:9876;192.168.33.136:9876");
        //设置生产者实例名称
        producer.setInstanceName("producer");
        //启动生产者
        producer.start();

        try {
            //发送消息
            for (int i=1;i<=10;i++){
                //模拟网络延迟,每秒发送一次MQ
                Thread.sleep(1000);
                //创建消息,topic主题名称  tags临时值代表小分类, body代表消息体
                Message message=new Message("itmayiedu-topic","TagA",("itmayiedu-"+i).getBytes());
                //发送消息
                SendResult sendResult=producer.send(message);
                System.out.println("来了来了:"+sendResult.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        producer.shutdown();
    }
}

四、消费者

package com.zn.tests;

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.exception.MQClientException;
import com.alibaba.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //创建消费者
        DefaultMQPushConsumer consumer=new DefaultMQPushConsumer("rmq-group");
        //设置NameServer地址
        consumer.setNamesrvAddr("192.168.33.135:9876;192.168.33.136:9876");
        //设置实例名称
        consumer.setInstanceName("consumer");
        //订阅topic
        consumer.subscribe("itmayiedu-topic","TagA");

        //监听消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                //获取消息
                for (MessageExt messageExt:list){
                    //RocketMQ由于是集群环境,所有产生的消息ID可能会重复
                    System.out.println(messageExt.getMsgId()+"---"+new String(messageExt.getBody()));
                }
                //接受消息状态 1.消费成功    2.消费失败   队列还有
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //启动消费者
        consumer.start();
        System.out.println("consumer Started!");
    }
}

五、控制台效果

提供者:

  Java操作RocketMQ_第2张图片

消费者:

  Java操作RocketMQ_第3张图片

六、RocketMQ控制台效果

测试前:

  Java操作RocketMQ_第4张图片

 测试后:

  Java操作RocketMQ_第5张图片

 

你可能感兴趣的:(Java操作RocketMQ)