码迷,mamicode.com
首页 > 编程语言 > 详细

Java操作RocketMQ

时间:2020-02-17 18:18:45      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:++   cat   ide   listen   thread   rocketmq   down   shu   stack   

一、目录展示

  技术图片

二、导入依赖

<dependency>
      <groupId>com.alibaba.rocketmq</groupId>
      <artifactId>rocketmq-client</artifactId>
      <version>3.0.10</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba.rocketmq</groupId>
      <artifactId>rocketmq-all</artifactId>
      <version>3.0.10</version>
      <type>pom</type>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.1.1</version>
    </dependency>

三、提供者

package com.zn;

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("java-group");
        producer.setNamesrvAddr("192.168.33.135:9876;192.168.33.136:9876");
        producer.setInstanceName("producer");
        producer.start();

        try {
            for (int i=0;i<10;i++){
                //每秒发送一次MQ
                Thread.sleep(1000);
                //topic主题名称  tag临时值, 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;

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("java-group");
        consumer.setNamesrvAddr("192.168.33.135:9876;192.168.33.136:9876");
        consumer.setInstanceName("consumer");
        consumer.subscribe("itmayiedu-topic","TagA");

        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt:list){
                    System.out.println(messageExt.getMsgId()+"---"+new String(messageExt.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
        System.out.println("consumer Started!");
    }
}

五、控制台效果

提供者:

  技术图片

消费者:

  技术图片

六、RocketMQ控制台效果

测试前:

  技术图片

 测试后:

  技术图片

 

Java操作RocketMQ

标签:++   cat   ide   listen   thread   rocketmq   down   shu   stack   

原文地址:https://www.cnblogs.com/Zzzzn/p/12322622.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!