RocketMQ安装与demo

下载地址:https://github.com/alibaba/RocketMQ/

1. 下载zip压缩包,解压,到根目录执行:mvn -Dmaven.test.skip=true clean package install assembly:assembly -U

2.完成后在target目录下 会生成alibaba-rocketmq-3.2.6-alibaba-rocketmq.tar.gz 

3. 将该压缩包放入linux中,执行 tar -zxvf alibaba-rocketmq-3.2.6-alibaba-rocketmq.tar.gz 解压

4. 将bin文件夹赋权限为777 chmod -R 777 /bin

5.进入bin目录。启动nameserver :nohup sh mqnamesrv &

         关闭nameserver: sh mqshutdown namesrv

         启动mqbroker     nohup sh  mqbroker -n "127.0.0.1:9876" &

关闭mqbroker     sh mqshutdown broker 

demo:

 Producer类

package com.lvxc.study.tech.rmq;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;

public class Producer {

	public static void main(String[] args) {
		DefaultMQProducer producer = new DefaultMQProducer("Producer");
		//nameserver服务,多个以;分开 
		producer.setNamesrvAddr("192.168.133.128:9876");
		try{
			producer.start();
			
			Message msg = new Message("PushTopic","push","1","Just for test.".getBytes());
			SendResult result = producer.send(msg);
			System.out.println("id:"+result.getMsgId()+" result:" +result.getSendStatus());;
			
			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());;
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			producer.shutdown();
		}
	}

}

  Consumer类

package com.lvxc.study.tech.rmq;

import java.util.List;

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.common.consumer.ConsumeFromWhere;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.common.message.MessageExt;

public class Consumer {

	public static void main(String[] args) {
		DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("PushConsumer");  
        consumer.setNamesrvAddr("192.168.133.128:9876");   
        try {  
            //订阅PushTopic下Tag为push的消息  
            consumer.subscribe("PushTopic", "push");  
            //程序第一次启动从消息队列头取数据  
            consumer.setConsumeFromWhere(  
                    ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);  
            consumer.registerMessageListener(  
                new MessageListenerConcurrently() {  
                    public ConsumeConcurrentlyStatus consumeMessage(  
                            List list,  
                            ConsumeConcurrentlyContext Context) {  
                        Message msg = list.get(0);  
                        System.out.println(msg.toString());  
                        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;  
                    }  
                }  
            );  
            consumer.start();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  

}

   

你可能感兴趣的:(RocketMQ)