Java Code Examples for org.apache.rocketmq.common.message.Message#getTopic()

The following examples show how to use org.apache.rocketmq.common.message.Message#getTopic() . 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: DeFiBusProducerImpl.java    From DeFiBus with Apache License 2.0 6 votes vote down vote up
public void publish(final Message msg, final SendCallback sendCallback,
    final long timeout) throws MQClientException, RemotingException, InterruptedException {
    if (msg.getUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL) == null) {
        msg.putUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL, DeFiBusConstant.DEFAULT_TTL);
    }

    final AtomicReference<MessageQueue> selectorArgs = new AtomicReference<MessageQueue>();
    AsynCircuitBreakSendCallBack asynCircuitBreakSendCallBack = new AsynCircuitBreakSendCallBack();
    asynCircuitBreakSendCallBack.setMsg(msg);
    asynCircuitBreakSendCallBack.setProducer(this.deFiBusProducer);
    asynCircuitBreakSendCallBack.setSelectorArg(selectorArgs);
    asynCircuitBreakSendCallBack.setSendCallback(sendCallback);

    String topic = msg.getTopic();
    boolean hasRouteData = deFiBusProducer.getDefaultMQProducer().getDefaultMQProducerImpl().getmQClientFactory().getTopicRouteTable().containsKey(topic);
    if (!hasRouteData) {
        LOGGER.info("no topic route info for " + topic + ", send heartbeat to nameserver");
        deFiBusProducer.getDefaultMQProducer().getDefaultMQProducerImpl().getmQClientFactory().updateTopicRouteInfoFromNameServer(topic);
    }

    DeFiBusProducerImpl.this.deFiBusProducer.getDefaultMQProducer().send(msg, messageQueueSelector, selectorArgs, asynCircuitBreakSendCallBack, timeout);
}
 
Example 2
Source File: RocketMqProducer.java    From paascloud-master with Apache License 2.0 6 votes vote down vote up
private static SendResult retrySendMessage(String pid, Message msg) {
	int iniCount = 1;
	SendResult result;
	while (true) {
		try {
			result = MqProducerBeanFactory.getBean(pid).send(msg);
			break;
		} catch (Exception e) {
			log.error("发送消息失败:", e);
			if (iniCount++ >= PRODUCER_RETRY_TIMES) {
				throw new TpcBizException(ErrorCodeEnum.TPC100500014, msg.getTopic(), msg.getKeys());
			}
		}
	}
	log.info("<== 发送MQ SendResult={}", result);
	return result;
}
 
Example 3
Source File: DefaultMQProducerImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private SendResult sendSelectImpl(
    Message msg,
    MessageQueueSelector selector,
    Object arg,
    final CommunicationMode communicationMode,
    final SendCallback sendCallback, final long timeout
) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    this.makeSureStateOK();
    Validators.checkMessage(msg, this.defaultMQProducer);

    TopicPublishInfo topicPublishInfo = this.tryToFindTopicPublishInfo(msg.getTopic());
    if (topicPublishInfo != null && topicPublishInfo.ok()) {
        MessageQueue mq = null;
        try {
            mq = selector.select(topicPublishInfo.getMessageQueueList(), msg, arg);
        } catch (Throwable e) {
            throw new MQClientException("select message queue throwed exception.", e);
        }

        if (mq != null) {
            return this.sendKernelImpl(msg, mq, communicationMode, sendCallback, null, timeout);
        } else {
            throw new MQClientException("select message queue return null.", null);
        }
    }

    throw new MQClientException("No route info for this topic, " + msg.getTopic(), null);
}
 
Example 4
Source File: DefaultMQProducerImpl.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private SendResult sendSelectImpl(
    Message msg,
    MessageQueueSelector selector,
    Object arg,
    final CommunicationMode communicationMode,
    final SendCallback sendCallback, final long timeout
) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    long beginStartTime = System.currentTimeMillis();
    this.makeSureStateOK();
    Validators.checkMessage(msg, this.defaultMQProducer);

    TopicPublishInfo topicPublishInfo = this.tryToFindTopicPublishInfo(msg.getTopic());
    if (topicPublishInfo != null && topicPublishInfo.ok()) {
        MessageQueue mq = null;
        try {
            mq = selector.select(topicPublishInfo.getMessageQueueList(), msg, arg);
        } catch (Throwable e) {
            throw new MQClientException("select message queue throwed exception.", e);
        }

        long costTime = System.currentTimeMillis() - beginStartTime;
        if (timeout < costTime) {
            throw new RemotingTooMuchRequestException("sendSelectImpl call timeout");
        }
        if (mq != null) {
            return this.sendKernelImpl(msg, mq, communicationMode, sendCallback, null, timeout - costTime);
        } else {
            throw new MQClientException("select message queue return null.", null);
        }
    }

    throw new MQClientException("No route info for this topic, " + msg.getTopic(), null);
}
 
