Search in sources :

Example 26 with RemotingException

use of org.apache.rocketmq.remoting.exception.RemotingException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class Producer method main.

public static void main(String[] args) throws UnsupportedEncodingException {
    try {
        MQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
        producer.start();
        String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD", "TagE" };
        for (int i = 0; i < 100; i++) {
            int orderId = i % 10;
            Message msg = new Message("TopicTestjjj", tags[i % tags.length], "KEY" + i, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.send(msg, new MessageQueueSelector() {

                @Override
                public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                    Integer id = (Integer) arg;
                    int index = id % mqs.size();
                    return mqs.get(index);
                }
            }, orderId);
            System.out.printf("%s%n", sendResult);
        }
        producer.shutdown();
    } catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQProducer(org.apache.rocketmq.client.producer.MQProducer) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) SendResult(org.apache.rocketmq.client.producer.SendResult) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 27 with RemotingException

use of org.apache.rocketmq.remoting.exception.RemotingException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class ProducerImplTest method testSend_Not_OK.

@Test
public void testSend_Not_OK() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    SendResult sendResult = new SendResult();
    sendResult.setSendStatus(SendStatus.FLUSH_DISK_TIMEOUT);
    when(rocketmqProducer.send(any(Message.class), anyLong())).thenReturn(sendResult);
    try {
        producer.send(producer.createBytesMessageToTopic("HELLO_TOPIC", new byte[] { 'a' }));
        failBecauseExceptionWasNotThrown(OMSRuntimeException.class);
    } catch (Exception e) {
        assertThat(e).hasMessageContaining("Send message to RocketMQ broker failed.");
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) OMSRuntimeException(io.openmessaging.exception.OMSRuntimeException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) Test(org.junit.Test)

Example 28 with RemotingException

use of org.apache.rocketmq.remoting.exception.RemotingException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultMQProducerImpl method sendKernelImpl.

