com.alibaba.rocketmq.common.consumer.ConsumeFromWhere Java Examples

The following examples show how to use com.alibaba.rocketmq.common.consumer.ConsumeFromWhere. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Consumer.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4");
    /**
     * 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费<br>
     * 如果非第一次启动,那么按照上次消费的位置继续消费
     */
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

    consumer.subscribe("TopicTest", "*");

    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                ConsumeConcurrentlyContext context) {
            System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    consumer.start();

    System.out.println("Consumer Started.");
}
 
Example #2
Source File: ConsumerManager.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public boolean registerConsumer(final String group, final ClientChannelInfo clientChannelInfo,
                                ConsumeType consumeType, MessageModel messageModel, ConsumeFromWhere consumeFromWhere,
                                final Set<SubscriptionData> subList) {

    ConsumerGroupInfo consumerGroupInfo = this.consumerTable.get(group);
    if (null == consumerGroupInfo) {
        ConsumerGroupInfo tmp = new ConsumerGroupInfo(group, consumeType, messageModel, consumeFromWhere);
        ConsumerGroupInfo prev = this.consumerTable.putIfAbsent(group, tmp);
        consumerGroupInfo = prev != null ? prev : tmp;
    }

    boolean r1 =
            consumerGroupInfo.updateChannel(clientChannelInfo, consumeType, messageModel,
                    consumeFromWhere);
    boolean r2 = consumerGroupInfo.updateSubscription(subList);

    if (r1 || r2) {
        this.consumerIdsChangeListener.consumerIdsChanged(group, consumerGroupInfo.getAllChannel());
    }

    return r1 || r2;
}
 
Example #3
Source File: Consumer.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4");

    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

    consumer.subscribe("TopicTest", "*");

    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                ConsumeConcurrentlyContext context) {
            System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    consumer.start();

    System.out.println("Consumer Started.");
}
 
Example #4
Source File: ConsumerManager.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
/**
 * 注册消费者 返回是否有变化
 */
public boolean registerConsumer(final String group, final ClientChannelInfo clientChannelInfo,
        ConsumeType consumeType, MessageModel messageModel, ConsumeFromWhere consumeFromWhere,
        final Set<SubscriptionData> subList) {
    ConsumerGroupInfo consumerGroupInfo = this.consumerTable.get(group);
    if (null == consumerGroupInfo) {
        ConsumerGroupInfo tmp = new ConsumerGroupInfo(group, consumeType, messageModel, consumeFromWhere);
        ConsumerGroupInfo prev = this.consumerTable.putIfAbsent(group, tmp);
        consumerGroupInfo = prev != null ? prev : tmp;
    }

    boolean r1 = consumerGroupInfo.updateChannel(clientChannelInfo, consumeType, messageModel,
        consumeFromWhere);
    boolean r2 = consumerGroupInfo.updateSubscription(subList);

    if (r1 || r2) {
        // ConsumerId列表变化,通知所有Consumer
        this.consumerIdsChangeListener.consumerIdsChanged(group, consumerGroupInfo.getAllChannel());
    }

    return r1 || r2;
}
 
Example #5
Source File: Consumer.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4");

    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

    consumer.subscribe("TopicTest", "*");

    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                                                        ConsumeConcurrentlyContext context) {
            System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    consumer.start();

    System.out.println("Consumer Started.");
}
 
