Search in sources :

Example 56 with MQClientException

use of org.apache.rocketmq.client.exception.MQClientException in project rocketmq-externals by apache.

the class ConsumerTest method main.

public static void main(String[] args) {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("g00003");
    consumer.setNamesrvAddr("localhost:9876");
    try {
        consumer.subscribe("flink-sink2", "*");
    } catch (MQClientException e) {
        e.printStackTrace();
    }
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            for (MessageExt msg : msgs) {
                System.out.println(msg.getKeys() + ":" + new String(msg.getBody()));
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    try {
        consumer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
    }
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 57 with MQClientException

use of org.apache.rocketmq.client.exception.MQClientException in project rocketmq-externals by apache.

the class ProducerTest method main.

public static void main(String[] args) {
    DefaultMQProducer producer = new DefaultMQProducer("p001");
    producer.setNamesrvAddr("localhost:9876");
    try {
        producer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
    }
    for (int i = 0; i < 10000; i++) {
        Message msg = new Message("flink-source2", "", "id_" + i, ("country_X province_" + i).getBytes());
        try {
            producer.send(msg);
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("send " + i);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 58 with MQClientException

use of org.apache.rocketmq.client.exception.MQClientException in project rocketmq-externals by apache.

the class RocketMQSource method doStart.

@Override
protected void doStart() throws FlumeException {
    consumer = new DefaultMQPullConsumer(consumerGroup);
    consumer.setNamesrvAddr(nameServer);
    consumer.setMessageModel(MessageModel.valueOf(messageModel));
    consumer.registerMessageQueueListener(topic, null);
    try {
        consumer.start();
    } catch (MQClientException e) {
        log.error("RocketMQ consumer start failed", e);
        throw new FlumeException("Failed to start RocketMQ consumer", e);
    }
    sourceCounter.start();
}
Also used : FlumeException(org.apache.flume.FlumeException) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 59 with MQClientException

use of org.apache.rocketmq.client.exception.MQClientException in project rocketmq by apache.

the class PullConsumerImpl method startup.

@Override
public synchronized void startup() {
    if (!started) {
        try {
            registerPullTaskCallback();
            this.pullConsumerScheduleService.start();
            this.localMessageCache.startup();
        } catch (MQClientException e) {
            throw new OMSRuntimeException("-1", e);
        }
    }
    this.started = true;
}
Also used : OMSRuntimeException(io.openmessaging.exception.OMSRuntimeException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 60 with MQClientException

use of org.apache.rocketmq.client.exception.MQClientException in project rocketmq by apache.

the class RebalancePushImpl method computePullFromWhere.

@Override
public long computePullFromWhere(MessageQueue mq) {
    long result = -1;
    final ConsumeFromWhere consumeFromWhere = this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer().getConsumeFromWhere();
    final OffsetStore offsetStore = this.defaultMQPushConsumerImpl.getOffsetStore();
    switch(consumeFromWhere) {
        case CONSUME_FROM_LAST_OFFSET_AND_FROM_MIN_WHEN_BOOT_FIRST:
        case CONSUME_FROM_MIN_OFFSET:
        case CONSUME_FROM_MAX_OFFSET:
        case CONSUME_FROM_LAST_OFFSET:
            {
                long lastOffset = offsetStore.readOffset(mq, ReadOffsetType.READ_FROM_STORE);
                if (lastOffset >= 0) {
                    result = lastOffset;
                } else // First start,no offset
                if (-1 == lastOffset) {
                    if (mq.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                        result = 0L;
                    } else {
                        try {
                            result = this.mQClientFactory.getMQAdminImpl().maxOffset(mq);
                        } catch (MQClientException e) {
                            result = -1;
                        }
                    }
                } else {
                    result = -1;
                }
                break;
            }
        case CONSUME_FROM_FIRST_OFFSET:
            {
                long lastOffset = offsetStore.readOffset(mq, ReadOffsetType.READ_FROM_STORE);
                if (lastOffset >= 0) {
                    result = lastOffset;
                } else if (-1 == lastOffset) {
                    result = 0L;
                } else {
                    result = -1;
                }
                break;
            }
        case CONSUME_FROM_TIMESTAMP:
            {
                long lastOffset = offsetStore.readOffset(mq, ReadOffsetType.READ_FROM_STORE);
                if (lastOffset >= 0) {
                    result = lastOffset;
                } else if (-1 == lastOffset) {
                    if (mq.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                        try {
                            result = this.mQClientFactory.getMQAdminImpl().maxOffset(mq);
                        } catch (MQClientException e) {
                            result = -1;
                        }
                    } else {
                        try {
                            long timestamp = UtilAll.parseDate(this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer().getConsumeTimestamp(), UtilAll.YYYYMMDDHHMMSS).getTime();
                            result = this.mQClientFactory.getMQAdminImpl().searchOffset(mq, timestamp);
                        } catch (MQClientException e) {
                            result = -1;
                        }
                    }
                } else {
                    result = -1;
                }
                break;
            }
        default:
            break;
    }
    return result;
}
Also used : MQClientException(org.apache.rocketmq.client.exception.MQClientException) ConsumeFromWhere(org.apache.rocketmq.common.consumer.ConsumeFromWhere) OffsetStore(org.apache.rocketmq.client.consumer.store.OffsetStore)

Aggregations

MQClientException (org.apache.rocketmq.client.exception.MQClientException)230 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)70 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)69 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)65 Message (org.apache.rocketmq.common.message.Message)35 SendResult (org.apache.rocketmq.client.producer.SendResult)34 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)29 MessageExt (org.apache.rocketmq.common.message.MessageExt)25 UnsupportedEncodingException (java.io.UnsupportedEncodingException)18 SubscriptionData (org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData)18 DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)17 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)17 RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)17 PullResult (org.apache.rocketmq.client.consumer.PullResult)16 TopicList (org.apache.rocketmq.common.protocol.body.TopicList)16 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)15 TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)15 BrokerData (org.apache.rocketmq.common.protocol.route.BrokerData)14 ConsumeConcurrentlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)11 ConsumeConcurrentlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)11