Serial and parallel processing of alibaba rocket mq

There are many ways to use rocket mq in the project,

The first is strictly in accordance with the time consumption mode. This mode needs to use serial mode. When the producer produces, the producer needs to push to a specific queue in an orderly manner:

                     SendResult result = producer.send(msg, new MessageQueueSelector(){
@Override
public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
Integer id = arg.hashCode();
int index = id % mqs.size();
return mqs.get(index);
}

}, dataAsyncEvent.getDataType());

 

                   Consumers should also consume strictly in order, using this orderly listener:

                   //The messages of the same queue can only be consumed by one thread at the same time, which can ensure that the messages are consumed in the same queue strictly and orderly

 

 consumer.registerMessageListener(new MessageListenerOrderly() {
@Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs,
                    ConsumeOrderlyContext context) {

                    return ConsumeOrderlyStatus.CONSUME_SUCCESS;

             }

                   public interface MessageListenerOrderly extends MessageListener {
    /**
     * The method throwing an exception is equivalent to returning  ConsumeOrderlyStatus .SUSPEND_CURRENT_QUEUE_A_MOMENT<br>
     * PS: It is recommended that the application does not throw an exception
     * 
     * @param msgs
     * msgs.size() >= 1<br>
     * DefaultMQPushConsumer.consumeMessageBatchMaxSize=1, the default number of messages is 1
     * @param context
     * @return
     */
    public ConsumeOrderlyStatus consumeMessage(final List<MessageExt> msgs,
            final ConsumeOrderlyContext context);

 

The second: do not pursue chronological order, as long as all the events produced can be consumed. This can be processed in parallel, which is much more efficient:

               Producer:

               SendResult result = producer.send(msg);

                Consumer: (handle with this interface)

               consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                    ConsumeConcurrentlyContext context) {

                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

             }

                public interface  MessageListenerConcurrently  extends MessageListener {
    /**
     * Method throwing an exception is equivalent to returning ConsumeConcurrentlyStatus.RECONSUME_LATER<br>
     * PS: It is recommended that applications do not throw exceptions
     * 
     * @param msgs
     * msgs.size() >= 1<br>
     * DefaultMQPushConsumer.consumeMessageBatchMaxSize=1, the default number of messages is 1
     * @param context
     * @return
     */
    public ConsumeConcurrentlyStatus consumeMessage(final List<MessageExt> msgs,
            final ConsumeConcurrentlyContext context);
}

 

http://blog.csdn.net/fangletian1981/article/details/20694381

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326692777&siteId=291194637