Example #6
Source File: ConsumerGroupInfo.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public boolean updateChannel(final ClientChannelInfo infoNew, ConsumeType consumeType,
        MessageModel messageModel, ConsumeFromWhere consumeFromWhere) {
    boolean updated = false;
    this.consumeType = consumeType;
    this.messageModel = messageModel;
    this.consumeFromWhere = consumeFromWhere;

    ClientChannelInfo infoOld = this.channelInfoTable.get(infoNew.getChannel());
    if (null == infoOld) { //原来没有注册对应的通道。
        //把新的通道注册进来 , 先进入的线程能够进入if 分支。
        ClientChannelInfo prev = this.channelInfoTable.put(infoNew.getChannel(), infoNew);
        if (null == prev) {
            log.info("new consumer connected, group: {} {} {} channel: {}", this.groupName, consumeType,
                messageModel, infoNew.toString());
            updated = true;
        }

        infoOld = infoNew;
    }
    else { //必须是相同的clientid 才能更新消费者通道信息。
        if (!infoOld.getClientId().equals(infoNew.getClientId())) {
            log.error(
                "[BUG] consumer channel exist in broker, but clientId not equal. GROUP: {} OLD: {} NEW: {} ",
                this.groupName,//
                infoOld.toString(),//
                infoNew.toString());
            this.channelInfoTable.put(infoNew.getChannel(), infoNew); //clientid不同的时候 ,也认为是加入新的消费者通道信息。
        }
    }

    this.lastUpdateTimestamp = System.currentTimeMillis();
    infoOld.setLastUpdateTimestamp(this.lastUpdateTimestamp);

    return updated;
}
 
Example #7
Source File: PushConsumer.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1");
    /**
     * 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费<br>
     * 如果非第一次启动,那么按照上次消费的位置继续消费
     */
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

    consumer.setMessageModel(MessageModel.BROADCASTING);

    consumer.subscribe("TopicTest", "TagA || TagC || TagD");

    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                ConsumeConcurrentlyContext context) {
            System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs);

            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    consumer.start();

    System.out.println("Broadcast Consumer Started.");
}
 
Example #8
Source File: Consumer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_3");

    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

    consumer.subscribe("TopicTest", "TagA || TagC || TagD");

    consumer.registerMessageListener(new MessageListenerOrderly() {
        AtomicLong consumeTimes = new AtomicLong(0);


        @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
            context.setAutoCommit(false);
            System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs);
            this.consumeTimes.incrementAndGet();
            if ((this.consumeTimes.get() % 2) == 0) {
                return ConsumeOrderlyStatus.SUCCESS;
            } else if ((this.consumeTimes.get() % 3) == 0) {
                return ConsumeOrderlyStatus.ROLLBACK;
            } else if ((this.consumeTimes.get() % 4) == 0) {
                return ConsumeOrderlyStatus.COMMIT;
            } else if ((this.consumeTimes.get() % 5) == 0) {
                context.setSuspendCurrentQueueTimeMillis(3000);
                return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
            }

            return ConsumeOrderlyStatus.SUCCESS;
        }
    });

    consumer.start();

    System.out.println("Consumer Started.");
}
 
Example #9
Source File: RocketMQConsumer.java    From uavstack with Apache License 2.0 5 votes vote down vote up
private DefaultMQPushConsumer initConsumer(QueueInfo queueInfo) {

        DefaultMQPushConsumer dmpc = new DefaultMQPushConsumer(consumerConfig.getComsumerGroup());
        /**
         * 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费<br>
         * 如果非第一次启动,那么按照上次消费的位置继续消费
         */

        dmpc.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        dmpc.setNamesrvAddr(consumerConfig.getNamingServer());
        if (consumerConfig.getPullBatchSize() >= 0) {
            dmpc.setPullBatchSize(50);
        }

        // subscribeTopics
        subscribeTopics(dmpc, queueInfo);

        // 配置是否是单线程的consumer监听,因为在处理事务的时候,使用actor模式,需要单线程处理那些数据库写的请求
        if (consumerConfig.getConsumeThreadMax() != null && consumerConfig.getConsumeThreadMax() > 0) {
            dmpc.setConsumeThreadMax(consumerConfig.getConsumeThreadMax());
        }
        if (consumerConfig.getConsumeThreadMin() != null && consumerConfig.getConsumeThreadMin() > 0) {
            dmpc.setConsumeThreadMin(consumerConfig.getConsumeThreadMin());
        }

        // 进行空值测试,如果没有填写queue类型一律按照queue信息算
        if (MQFactory.QueueType.TOPIC.equals(queueInfo.getQueueType())) {
            dmpc.setMessageModel(MessageModel.BROADCASTING);
        }
        return dmpc;
    }
 
