Search in sources :

Example 1 with MQMessage

use of com.jeesuite.amqp.MQMessage in project jeesuite-libs by vakinge.

the class CMQConsumerAdapter method handleMessageConsumed.

@Override
public String handleMessageConsumed(MQMessage message) {
    try {
        Message originMessage = message.getOriginMessage(Message.class);
        String receiptHandle = originMessage.receiptHandle;
        CMQManager.getQueue().deleteMessage(receiptHandle);
    } catch (Exception e) {
        logger.error("", e);
    }
    return null;
}
Also used : MQMessage(com.jeesuite.amqp.MQMessage) Message(com.qcloud.cmq.Message)

Example 2 with MQMessage

use of com.jeesuite.amqp.MQMessage in project jeesuite-libs by vakinge.

the class RocketProducerAdapter method sendMessage.

@Override
public String sendMessage(MQMessage message, boolean async) {
    Message _message = new Message(message.getTopic(), message.getTag(), message.getBizKey(), message.bodyAsBytes());
    if (StringUtils.isNotBlank(message.getProduceBy())) {
        _message.putUserProperty(MessageHeaderNames.produceBy.name(), message.getProduceBy());
    }
    if (StringUtils.isNotBlank(message.getRequestId())) {
        _message.putUserProperty(MessageHeaderNames.requestId.name(), message.getRequestId());
    }
    if (StringUtils.isNotBlank(message.getTenantId())) {
        _message.putUserProperty(MessageHeaderNames.tenantId.name(), message.getTenantId());
    }
    if (StringUtils.isNotBlank(message.getCheckUrl())) {
        _message.putUserProperty(MessageHeaderNames.checkUrl.name(), message.getProduceBy());
    }
    if (StringUtils.isNotBlank(message.getTransactionId())) {
        _message.putUserProperty(MessageHeaderNames.transactionId.name(), message.getTransactionId());
    }
    try {
        if (async) {
            producer.send(_message, new SendCallback() {

                @Override
                public void onSuccess(SendResult sendResult) {
                    if (logger.isDebugEnabled())
                        logger.debug("MQ_SEND_SUCCESS:{} -> msgId:{},status:{},offset:{}", message.getTopic(), sendResult.getMsgId(), sendResult.getSendStatus().name(), sendResult.getQueueOffset());
                    message.setMsgId(sendResult.getMsgId());
                    handleSuccess(message);
                }

                @Override
                public void onException(Throwable e) {
                    handleError(message, e);
                    logger.warn("MQ_SEND_FAIL:" + message.getTopic(), e);
                }
            });
        } else {
            SendResult sendResult = producer.send(_message);
            message.setMsgId(sendResult.getMsgId());
            if (sendResult.getSendStatus() == SendStatus.SEND_OK) {
                handleSuccess(message);
            } else {
                handleError(message, new MQClientException(0, sendResult.getSendStatus().name()));
            }
        }
    } catch (Exception e) {
        handleError(message, e);
        logger.warn("MQ_SEND_FAIL:" + message.getTopic(), e);
    }
    return null;
}
Also used : Message(org.apache.rocketmq.common.message.Message) MQMessage(com.jeesuite.amqp.MQMessage) SendResult(org.apache.rocketmq.client.producer.SendResult) SendCallback(org.apache.rocketmq.client.producer.SendCallback) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 3 with MQMessage

use of com.jeesuite.amqp.MQMessage in project jeesuite-libs by vakinge.

the class KafkaConsumerAdapter method fetchMessages.

@Override
public List<MQMessage> fetchMessages() {
    // 手动提交offset
    trySubmitOffsets();
    ConsumerRecords<String, String> records = kafkaConsumer.poll(timeoutDuration);
    Iterator<ConsumerRecord<String, String>> iterator = records.iterator();
    List<MQMessage> result = new ArrayList<>(records.count());
    MQMessage message;
    ConsumerRecord<String, String> item;
    while (iterator.hasNext()) {
        item = iterator.next();
        message = new MQMessage(item.topic(), item.value());
        message.setOriginMessage(item);
        result.add(message);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) MQMessage(com.jeesuite.amqp.MQMessage) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Example 4 with MQMessage

use of com.jeesuite.amqp.MQMessage in project jeesuite-libs by vakinge.

the class MNSProducer method publishMessage.

public String publishMessage(String topicName, Object data) {
    CloudTopic topic = getTopic(topicName);
    TopicMessage tMessage = new RawTopicMessage();
    tMessage.setBaseMessageBody(new MQMessage(topicName, data).toMessageValue(true));
    topic.publishMessage(tMessage);
    return tMessage.getMessageId();
}
Also used : CloudTopic(com.aliyun.mns.client.CloudTopic) RawTopicMessage(com.aliyun.mns.model.RawTopicMessage) TopicMessage(com.aliyun.mns.model.TopicMessage) RawTopicMessage(com.aliyun.mns.model.RawTopicMessage) MQMessage(com.jeesuite.amqp.MQMessage)

Aggregations

MQMessage (com.jeesuite.amqp.MQMessage)4 CloudTopic (com.aliyun.mns.client.CloudTopic)1 RawTopicMessage (com.aliyun.mns.model.RawTopicMessage)1 TopicMessage (com.aliyun.mns.model.TopicMessage)1 Message (com.qcloud.cmq.Message)1 ArrayList (java.util.ArrayList)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 MQClientException (org.apache.rocketmq.client.exception.MQClientException)1 SendCallback (org.apache.rocketmq.client.producer.SendCallback)1 SendResult (org.apache.rocketmq.client.producer.SendResult)1 Message (org.apache.rocketmq.common.message.Message)1