Example 5
Source File: DefaultMQProducerImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 带队列选择器的发送
 * @param msg msg
 * @param selector selector
 * @param arg arg
 * @param communicationMode 发送模型
 * @param sendCallback callback
 * @param timeout timeout
 * @return ;
 * @throws MQClientException ;
 * @throws RemotingException ;
 * @throws MQBrokerException ;
 * @throws InterruptedException ;
 */
private SendResult sendSelectImpl(
    Message msg,
    MessageQueueSelector selector,
    Object arg,
    final CommunicationMode communicationMode,
    final SendCallback sendCallback, final long timeout
) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {

    long beginStartTime = System.currentTimeMillis();
    this.makeSureStateOK();
    Validators.checkMessage(msg, this.defaultMQProducer);

    TopicPublishInfo topicPublishInfo = this.tryToFindTopicPublishInfo(msg.getTopic());
    if (topicPublishInfo != null && topicPublishInfo.ok()) {
        MessageQueue mq = null;
        try {
            mq = selector.select(topicPublishInfo.getMessageQueueList(), msg, arg);
        } catch (Throwable e) {
            throw new MQClientException("select message queue throwed exception.", e);
        }

        long costTime = System.currentTimeMillis() - beginStartTime;
        if (timeout < costTime) {
            throw new RemotingTooMuchRequestException("sendSelectImpl call timeout");
        }
        if (mq != null) {
            return this.sendKernelImpl(msg, mq, communicationMode, sendCallback, null, timeout - costTime);
        } else {
            throw new MQClientException("select message queue return null.", null);
        }
    }

    throw new MQClientException("No route info for this topic, " + msg.getTopic(), null);
}
 
Example 6
Source File: DefaultMQProducerImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private SendResult sendSelectImpl(
    Message msg,
    MessageQueueSelector selector,
    Object arg,
    final CommunicationMode communicationMode,
    final SendCallback sendCallback, final long timeout
) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    this.makeSureStateOK();
    Validators.checkMessage(msg, this.defaultMQProducer);

    TopicPublishInfo topicPublishInfo = this.tryToFindTopicPublishInfo(msg.getTopic());
    if (topicPublishInfo != null && topicPublishInfo.ok()) {
        MessageQueue mq = null;
        try {
            mq = selector.select(topicPublishInfo.getMessageQueueList(), msg, arg);
        } catch (Throwable e) {
            throw new MQClientException("select message queue throwed exception.", e);
        }

        if (mq != null) {
            return this.sendKernelImpl(msg, mq, communicationMode, sendCallback, null, timeout);
        } else {
            throw new MQClientException("select message queue return null.", null);
        }
    }

    throw new MQClientException("No route info for this topic, " + msg.getTopic(), null);
}
 
Example 7
Source File: MqMessage.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Check message message.
 *
 * @param message the message
 *
 * @return the message
 */
public static Message checkMessage(Message message) {

	String body = new String(message.getBody());
	String topic = message.getTopic();
	String key = message.getKeys();
	String tag = message.getTags();
	printCheckMessageLog(topic, key, body, tag);
	checkMessage(topic, key, body);
	return buildMessage(body, topic, tag, key);

}
 
Example 8
Source File: MqMessage.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiates a new Mq message.
 *
 * @param message the message
 */
public MqMessage(Message message) {
	this.body = new String(message.getBody());
	this.topic = message.getTopic();
	this.key = message.getKeys();
	this.tag = message.getTags();

}
 
Example 9
Source File: DefaultMQProducerImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
private SendResult sendSelectImpl(//
    Message msg, //
    MessageQueueSelector selector, //
    Object arg, //
    final CommunicationMode communicationMode, //
    final SendCallback sendCallback, final long timeout//
) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    this.makeSureStateOK();
    Validators.checkMessage(msg, this.defaultMQProducer);

    TopicPublishInfo topicPublishInfo = this.tryToFindTopicPublishInfo(msg.getTopic());
    if (topicPublishInfo != null && topicPublishInfo.ok()) {
        MessageQueue mq = null;
        try {
            mq = selector.select(topicPublishInfo.getMessageQueueList(), msg, arg);
        } catch (Throwable e) {
            throw new MQClientException("select message queue throwed exception.", e);
        }

        if (mq != null) {
            return this.sendKernelImpl(msg, mq, communicationMode, sendCallback, null, timeout);
        } else {
            throw new MQClientException("select message queue return null.", null);
        }
    }

    throw new MQClientException("No route info for this topic, " + msg.getTopic(), null);
}
 