Example #10
Source File: ConsumerGroupInfo.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public ConsumerGroupInfo(String groupName, ConsumeType consumeType, MessageModel messageModel,
                         ConsumeFromWhere consumeFromWhere) {
    this.groupName = groupName;
    this.consumeType = consumeType;
    this.messageModel = messageModel;
    this.consumeFromWhere = consumeFromWhere;
}
 
Example #11
Source File: ConsumerGroupInfo.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
public ConsumerGroupInfo(String groupName, ConsumeType consumeType, MessageModel messageModel,
        ConsumeFromWhere consumeFromWhere) {
    this.groupName = groupName;
    this.consumeType = consumeType;
    this.messageModel = messageModel;
    this.consumeFromWhere = consumeFromWhere;
}
 
Example #12
Source File: ConsumerGroupInfo.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
/**
 * 更新netty连接 返回值表示是否发生变更
 */
public boolean updateChannel(final ClientChannelInfo infoNew, ConsumeType consumeType,
        MessageModel messageModel, ConsumeFromWhere consumeFromWhere) {
    boolean updated = false;
    this.consumeType = consumeType;
    this.messageModel = messageModel;
    this.consumeFromWhere = consumeFromWhere;

    ClientChannelInfo infoOld = this.channelInfoTable.get(infoNew.getChannel());
    if (null == infoOld) {
        ClientChannelInfo prev = this.channelInfoTable.put(infoNew.getChannel(), infoNew);
        // 判断是否插入成功
        if (null == prev) {
            log.info("new consumer connected, group: {} {} {} channel: {}", this.groupName, consumeType,
                messageModel, infoNew.toString());
            updated = true;
        }

        infoOld = infoNew;
    }
    else {
        if (!infoOld.getClientId().equals(infoNew.getClientId())) {
            log.error(
                "[BUG] consumer channel exist in broker, but clientId not equal. GROUP: {} OLD: {} NEW: {} ",
                this.groupName, //
                infoOld.toString(), //
                infoNew.toString());
            this.channelInfoTable.put(infoNew.getChannel(), infoNew);
        }
    }

    this.lastUpdateTimestamp = System.currentTimeMillis();
    infoOld.setLastUpdateTimestamp(this.lastUpdateTimestamp);

    return updated;
}
 
Example #13
Source File: ConsumerGroupInfo.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public boolean updateChannel(final ClientChannelInfo infoNew, ConsumeType consumeType,
                             MessageModel messageModel, ConsumeFromWhere consumeFromWhere) {
    boolean updated = false;
    this.consumeType = consumeType;
    this.messageModel = messageModel;
    this.consumeFromWhere = consumeFromWhere;

    ClientChannelInfo infoOld = this.channelInfoTable.get(infoNew.getChannel());
    if (null == infoOld) {
        ClientChannelInfo prev = this.channelInfoTable.put(infoNew.getChannel(), infoNew);
        if (null == prev) {
            log.info("new consumer connected, group: {} {} {} channel: {}", this.groupName, consumeType,
                    messageModel, infoNew.toString());
            updated = true;
        }

        infoOld = infoNew;
    } else {
        if (!infoOld.getClientId().equals(infoNew.getClientId())) {
            log.error(
                    "[BUG] consumer channel exist in broker, but clientId not equal. GROUP: {} OLD: {} NEW: {} ",
                    this.groupName,//
                    infoOld.toString(),//
                    infoNew.toString());
            this.channelInfoTable.put(infoNew.getChannel(), infoNew);
        }
    }

    this.lastUpdateTimestamp = System.currentTimeMillis();
    infoOld.setLastUpdateTimestamp(this.lastUpdateTimestamp);

    return updated;
}
 
