Search in sources :

Example 1 with RequestCallback

use of org.apache.rocketmq.client.producer.RequestCallback in project rocketmq by apache.

the class AsyncRequestProducer method main.

public static void main(String[] args) throws MQClientException, InterruptedException {
    String producerGroup = "please_rename_unique_group_name";
    String topic = "RequestTopic";
    long ttl = 3000;
    DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
    producer.start();
    try {
        Message msg = new Message(topic, "", "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
        long begin = System.currentTimeMillis();
        producer.request(msg, new RequestCallback() {

            @Override
            public void onSuccess(Message message) {
                long cost = System.currentTimeMillis() - begin;
                System.out.printf("request to <%s> cost: %d replyMessage: %s %n", topic, cost, message);
            }

            @Override
            public void onException(Throwable e) {
                System.err.printf("request to <%s> fail.", topic);
            }
        }, ttl);
    } catch (Exception e) {
        log.warn("", e);
    }
/* shutdown after your request callback is finished */
// producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) RequestCallback(org.apache.rocketmq.client.producer.RequestCallback) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 2 with RequestCallback

use of org.apache.rocketmq.client.producer.RequestCallback in project incubator-eventmesh by apache.

the class ProducerImpl method rrCallbackConvert.

private RequestCallback rrCallbackConvert(final Message message, final RequestReplyCallback rrCallback) {
    return new RequestCallback() {

        @Override
        public void onSuccess(org.apache.rocketmq.common.message.Message message) {
            // clean the message property to lowercase
            for (String sysPropKey : MessageConst.STRING_HASH_SET) {
                if (StringUtils.isNotEmpty(message.getProperty(sysPropKey))) {
                    String prop = message.getProperty(sysPropKey);
                    String tmpPropKey = sysPropKey.toLowerCase().replaceAll("_", Constants.MESSAGE_PROP_SEPARATOR);
                    MessageAccessor.putProperty(message, tmpPropKey, prop);
                    message.getProperties().remove(sysPropKey);
                }
            }
            CloudEvent event = RocketMQMessageFactory.createReader(message).toEvent();
            rrCallback.onSuccess(event);
        }

        @Override
        public void onException(Throwable e) {
            String topic = message.getTopic();
            ConnectorRuntimeException onsEx = ProducerImpl.this.checkProducerException(topic, null, e);
            OnExceptionContext context = new OnExceptionContext();
            context.setTopic(topic);
            context.setException(onsEx);
            rrCallback.onException(e);
        }
    };
}
Also used : ConnectorRuntimeException(org.apache.eventmesh.api.exception.ConnectorRuntimeException) RequestCallback(org.apache.rocketmq.client.producer.RequestCallback) Message(org.apache.rocketmq.common.message.Message) OnExceptionContext(org.apache.eventmesh.api.exception.OnExceptionContext) CloudEvent(io.cloudevents.CloudEvent)

Example 3 with RequestCallback

use of org.apache.rocketmq.client.producer.RequestCallback in project rocketmq-spring by apache.

the class RocketMQTemplate method sendAndReceive.

/**
 * Send request message in asynchronous mode. </p> This method returns immediately. On receiving reply message,
 * <code>rocketMQLocalRequestCallback</code> will be executed. </p>
 *
 * @param destination formats: `topicName:tags`
 * @param message {@link org.springframework.messaging.Message} the message to be sent.
 * @param rocketMQLocalRequestCallback callback that will invoked when reply message received.
 * @param hashKey needed when sending message orderly
 * @param timeout send timeout in millis
 * @param delayLevel message delay level(0 means no delay)
 * @return
 */
public void sendAndReceive(String destination, Message<?> message, RocketMQLocalRequestCallback rocketMQLocalRequestCallback, String hashKey, long timeout, int delayLevel) {
    if (Objects.isNull(message) || Objects.isNull(message.getPayload())) {
        log.error("send request message failed. destination:{}, message is null ", destination);
        throw new IllegalArgumentException("`message` and `message.payload` cannot be null");
    }
    try {
        org.apache.rocketmq.common.message.Message rocketMsg = this.createRocketMqMessage(destination, message);
        if (delayLevel > 0) {
            rocketMsg.setDelayTimeLevel(delayLevel);
        }
        if (timeout <= 0) {
            timeout = producer.getSendMsgTimeout();
        }
        RequestCallback requestCallback = null;
        if (rocketMQLocalRequestCallback != null) {
            requestCallback = new RequestCallback() {

                @Override
                public void onSuccess(org.apache.rocketmq.common.message.Message message) {
                    rocketMQLocalRequestCallback.onSuccess(doConvertMessage((MessageExt) message, getMessageType(rocketMQLocalRequestCallback)));
                }

                @Override
                public void onException(Throwable e) {
                    rocketMQLocalRequestCallback.onException(e);
                }
            };
        }
        if (Objects.isNull(hashKey) || hashKey.isEmpty()) {
            producer.request(rocketMsg, requestCallback, timeout);
        } else {
            producer.request(rocketMsg, messageQueueSelector, hashKey, requestCallback, timeout);
        }
    } catch (Exception e) {
        log.error("send request message failed. destination:{}, message:{} ", destination, message);
        throw new MessagingException(e.getMessage(), e);
    }
}
Also used : RequestCallback(org.apache.rocketmq.client.producer.RequestCallback) MessagingException(org.springframework.messaging.MessagingException) MessagingException(org.springframework.messaging.MessagingException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 4 with RequestCallback

use of org.apache.rocketmq.client.producer.RequestCallback in project rocketmq-comments by HScarb.

the class AsyncRequestProducer method main.

public static void main(String[] args) throws MQClientException, InterruptedException {
    String producerGroup = "please_rename_unique_group_name";
    String topic = "RequestTopic";
    long ttl = 3000;
    DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
    producer.start();
    try {
        Message msg = new Message(topic, "", "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
        long begin = System.currentTimeMillis();
        producer.request(msg, new RequestCallback() {

            @Override
            public void onSuccess(Message message) {
                long cost = System.currentTimeMillis() - begin;
                System.out.printf("request to <%s> cost: %d replyMessage: %s %n", topic, cost, message);
            }

            @Override
            public void onException(Throwable e) {
                System.err.printf("request to <%s> fail.", topic);
            }
        }, ttl);
    } catch (Exception e) {
        log.warn("", e);
    }
/* shutdown after your request callback is finished */
// producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) RequestCallback(org.apache.rocketmq.client.producer.RequestCallback) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

RequestCallback (org.apache.rocketmq.client.producer.RequestCallback)4 MQClientException (org.apache.rocketmq.client.exception.MQClientException)3 Message (org.apache.rocketmq.common.message.Message)3 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)2 CloudEvent (io.cloudevents.CloudEvent)1 ConnectorRuntimeException (org.apache.eventmesh.api.exception.ConnectorRuntimeException)1 OnExceptionContext (org.apache.eventmesh.api.exception.OnExceptionContext)1 MessagingException (org.springframework.messaging.MessagingException)1