private // 
SendResult sendKernelImpl(// 
final Message msg, // 
final MessageQueue mq, // 
final CommunicationMode communicationMode, // 
final SendCallback sendCallback, // 
final TopicPublishInfo topicPublishInfo, final long timeout) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    // 获取 broker地址
    String brokerAddr = this.mQClientFactory.findBrokerAddressInPublish(mq.getBrokerName());
    if (null == brokerAddr) {
        tryToFindTopicPublishInfo(mq.getTopic());
        brokerAddr = this.mQClientFactory.findBrokerAddressInPublish(mq.getBrokerName());
    }
    SendMessageContext context = null;
    if (brokerAddr != null) {
        brokerAddr = MixAll.brokerVIPChannel(this.defaultMQProducer.isSendMessageWithVIPChannel(), brokerAddr);
        byte[] prevBody = msg.getBody();
        try {
            // for MessageBatch,ID has been set in the generating process
            if (!(msg instanceof MessageBatch)) {
                // 设置设置UNIQ_id,所以当看见msgId的时候为什么解析不一样了懂了吧
                MessageClientIDSetter.setUniqID(msg);
            }
            // 又是根据位来进行每位是啥的判断
            int sysFlag = 0;
            if (this.tryToCompressMessage(msg)) {
                sysFlag |= MessageSysFlag.COMPRESSED_FLAG;
            }
            // 根据事务属性key获取值看是否是事务消息
            final String tranMsg = msg.getProperty(MessageConst.PROPERTY_TRANSACTION_PREPARED);
            if (tranMsg != null && Boolean.parseBoolean(tranMsg)) {
                sysFlag |= MessageSysFlag.TRANSACTION_PREPARED_TYPE;
            }
            if (hasCheckForbiddenHook()) {
                CheckForbiddenContext checkForbiddenContext = new CheckForbiddenContext();
                checkForbiddenContext.setNameSrvAddr(this.defaultMQProducer.getNamesrvAddr());
                checkForbiddenContext.setGroup(this.defaultMQProducer.getProducerGroup());
                checkForbiddenContext.setCommunicationMode(communicationMode);
                checkForbiddenContext.setBrokerAddr(brokerAddr);
                checkForbiddenContext.setMessage(msg);
                checkForbiddenContext.setMq(mq);
                checkForbiddenContext.setUnitMode(this.isUnitMode());
                this.executeCheckForbiddenHook(checkForbiddenContext);
            }
            if (this.hasSendMessageHook()) {
                context = new SendMessageContext();
                context.setProducer(this);
                context.setProducerGroup(this.defaultMQProducer.getProducerGroup());
                context.setCommunicationMode(communicationMode);
                context.setBornHost(this.defaultMQProducer.getClientIP());
                context.setBrokerAddr(brokerAddr);
                context.setMessage(msg);
                context.setMq(mq);
                String isTrans = msg.getProperty(MessageConst.PROPERTY_TRANSACTION_PREPARED);
                if (isTrans != null && isTrans.equals("true")) {
                    context.setMsgType(MessageType.Trans_Msg_Half);
                }
                if (msg.getProperty("__STARTDELIVERTIME") != null || msg.getProperty(MessageConst.PROPERTY_DELAY_TIME_LEVEL) != null) {
                    context.setMsgType(MessageType.Delay_Msg);
                }
                this.executeSendMessageHookBefore(context);
            }
            // 构建SendMessageRequestHeader
            SendMessageRequestHeader requestHeader = new SendMessageRequestHeader();
            requestHeader.setProducerGroup(this.defaultMQProducer.getProducerGroup());
            requestHeader.setTopic(msg.getTopic());
            requestHeader.setDefaultTopic(this.defaultMQProducer.getCreateTopicKey());
            requestHeader.setDefaultTopicQueueNums(this.defaultMQProducer.getDefaultTopicQueueNums());
            requestHeader.setQueueId(mq.getQueueId());
            requestHeader.setSysFlag(sysFlag);
            requestHeader.setBornTimestamp(System.currentTimeMillis());
            requestHeader.setFlag(msg.getFlag());
            requestHeader.setProperties(MessageDecoder.messageProperties2String(msg.getProperties()));
            requestHeader.setReconsumeTimes(0);
            requestHeader.setUnitMode(this.isUnitMode());
            requestHeader.setBatch(msg instanceof MessageBatch);
            if (requestHeader.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
                String reconsumeTimes = MessageAccessor.getReconsumeTime(msg);
                if (reconsumeTimes != null) {
                    requestHeader.setReconsumeTimes(Integer.valueOf(reconsumeTimes));
                    MessageAccessor.clearProperty(msg, MessageConst.PROPERTY_RECONSUME_TIME);
                }
                String maxReconsumeTimes = MessageAccessor.getMaxReconsumeTimes(msg);
                if (maxReconsumeTimes != null) {
                    requestHeader.setMaxReconsumeTimes(Integer.valueOf(maxReconsumeTimes));
                    MessageAccessor.clearProperty(msg, MessageConst.PROPERTY_MAX_RECONSUME_TIMES);
                }
            }
            SendResult sendResult = null;
            switch(communicationMode) {
                case ASYNC:
                    sendResult = // 异步发送消息
                    this.mQClientFactory.getMQClientAPIImpl().sendMessage(// 1
                    brokerAddr, // 2
                    mq.getBrokerName(), // 3
                    msg, // 4
                    requestHeader, // 5
                    timeout, // 6
                    communicationMode, // 7
                    sendCallback, // 8
                    topicPublishInfo, // 9
                    this.mQClientFactory, // 10
                    this.defaultMQProducer.getRetryTimesWhenSendAsyncFailed(), // 
                    context, this);
                    break;
                case ONEWAY:
                case SYNC:
                    sendResult = // 同步以及广播发送消息
                    this.mQClientFactory.getMQClientAPIImpl().sendMessage(brokerAddr, mq.getBrokerName(), msg, requestHeader, timeout, communicationMode, context, this);
                    break;
                default:
                    assert false;
                    break;
            }
            if (this.hasSendMessageHook()) {
                context.setSendResult(sendResult);
                // 发送消息后逻辑
                this.executeSendMessageHookAfter(context);
            }
            return sendResult;
        } catch (RemotingException e) {
            if (this.hasSendMessageHook()) {
                context.setException(e);
                this.executeSendMessageHookAfter(context);
            }
            throw e;
        } catch (MQBrokerException e) {
            if (this.hasSendMessageHook()) {
                context.setException(e);
                this.executeSendMessageHookAfter(context);
            }
            throw e;
        } catch (InterruptedException e) {
            if (this.hasSendMessageHook()) {
                context.setException(e);
                this.executeSendMessageHookAfter(context);
            }
            throw e;
        } finally {
            msg.setBody(prevBody);
        }
    }
    throw new MQClientException("The broker[" + mq.getBrokerName() + "] not exist", null);
}
Also used : MessageBatch(org.apache.rocketmq.common.message.MessageBatch) CheckForbiddenContext(org.apache.rocketmq.client.hook.CheckForbiddenContext) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) TransactionSendResult(org.apache.rocketmq.client.producer.TransactionSendResult) SendResult(org.apache.rocketmq.client.producer.SendResult) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 29 with RemotingException