Example #14
Source File: ConsumerManager.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 把 消费者(用clientChannelInfo 标识) 注册到指定的消费者分组(group),, 并且包含订阅元数据。
 * (消费类型, 消费模式, 位点类型,订阅的topic列表 。)
 * @param group
 * @param clientChannelInfo
 * @param consumeType
 * @param messageModel
 * @param consumeFromWhere
 * @param subList
 * @return
 */
public boolean registerConsumer(final String group, final ClientChannelInfo clientChannelInfo,
        ConsumeType consumeType, MessageModel messageModel, ConsumeFromWhere consumeFromWhere,
        final Set<SubscriptionData> subList) {
    ConsumerGroupInfo consumerGroupInfo = this.consumerTable.get(group);
    if (null == consumerGroupInfo) {
        ConsumerGroupInfo tmp = new ConsumerGroupInfo(group, consumeType, messageModel, consumeFromWhere);
        ConsumerGroupInfo prev = this.consumerTable.putIfAbsent(group, tmp);
        consumerGroupInfo = prev != null ? prev : tmp;
    }

    //把消费者通道(对应一个clientid ,默认clientip@processid.)注册到消费者分组下。
    boolean r1 =
            consumerGroupInfo.updateChannel(clientChannelInfo, consumeType, messageModel,
                consumeFromWhere);
    boolean r2 = consumerGroupInfo.updateSubscription(subList);

    if (r1 || r2) { //消费者被加入到消费者分组(r1) || 消费者分组增加或者删除了对topic的订阅 , 则给分组中的所有消费者发送
        //消费者id的变更通知, 启动client的rebalance (但这里有一点疑问, 如果消费者退出消费者分组,也应该启动消费者id 的变更通知的。
        // 仔细想一下,因为这里是消费者注册操作, 所以当然是消费者加入消费者分组!!!而消费者退出则应该通过其他的心跳操作来解决。

        //所谓rebalance ,以集群消费为例,其实就是一个消费者分组中的消费者要分摊消费topic下的所有消息。
        //而具体的rebalance算法是按照topic来做的 ,具体是:
        //每一个消费者client 收到reblance指令以后,看自己订阅了哪些topic ,然后按topic分别做rebalance .

        //而每一个topic内的rebalance操作如下:
        //先从client本地缓存的路由表中获取topic归属的brokername下的broker master .
        //然后问broker master要订阅者clientid列表, 然后把topic下的消费队列按均摊算法大致均匀分配给各个clientid ,
        //退出某一个队列的消费者放手(把位点更新到broker)  , 而加入队列的消费者接手(把放手的那个消费者位点拿过来以拉的方式消费)。
        // )
        this.consumerIdsChangeListener.consumerIdsChanged(group, consumerGroupInfo.getAllChannel());
    }

    return r1 || r2;
}
 
Example #15
Source File: ConsumerGroupInfo.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public ConsumerGroupInfo(String groupName, ConsumeType consumeType, MessageModel messageModel,
        ConsumeFromWhere consumeFromWhere) {
    this.groupName = groupName;
    this.consumeType = consumeType;
    this.messageModel = messageModel;
    this.consumeFromWhere = consumeFromWhere;
}
 
