Search in sources :

Example 1 with Message

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

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 2 with Message

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

the class SimplePushConsumer method main.

public static void main(String[] args) {
    final MessagingAccessPoint messagingAccessPoint = MessagingAccessPointFactory.getMessagingAccessPoint("openmessaging:rocketmq://IP1:9876,IP2:9876/namespace");
    final PushConsumer consumer = messagingAccessPoint.createPushConsumer(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.attachQueue("OMS_HELLO_TOPIC", new MessageListener() {

        @Override
        public void onMessage(final Message message, final ReceivedMessageContext context) {
            System.out.printf("Received one message: %s%n", message.headers().getString(MessageHeader.MESSAGE_ID));
            context.ack();
        }
    });
    consumer.startup();
    System.out.printf("Consumer startup OK%n");
}
Also used : Message(io.openmessaging.Message) PushConsumer(io.openmessaging.PushConsumer) MessageListener(io.openmessaging.MessageListener) MessagingAccessPoint(io.openmessaging.MessagingAccessPoint) ReceivedMessageContext(io.openmessaging.ReceivedMessageContext)

Example 3 with Message

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

the class PullConsumerImplTest method testPoll_WithTimeout.

@Test
public void testPoll_WithTimeout() {
    // There is a default timeout value, @see ClientConfig#omsOperationTimeout.
    Message message = consumer.poll();
    assertThat(message).isNull();
    message = consumer.poll(OMS.newKeyValue().put(PropertyKeys.OPERATION_TIMEOUT, 100));
    assertThat(message).isNull();
}
Also used : Message(io.openmessaging.Message) BytesMessage(io.openmessaging.BytesMessage) Test(org.junit.Test)

Example 4 with Message

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

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 5 with Message

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

the class PushConsumerImplTest method testConsumeMessage.

@Test
public void testConsumeMessage() {
    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("HELLO_QUEUE");
    consumer.attachQueue("HELLO_QUEUE", new MessageListener() {

        @Override
        public void onMessage(final Message message, final ReceivedMessageContext context) {
            assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("NewMsgId");
            assertThat(((BytesMessage) message).getBody()).isEqualTo(testBody);
            context.ack();
        }
    });
    ((MessageListenerConcurrently) rocketmqPushConsumer.getMessageListener()).consumeMessage(Collections.singletonList(consumedMsg), null);
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) Message(io.openmessaging.Message) BytesMessage(io.openmessaging.BytesMessage) MessageListener(io.openmessaging.MessageListener) ReceivedMessageContext(io.openmessaging.ReceivedMessageContext) Test(org.junit.Test)

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