Rocket MQ--004--发送和消费消息示例

消息发送模式

Rocket MQ发送消息可分为同步发送、异步发送和单向发送
同步发送:发送消息要等待,直到MQ响应
异步发送:发送消息不需要等待
单向发送:发送之后代码继续运行,无需关心发送结果,无需等待MQ响应

消息发送示例

package com.klcwqy.rocketmq;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * Rocket MQ发送消息模式
 * @author klcwqy
 *
 */
public class SendMessageModel {

	private static DefaultMQProducer producer = null;
	
	static {
		 producer = new DefaultMQProducer("SendMessageModel");
		 producer.setNamesrvAddr("192.168.8.2:9876;192.168.8.3:9876");
		 try {
			producer.start();
			//设置发送异步消息失败时的尝试次数,异步消息时打开
			//producer.setRetryTimesWhenSendAsyncFailed(0);
		} catch (MQClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 发送同步消息
	 * @param message
	 * @throws Exception
	 */
	public static void sendMessage(Message message) throws Exception {
		//等待发送结果
		SendResult sendResult = producer.send(message);
		System.out.printf("%s%n",sendResult);
	}
	
	/**
	 * 发送异步消息
	 * @param message
	 * @throws Exception
	 */
	public static void sendAsyncMessage(Message message) throws Exception {
		//无需等待结果
		producer.send(message, new SendCallback() {
			
			public void onSuccess(SendResult sendResult) {
				//发送成功时调用
				System.out.printf("%s%n",sendResult);
			}
			
			public void onException(Throwable e) {
				//发送失败时调用
			}
		});
	}
	
	/**
	 * 发送单向消息
	 * @param message
	 * @throws InterruptedException 
	 * @throws RemotingException 
	 * @throws MQClientException 
	 */
	public static void sendOneWayMessage(Message message) throws Exception {
		//无需等待结果
		producer.sendOneway(message);
	}
}

消息消费模式

消息消费分为拉取和推送模式
拉取模式:客户端主动去MQ拉取消息
推送模式:MQ主动将消息推送到客户端

消息消费示例

1 推送模式

package com.klcwqy.rocketmq;

import java.util.List;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

public class PushConsume {
	
	public static void main(String[] args) throws Exception {
		//创建消费
		DefaultMQPushConsumer consume = new DefaultMQPushConsumer("test-consume-group");
		//绑定NameServer
		consume.setNamesrvAddr("192.168.8.2:9876;192.168.8.3:9876");
		//订阅Topic
		consume.subscribe(CaseOneProducer.TOPIC, "*");
		//注册回调,有消息时mq会推进来
		consume.registerMessageListener(new MessageListenerConcurrently() {
			
			public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
				MessageExt messageExt = msgs.get(0);
				System.out.println(new String(messageExt.getBody()));
				return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
			}
		});
		//启动消费者
		consume.start();
		
	}
}

2 拉取模式

package com.klcwqy.rocketmq;

import java.util.List;

import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;

public class PullConsume {
	
	public static void main(String[] args) throws Exception {
		//创建消费
		DefaultLitePullConsumer consume = new DefaultLitePullConsumer("test-consume-group");
		//绑定NameServer
		consume.setNamesrvAddr("192.168.8.2:9876;192.168.8.3:9876");
		consume.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
		consume.subscribe("TopicTest", "*");
		consume.start();
        while (true) {
            List<MessageExt> messageExts = consume.poll();
            System.out.printf("%s%n", messageExts);
        }
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值