Example #16
Source File: PushConsumer.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException, MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("yyzGroup23");
    consumer.setNamesrvAddr("10.2.223.157:9876;10.2.223.158:9876;10.2.223.159:9876");
   // consumer.setNamesrvAddr("10.2.223.228:9876");
    //consumer.subscribe("my-topic-2", "*", new GroovyScript(groovyScript));
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
    //consumer.setMessageModel(MessageModel.CLUSTERING);
    consumer.subscribe("yyztest2", "*");

    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override  //consumeMessage在ConsumeMessageConcurrentlyService中的接口consumeMessageDirectly中執行該函數
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                ConsumeConcurrentlyContext context) {
            if(msgs == null || msgs.size() == 0){
                System.out.println("not get msgs"); /* Add Trace Log */
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
            System.out.println("consumeMessage recv {} Msgs:"+msgs.size());
            for (MessageExt msg : msgs) { /* Add Trace Log */
                System.out.println("Msgid:{}" +msg.getMsgId());
            }

            for(MessageExt messageExt: msgs) {
                System.out.println("recv msg with topic:" + messageExt.getTopic() + ",msgTag:" + messageExt.getTags() +  ", body:" + new String(messageExt.getBody()));
            }
             /* 如果返回不是成功,则该msgs消息会在内存中,offset还是在上次的位置 */
            //业务处理消息后,对返回值的检查在ConsumeRequest.run-> ConsumeMessageConcurrentlyService.processConsumeResult 中
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });

    consumer.start();

    System.out.println("Consumer Started.");
}
 
Example #17
Source File: Consumer.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_3");

    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

    consumer.subscribe("TopicTest", "TagA || TagC || TagD");

    consumer.registerMessageListener(new MessageListenerOrderly() {
        AtomicLong consumeTimes = new AtomicLong(0);


        @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
            context.setAutoCommit(false);
            System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs);
            this.consumeTimes.incrementAndGet();
            if ((this.consumeTimes.get() % 2) == 0) {
                return ConsumeOrderlyStatus.SUCCESS;
            }
            else if ((this.consumeTimes.get() % 3) == 0) {
                return ConsumeOrderlyStatus.ROLLBACK;
            }
            else if ((this.consumeTimes.get() % 4) == 0) {
                return ConsumeOrderlyStatus.COMMIT;
            }
            else if ((this.consumeTimes.get() % 5) == 0) {
                context.setSuspendCurrentQueueTimeMillis(3000);
                return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
            }

            return ConsumeOrderlyStatus.SUCCESS;
        }
    });

    consumer.start();

    System.out.println("Consumer Started.");
}
 
Example #18
Source File: ConsumerMeta.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public ConsumerMeta(String groupName, String topic, Collection<String> tags,
		MessageModel messageModel, ConsumeFromWhere consumeFromWhere, long ignoreOffSetThreshold) {
	super();
	this.groupName = groupName;
	this.topic = topic;
	this.tags = tags;
	this.messageModel = messageModel;
	this.consumeFromWhere = consumeFromWhere;
	this.ignoreOffSetThreshold = ignoreOffSetThreshold;
}
 
Example #19
Source File: ConsumerMeta.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public ConsumerMeta(String groupName, String topic, String... tags) {
	super();
	this.groupName = groupName;
	this.topic = topic;
	this.tags = Arrays.asList(tags);
	this.messageModel = MessageModel.CLUSTERING;
	this.consumeFromWhere = ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET;
	this.ignoreOffSetThreshold = -1;
}
 