use of org.apache.rocketmq.remoting.exception.RemotingException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class RemoteBrokerOffsetStoreTest method testReadOffset_WithException.

@Test
public void testReadOffset_WithException() throws Exception {
    OffsetStore offsetStore = new RemoteBrokerOffsetStore(mQClientFactory, group);
    MessageQueue messageQueue = new MessageQueue(topic, brokerName, 2);
    offsetStore.updateOffset(messageQueue, 1024, false);
    doThrow(new MQBrokerException(-1, "")).when(mqClientAPI).queryConsumerOffset(anyString(), any(QueryConsumerOffsetRequestHeader.class), anyLong());
    assertThat(offsetStore.readOffset(messageQueue, ReadOffsetType.READ_FROM_STORE)).isEqualTo(-1);
    doThrow(new RemotingException("", null)).when(mqClientAPI).queryConsumerOffset(anyString(), any(QueryConsumerOffsetRequestHeader.class), anyLong());
    assertThat(offsetStore.readOffset(messageQueue, ReadOffsetType.READ_FROM_STORE)).isEqualTo(-2);
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) QueryConsumerOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.QueryConsumerOffsetRequestHeader) Test(org.junit.Test)

Example 30 with RemotingException

use of org.apache.rocketmq.remoting.exception.RemotingException in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class MQClientAPIImplTest method testSendMessageAsync_WithException.

@Test
public void testSendMessageAsync_WithException() throws RemotingException, InterruptedException, MQBrokerException {
    doThrow(new RemotingTimeoutException("Remoting Exception in Test")).when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(RemotingException.class);
    } catch (RemotingException e) {
        assertThat(e).hasMessage("Remoting Exception in Test");
    }
    doThrow(new InterruptedException("Interrupted Exception in Test")).when(remotingClient).invokeAsync(anyString(), any(RemotingCommand.class), anyLong(), any(InvokeCallback.class));
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ASYNC, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(InterruptedException.class);
    } catch (InterruptedException e) {
        assertThat(e).hasMessage("Interrupted Exception in Test");
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) InvokeCallback(org.apache.rocketmq.remoting.InvokeCallback) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) Test(org.junit.Test)

Aggregations

RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)45 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)39 MQClientException (org.apache.rocketmq.client.exception.MQClientException)39 SendResult (org.apache.rocketmq.client.producer.SendResult)13 Message (org.apache.rocketmq.common.message.Message)13 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)9 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)8 Test (org.junit.Test)8 RemotingCommandException (org.apache.rocketmq.remoting.exception.RemotingCommandException)7 RemotingSendRequestException (org.apache.rocketmq.remoting.exception.RemotingSendRequestException)7 IOException (java.io.IOException)6 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)6 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)6 SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)6 SubscriptionData (org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData)6 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)6 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)6 MessageExt (org.apache.rocketmq.common.message.MessageExt)5