Search in sources :

Example 6 with Message

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

the class PullConsumerImplTest method testPoll.

@Test
public void testPoll() {
    final byte[] testBody = new byte[] { 'a', 'b' };
    MessageExt consumedMsg = new MessageExt();
    consumedMsg.setMsgId("NewMsgId");
    consumedMsg.setBody(testBody);
    consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC");
    consumedMsg.setTopic(queueName);
    when(localMessageCache.poll()).thenReturn(consumedMsg);
    Message message = consumer.poll();
    assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("NewMsgId");
    assertThat(((BytesMessage) message).getBody()).isEqualTo(testBody);
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) Message(io.openmessaging.Message) BytesMessage(io.openmessaging.BytesMessage) BytesMessage(io.openmessaging.BytesMessage) Test(org.junit.Test)

Example 7 with Message

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

the class SimplePullConsumer method main.

public static void main(String[] args) {
    final MessagingAccessPoint messagingAccessPoint = MessagingAccessPointFactory.getMessagingAccessPoint("openmessaging:rocketmq://IP1:9876,IP2:9876/namespace");
    final PullConsumer consumer = messagingAccessPoint.createPullConsumer("OMS_HELLO_TOPIC", OMS.newKeyValue().put(NonStandardKeys.CONSUMER_GROUP, "OMS_CONSUMER"));
    messagingAccessPoint.startup();
    System.out.printf("MessagingAccessPoint startup OK%n");
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        @Override
        public void run() {
            consumer.shutdown();
            messagingAccessPoint.shutdown();
        }
    }));
    consumer.startup();
    System.out.printf("Consumer startup OK%n");
    while (true) {
        Message message = consumer.poll();
        if (message != null) {
            String msgId = message.headers().getString(MessageHeader.MESSAGE_ID);
            System.out.printf("Received one message: %s%n", msgId);
            consumer.ack(msgId);
        }
    }
}
Also used : Message(io.openmessaging.Message) MessagingAccessPoint(io.openmessaging.MessagingAccessPoint) PullConsumer(io.openmessaging.PullConsumer)

Example 8 with Message

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

the class SequenceProducerImpl method commit.

@Override
public synchronized void commit() {
    List<Message> messages = new ArrayList<>();
    msgCacheQueue.drainTo(messages);
    List<org.apache.rocketmq.common.message.Message> rmqMessages = new ArrayList<>();
    for (Message message : messages) {
        rmqMessages.add(OMSUtil.msgConvert((BytesMessage) message));
    }
    if (rmqMessages.size() == 0) {
        return;
    }
    try {
        SendResult sendResult = this.rocketmqProducer.send(rmqMessages);
        String[] msgIdArray = sendResult.getMsgId().split(",");
        for (int i = 0; i < messages.size(); i++) {
            Message message = messages.get(i);
            message.headers().put(MessageHeader.MESSAGE_ID, msgIdArray[i]);
        }
    } catch (Exception e) {
        throw checkProducerException("", "", e);
    }
}
Also used : Message(io.openmessaging.Message) BytesMessage(io.openmessaging.BytesMessage) SendResult(org.apache.rocketmq.client.producer.SendResult) ArrayList(java.util.ArrayList) BytesMessage(io.openmessaging.BytesMessage) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 9 with Message

use of io.openmessaging.Message 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 10 with Message

use of io.openmessaging.Message 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)

Aggregations

Message (io.openmessaging.Message)14 BytesMessage (io.openmessaging.BytesMessage)8 MessagingAccessPoint (io.openmessaging.MessagingAccessPoint)6 Test (org.junit.Test)6 MessageListener (io.openmessaging.MessageListener)4 ReceivedMessageContext (io.openmessaging.ReceivedMessageContext)4 MessageExt (org.apache.rocketmq.common.message.MessageExt)4 Producer (io.openmessaging.Producer)2 Promise (io.openmessaging.Promise)2 PromiseListener (io.openmessaging.PromiseListener)2 PullConsumer (io.openmessaging.PullConsumer)2 PushConsumer (io.openmessaging.PushConsumer)2 SendResult (io.openmessaging.SendResult)2 ArrayList (java.util.ArrayList)2 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 SendResult (org.apache.rocketmq.client.producer.SendResult)2