Example 10
Source File: AbstractMQTransactionProducer.java    From rocketmq-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
public SendResult sendMessageInTransaction(Message msg, Object arg) throws MQException {
    try {
        SendResult sendResult = transactionProducer.sendMessageInTransaction(msg, arg);
        if(sendResult.getSendStatus() != SendStatus.SEND_OK) {
            log.error("事务消息发送失败,topic : {}, msgObj {}", msg.getTopic(), msg);
            throw new MQException("事务消息发送失败,topic :" + msg.getTopic() + ", status :" + sendResult.getSendStatus());
        }
        log.info("发送事务消息成功,事务id: {}", msg.getTransactionId());
        return sendResult;
    } catch (Exception e) {
        log.error("事务消息发送失败,topic : {}, msgObj {}", msg.getTopic(), msg);
        throw new MQException("事务消息发送失败,topic :" + msg.getTopic() + ",e:" + e.getMessage());
    }
}
 
Example 11
Source File: AbstractMQProducer.java    From rocketmq-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * 同步发送消息
 * @param message  消息体
 * @throws MQException 消息异常
 */
public void syncSend(Message message) throws MQException {
    try {
        SendResult sendResult = producer.send(message);
        log.debug("send rocketmq message ,messageId : {}", sendResult.getMsgId());
        this.doAfterSyncSend(message, sendResult);
    } catch (Exception e) {
        log.error("消息发送失败,topic : {}, msgObj {}", message.getTopic(), message);
        throw new MQException("消息发送失败,topic :" + message.getTopic() + ",e:" + e.getMessage());
    }
}
 
Example 12
Source File: AbstractMQProducer.java    From rocketmq-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * 同步发送消息
 * @param message  消息体
 * @param hashKey  用于hash后选择queue的key
 * @throws MQException 消息异常
 */
public void syncSendOrderly(Message message, String hashKey) throws MQException {
    if(StringUtils.isEmpty(hashKey)) {
        // fall back to normal
        syncSend(message);
    }
    try {
        SendResult sendResult = producer.send(message, messageQueueSelector, hashKey);
        log.debug("send rocketmq message orderly ,messageId : {}", sendResult.getMsgId());
        this.doAfterSyncSend(message, sendResult);
    } catch (Exception e) {
        log.error("顺序消息发送失败,topic : {}, msgObj {}", message.getTopic(), message);
        throw new MQException("顺序消息发送失败,topic :" + message.getTopic() + ",e:" + e.getMessage());
    }
}
 
Example 13
Source File: AbstractMQProducer.java    From rocketmq-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * 异步发送消息
 * @param message msgObj
 * @param sendCallback 回调
 * @throws MQException 消息异常
 */
public void asyncSend(Message message, SendCallback sendCallback) throws MQException {
    try {
        producer.send(message, sendCallback);
        log.debug("send rocketmq message async");
    } catch (Exception e) {
        log.error("消息发送失败,topic : {}, msgObj {}", message.getTopic(), message);
        throw new MQException("消息发送失败,topic :" + message.getTopic() + ",e:" + e.getMessage());
    }
}
 
