RocketMQ learning

1, MQ mainly consists of four parts

  producer  consumer  broker  nameserver

boker nameserver as porters for the unified management services producer consumer does not have to say a producer a consumer

package com.jason.mq;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class SyncProducer {

public static void main(String[] args) throws Exception {

DefaultMQProducer producer =
new DefaultMQProducer("please_rename_unique_group_name");
producer.setNamesrvAddr("127.0.0.1:9876");
producer.start();

for(int i = 0; i < 100; i++) {
/* Message msg = new Message("TopicTest","TagA",
("Hello RockerMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);*/

Message message = new Message();
message.setTopic("TopicTest");
message.putUserProperty(i + "", "hello" + i);
message.setBody((i +"**").getBytes(RemotingHelper.DEFAULT_CHARSET));
SendResult sendResult = producer.send(message);
System.out.println(sendResult);
}
producer.shutdown();


}
}


以上是生产者 产生消息 发送到broker


package com.jason.mq;

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.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.io.UnsupportedEncodingException;
import java.util.List;

public class SyncConsumer {
public static void main(String[] args) throws Exception {

DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_to_unique_group_name");
consumer.setNamesrvAddr("127.0.0.1:9876");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

consumer.subscribe("TopicTest", "*");
consumer.registerMessageListener(new MessageListenerConcurrently() {
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {

for (MessageExt msg : list) {
try {
System.out.println("Receive properties: " + msg.getProperties());
System.out.println ( "the Receive body:" + new new String (msg.getBody (), RemotingHelper.DEFAULT_CHARSET));

} the catch (UnsupportedEncodingException E) {
e.printStackTrace ();
} the finally {
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
}


ConsumeConcurrentlyStatus.CONSUME_SUCCESS return;
}
});
consumer.start ();
}
}

or more for consumers to obtain information push

consumers into push pull
What is the difference? You know what?
push for general consumption did not slow down the situation (broker is actively push messages in the past) pull the initiative in general where consumers (consumers take the initiative to pull information)
Initiative to push, then use the case if consumer spending is too slow, then the broker will accumulate too many messages, so the consumption of fast processing of
the case of active pull in, as the case may be, they want to take the initiative to pull this information ,, it is also a short board, because the consumer can not determine exactly when to pull the latest news, the consumer has been, should have been pull

the actual situation choose.
I personally use push more




 

Guess you like

Origin www.cnblogs.com/lzphu/p/12233650.html