Search in sources :

Example 1 with SendResult

use of io.openmessaging.SendResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class ProducerImpl method sendAsync.

private Promise<SendResult> sendAsync(final Message message, long timeout) {
    checkMessageType(message);
    org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message);
    final Promise<SendResult> promise = new DefaultPromise<>();
    try {
        this.rocketmqProducer.send(rmqMessage, new SendCallback() {

            @Override
            public void onSuccess(final org.apache.rocketmq.client.producer.SendResult rmqResult) {
                message.headers().put(MessageHeader.MESSAGE_ID, rmqResult.getMsgId());
                promise.set(OMSUtil.sendResultConvert(rmqResult));
            }

            @Override
            public void onException(final Throwable e) {
                promise.setFailure(e);
            }
        }, timeout);
    } catch (Exception e) {
        promise.setFailure(e);
    }
    return promise;
}
Also used : OMSRuntimeException(io.openmessaging.exception.OMSRuntimeException) DefaultPromise(io.openmessaging.rocketmq.promise.DefaultPromise) SendResult(io.openmessaging.SendResult) SendCallback(org.apache.rocketmq.client.producer.SendCallback)

Example 2 with SendResult

use of io.openmessaging.SendResult in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class SimpleProducer method main.

public static void main(String[] args) {
    final MessagingAccessPoint messagingAccessPoint = MessagingAccessPointFactory.getMessagingAccessPoint("openmessaging:rocketmq://IP1:9876,IP2:9876/namespace");
    final Producer producer = messagingAccessPoint.createProducer();
    messagingAccessPoint.startup();
    System.out.printf("MessagingAccessPoint startup OK%n");
    producer.startup();
    System.out.printf("Producer startup OK%n");
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        @Override
        public void run() {
            producer.shutdown();
            messagingAccessPoint.shutdown();
        }
    }));
    {
        Message message = producer.createBytesMessageToTopic("OMS_HELLO_TOPIC", "OMS_HELLO_BODY".getBytes(Charset.forName("UTF-8")));
        SendResult sendResult = producer.send(message);
        // final Void aVoid = result.get(3000L);
        System.out.printf("Send async message OK, msgId: %s%n", sendResult.messageId());
    }
    {
        final Promise<SendResult> result = producer.sendAsync(producer.createBytesMessageToTopic("OMS_HELLO_TOPIC", "OMS_HELLO_BODY".getBytes(Charset.forName("UTF-8"))));
        result.addListener(new PromiseListener<SendResult>() {

            @Override
            public void operationCompleted(Promise<SendResult> promise) {
                System.out.printf("Send async message OK, msgId: %s%n", promise.get().messageId());
            }

            @Override
            public void operationFailed(Promise<SendResult> promise) {
                System.out.printf("Send async message Failed, error: %s%n", promise.getThrowable().getMessage());
            }
        });
    }
    {
        producer.sendOneway(producer.createBytesMessageToTopic("OMS_HELLO_TOPIC", "OMS_HELLO_BODY".getBytes(Charset.forName("UTF-8"))));
        System.out.printf("Send oneway message OK%n");
    }
}
Also used : Promise(io.openmessaging.Promise) Producer(io.openmessaging.Producer) Message(io.openmessaging.Message) PromiseListener(io.openmessaging.PromiseListener) SendResult(io.openmessaging.SendResult) MessagingAccessPoint(io.openmessaging.MessagingAccessPoint)

Example 3 with SendResult

use of io.openmessaging.SendResult in project rocketmq by apache.

the class SimpleProducer method main.

public static void main(String[] args) {
    final MessagingAccessPoint messagingAccessPoint = MessagingAccessPointFactory.getMessagingAccessPoint("openmessaging:rocketmq://IP1:9876,IP2:9876/namespace");
    final Producer producer = messagingAccessPoint.createProducer();
    messagingAccessPoint.startup();
    System.out.printf("MessagingAccessPoint startup OK%n");
    producer.startup();
    System.out.printf("Producer startup OK%n");
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        @Override
        public void run() {
            producer.shutdown();
            messagingAccessPoint.shutdown();
        }
    }));
    {
        Message message = producer.createBytesMessageToTopic("OMS_HELLO_TOPIC", "OMS_HELLO_BODY".getBytes(Charset.forName("UTF-8")));
        SendResult sendResult = producer.send(message);
        // final Void aVoid = result.get(3000L);
        System.out.printf("Send async message OK, msgId: %s%n", sendResult.messageId());
    }
    {
        final Promise<SendResult> result = producer.sendAsync(producer.createBytesMessageToTopic("OMS_HELLO_TOPIC", "OMS_HELLO_BODY".getBytes(Charset.forName("UTF-8"))));
        result.addListener(new PromiseListener<SendResult>() {

            @Override
            public void operationCompleted(Promise<SendResult> promise) {
                System.out.printf("Send async message OK, msgId: %s%n", promise.get().messageId());
            }

            @Override
            public void operationFailed(Promise<SendResult> promise) {
                System.out.printf("Send async message Failed, error: %s%n", promise.getThrowable().getMessage());
            }
        });
    }
    {
        producer.sendOneway(producer.createBytesMessageToTopic("OMS_HELLO_TOPIC", "OMS_HELLO_BODY".getBytes(Charset.forName("UTF-8"))));
        System.out.printf("Send oneway message OK%n");
    }
}
Also used : Promise(io.openmessaging.Promise) Producer(io.openmessaging.Producer) Message(io.openmessaging.Message) PromiseListener(io.openmessaging.PromiseListener) SendResult(io.openmessaging.SendResult) MessagingAccessPoint(io.openmessaging.MessagingAccessPoint)

Example 4 with SendResult

use of io.openmessaging.SendResult in project rocketmq by apache.

the class ProducerImpl method sendAsync.

private Promise<SendResult> sendAsync(final Message message, long timeout) {
    checkMessageType(message);
    org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message);
    final Promise<SendResult> promise = new DefaultPromise<>();
    try {
        this.rocketmqProducer.send(rmqMessage, new SendCallback() {

            @Override
            public void onSuccess(final org.apache.rocketmq.client.producer.SendResult rmqResult) {
                message.headers().put(MessageHeader.MESSAGE_ID, rmqResult.getMsgId());
                promise.set(OMSUtil.sendResultConvert(rmqResult));
            }

            @Override
            public void onException(final Throwable e) {
                promise.setFailure(e);
            }
        }, timeout);
    } catch (Exception e) {
        promise.setFailure(e);
    }
    return promise;
}
Also used : OMSRuntimeException(io.openmessaging.exception.OMSRuntimeException) DefaultPromise(io.openmessaging.rocketmq.promise.DefaultPromise) SendResult(io.openmessaging.SendResult) SendCallback(org.apache.rocketmq.client.producer.SendCallback)

Aggregations

SendResult (io.openmessaging.SendResult)4 Message (io.openmessaging.Message)2 MessagingAccessPoint (io.openmessaging.MessagingAccessPoint)2 Producer (io.openmessaging.Producer)2 Promise (io.openmessaging.Promise)2 PromiseListener (io.openmessaging.PromiseListener)2 OMSRuntimeException (io.openmessaging.exception.OMSRuntimeException)2 DefaultPromise (io.openmessaging.rocketmq.promise.DefaultPromise)2 SendCallback (org.apache.rocketmq.client.producer.SendCallback)2