Example 14
Source File: DefaultMQProducerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private SendResult sendSelectImpl(
    Message msg,
    MessageQueueSelector selector,
    Object arg,
    final CommunicationMode communicationMode,
    final SendCallback sendCallback, final long timeout
) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
    long beginStartTime = System.currentTimeMillis();
    this.makeSureStateOK();
    Validators.checkMessage(msg, this.defaultMQProducer);

    TopicPublishInfo topicPublishInfo = this.tryToFindTopicPublishInfo(msg.getTopic());
    if (topicPublishInfo != null && topicPublishInfo.ok()) {
        MessageQueue mq = null;
        try {
            List<MessageQueue> messageQueueList =
                mQClientFactory.getMQAdminImpl().parsePublishMessageQueues(topicPublishInfo.getMessageQueueList());
            Message userMessage = MessageAccessor.cloneMessage(msg);
            String userTopic = NamespaceUtil.withoutNamespace(userMessage.getTopic(), mQClientFactory.getClientConfig().getNamespace());
            userMessage.setTopic(userTopic);

            mq = mQClientFactory.getClientConfig().queueWithNamespace(selector.select(messageQueueList, userMessage, arg));
        } catch (Throwable e) {
            throw new MQClientException("select message queue throwed exception.", e);
        }

        long costTime = System.currentTimeMillis() - beginStartTime;
        if (timeout < costTime) {
            throw new RemotingTooMuchRequestException("sendSelectImpl call timeout");
        }
        if (mq != null) {
            return this.sendKernelImpl(msg, mq, communicationMode, sendCallback, null, timeout - costTime);
        } else {
            throw new MQClientException("select message queue return null.", null);
        }
    }

    validateNameServerSetting();
    throw new MQClientException("No route info for this topic, " + msg.getTopic(), null);
}
 
Example 15
Source File: DefaultMQProducerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private Message waitResponse(Message msg, long timeout, RequestResponseFuture requestResponseFuture, long cost) throws InterruptedException, RequestTimeoutException, MQClientException {
    Message responseMessage = requestResponseFuture.waitResponseMessage(timeout - cost);
    if (responseMessage == null) {
        if (requestResponseFuture.isSendRequestOk()) {
            throw new RequestTimeoutException(ClientErrorCode.REQUEST_TIMEOUT_EXCEPTION,
                "send request message to <" + msg.getTopic() + "> OK, but wait reply message timeout, " + timeout + " ms.");
        } else {
            throw new MQClientException("send request message to <" + msg.getTopic() + "> fail", requestResponseFuture.getCause());
        }
    }
    return responseMessage;
}
 
Example 16
Source File: MQClientAPIImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
private SendResult processSendResponse(
    final String brokerName,
    final Message msg,
    final RemotingCommand response
) throws MQBrokerException, RemotingCommandException {
    SendStatus sendStatus;
    switch (response.getCode()) {
        case ResponseCode.FLUSH_DISK_TIMEOUT: {
            sendStatus = SendStatus.FLUSH_DISK_TIMEOUT;
            break;
        }
        case ResponseCode.FLUSH_SLAVE_TIMEOUT: {
            sendStatus = SendStatus.FLUSH_SLAVE_TIMEOUT;
            break;
        }
        case ResponseCode.SLAVE_NOT_AVAILABLE: {
            sendStatus = SendStatus.SLAVE_NOT_AVAILABLE;
            break;
        }
        case ResponseCode.SUCCESS: {
            sendStatus = SendStatus.SEND_OK;
            break;
        }
        default: {
            throw new MQBrokerException(response.getCode(), response.getRemark());
        }
    }

    SendMessageResponseHeader responseHeader =
            (SendMessageResponseHeader) response.decodeCommandCustomHeader(SendMessageResponseHeader.class);

    //If namespace not null , reset Topic without namespace.
    String topic = msg.getTopic();
    if (StringUtils.isNotEmpty(this.clientConfig.getNamespace())) {
        topic = NamespaceUtil.withoutNamespace(topic, this.clientConfig.getNamespace());
    }

    MessageQueue messageQueue = new MessageQueue(topic, brokerName, responseHeader.getQueueId());

    String uniqMsgId = MessageClientIDSetter.getUniqID(msg);
    if (msg instanceof MessageBatch) {
        StringBuilder sb = new StringBuilder();
        for (Message message : (MessageBatch) msg) {
            sb.append(sb.length() == 0 ? "" : ",").append(MessageClientIDSetter.getUniqID(message));
        }
        uniqMsgId = sb.toString();
    }
    SendResult sendResult = new SendResult(sendStatus,
            uniqMsgId,
            responseHeader.getMsgId(), messageQueue, responseHeader.getQueueOffset());
    sendResult.setTransactionId(responseHeader.getTransactionId());
    String regionId = response.getExtFields().get(MessageConst.PROPERTY_MSG_REGION);
    String traceOn = response.getExtFields().get(MessageConst.PROPERTY_TRACE_SWITCH);
    if (regionId == null || regionId.isEmpty()) {
        regionId = MixAll.DEFAULT_TRACE_REGION_ID;
    }
    if (traceOn != null && traceOn.equals("false")) {
        sendResult.setTraceOn(false);
    } else {
        sendResult.setTraceOn(true);
    }
    sendResult.setRegionId(regionId);
    return sendResult;
}