Example #20
Source File: Consumer.java    From zheng with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    DefaultMQPushConsumer consumer =
            new DefaultMQPushConsumer("PushConsumer");
    consumer.setNamesrvAddr("127.0.0.1:9876");
    try {
        //订阅PushTopic下Tag为push的消息
        consumer.subscribe("PushTopic", "push");
        //程序第一次启动从消息队列头取数据
        consumer.setConsumeFromWhere(
                ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        consumer.registerMessageListener(
                new MessageListenerConcurrently() {
                    @Override
                    public ConsumeConcurrentlyStatus consumeMessage(
                            List<MessageExt> list,
                            ConsumeConcurrentlyContext context) {
                        Message msg = list.get(0);
                        System.out.println(msg.toString());
                        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                    }
                }
        );
        consumer.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #21
Source File: ConsumerFactoryBean.java    From easyooo-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	setConsumerGroup(applicationName);
	setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
	setConsumeMessageBatchMaxSize(1);
	
	setMessageModel(MessageModel.BROADCASTING);
	
	subscribe(topicName, "*");
	setMessageListener(new JvmCacheMessageListenerOrderly());
	
	start();
}
 
Example #22
Source File: ConsumerData.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public void setConsumeFromWhere(ConsumeFromWhere consumeFromWhere) {
    this.consumeFromWhere = consumeFromWhere;
}
 
Example #23
Source File: ConsumerConnection.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public ConsumeFromWhere getConsumeFromWhere() {
    return consumeFromWhere;
}
 
Example #24
Source File: ConsumerData.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public ConsumeFromWhere getConsumeFromWhere() {
    return consumeFromWhere;
}
 
Example #25
Source File: ConsumerGroupInfo.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public void setConsumeFromWhere(ConsumeFromWhere consumeFromWhere) {
    this.consumeFromWhere = consumeFromWhere;
}
 
Example #26
Source File: MqConsumer.java    From RocketMqCurrencyBoot with Apache License 2.0 4 votes vote down vote up
/**
 * 启动一个消费端    
 * @param Topic   队列名称
 * @param Tags	  标签
 * @param consumeFromWhere      从哪里开始消费
 * @param messageModel 			广播  / 聚集
 * @throws Exception    		错误消息  
 */
public String init(String Topic, String Tags, ConsumeFromWhere consumeFromWhere,
		MessageModel messageModel) throws Exception
{

	// 参数信息
	logger.info(MessageFormat
			.format("消费者 初始化!  consumerGroup={0}   namesrvAddr={1}  Topic={2}   Tags={3}  ConsumeFromWhere={4}  MessageModel={5} ",
					consumerGroup, namesrvAddr, Topic, Tags, consumeFromWhere, messageModel));

	// 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例<br>
	// 注意:ConsumerGroupName需要由应用来保证唯一
	defaultMQPushConsumer = new DefaultMQPushConsumer(consumerGroup);
	defaultMQPushConsumer.setNamesrvAddr(namesrvAddr);
	defaultMQPushConsumer.setInstanceName(String.valueOf(System.currentTimeMillis()));

	// 订阅指定MyTopic下tags等于MyTag
	if (StringUtils.isEmpty(Topic)) throw new Exception("Topic 不能为空");
	if (StringUtils.isEmpty(Tags)) { throw new Exception("Tags 不能为空"); }

	defaultMQPushConsumer.subscribe(Topic, Tags);

	// 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费<br>
	// 如果非第一次启动,那么按照上次消费的位置继续消费
	if (consumeFromWhere == null)
	{
		consumeFromWhere = consumeFromWhere.CONSUME_FROM_FIRST_OFFSET;
	}
	defaultMQPushConsumer.setConsumeFromWhere(consumeFromWhere);

	if (messageModel == null)
	{
		messageModel = messageModel.CLUSTERING;
	}

	// 设置为集群消费(区别于广播消费)
	defaultMQPushConsumer.setMessageModel(messageModel);
	defaultMQPushConsumer.registerMessageListener(defaultMessageListener);

	// Consumer对象在使用之前必须要调用start初始化,初始化一次即可<br>
	defaultMQPushConsumer.start();

	String clientID = defaultMQPushConsumer.getClientIP() + "@"
			+ defaultMQPushConsumer.getInstanceName();

	logger.info("消费者 " + clientID + "启动成功!");

	return clientID;
}
 
Example #27
Source File: DefaultMQPushConsumer.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public void setConsumeFromWhere(ConsumeFromWhere consumeFromWhere) {
    this.consumeFromWhere = consumeFromWhere;
}
 
Example #28
Source File: DefaultMQPushConsumer.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
public ConsumeFromWhere getConsumeFromWhere() {
    return consumeFromWhere;
}
 
Example #29
Source File: ConsumerMeta.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public ConsumeFromWhere getConsumeFromWhere() {
	return consumeFromWhere;
}
 
Example #30
Source File: ConsumerGroupInfo.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public ConsumeFromWhere getConsumeFromWhere() {
    return consumeFromWhere;
}