io.openmessaging.MessageHeader Java Examples

The following examples show how to use io.openmessaging.MessageHeader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProducerImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
private SendResult send(final Message message, long timeout) {
    checkMessageType(message);
    org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message);
    try {
        org.apache.rocketmq.client.producer.SendResult rmqResult = this.rocketmqProducer.send(rmqMessage, timeout);
        if (!rmqResult.getSendStatus().equals(SendStatus.SEND_OK)) {
            log.error(String.format("Send message to RocketMQ failed, %s", message));
            throw new OMSRuntimeException("-1", "Send message to RocketMQ broker failed.");
        }
        message.headers().put(MessageHeader.MESSAGE_ID, rmqResult.getMsgId());
        return OMSUtil.sendResultConvert(rmqResult);
    } catch (Exception e) {
        log.error(String.format("Send message to RocketMQ failed, %s", message), e);
        throw checkProducerException(rmqMessage.getTopic(), message.headers().getString(MessageHeader.MESSAGE_ID), e);
    }
}
 
Example #2
Source File: PushConsumerImplTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: ProducerImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private SendResult send(final Message message, long timeout) {
    checkMessageType(message);
    org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message);
    try {
        org.apache.rocketmq.client.producer.SendResult rmqResult = this.rocketmqProducer.send(rmqMessage, timeout);
        if (!rmqResult.getSendStatus().equals(SendStatus.SEND_OK)) {
            log.error(String.format("Send message to RocketMQ failed, %s", message));
            throw new OMSRuntimeException("-1", "Send message to RocketMQ broker failed.");
        }
        message.headers().put(MessageHeader.MESSAGE_ID, rmqResult.getMsgId());
        return OMSUtil.sendResultConvert(rmqResult);
    } catch (Exception e) {
        log.error(String.format("Send message to RocketMQ failed, %s", message), e);
        throw checkProducerException(rmqMessage.getTopic(), message.headers().getString(MessageHeader.MESSAGE_ID), e);
    }
}
 
Example #4
Source File: PushConsumerImplTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #5
Source File: ProducerImpl.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private SendResult send(final Message message, long timeout) {
    checkMessageType(message);
    org.apache.rocketmq.common.message.Message rmqMessage = msgConvert((BytesMessage) message);
    try {
        org.apache.rocketmq.client.producer.SendResult rmqResult = this.rocketmqProducer.send(rmqMessage, timeout);
        if (!rmqResult.getSendStatus().equals(SendStatus.SEND_OK)) {
            log.error(String.format("Send message to RocketMQ failed, %s", message));
            throw new OMSRuntimeException("-1", "Send message to RocketMQ broker failed.");
        }
        message.headers().put(MessageHeader.MESSAGE_ID, rmqResult.getMsgId());
        return OMSUtil.sendResultConvert(rmqResult);
    } catch (Exception e) {
        log.error(String.format("Send message to RocketMQ failed, %s", message), e);
        throw checkProducerException(rmqMessage.getTopic(), message.headers().getString(MessageHeader.MESSAGE_ID), e);
    }
}
 
Example #6
Source File: PushConsumerImplTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #7
Source File: SimplePushConsumer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example #8
Source File: SequenceProducerImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public void send(final Message message) {
    checkMessageType(message);
    org.apache.rocketmq.common.message.Message rmqMessage = OMSUtil.msgConvert((BytesMessage) message);
    try {
        Validators.checkMessage(rmqMessage, this.rocketmqProducer);
    } catch (MQClientException e) {
        throw checkProducerException(rmqMessage.getTopic(), message.headers().getString(MessageHeader.MESSAGE_ID), e);
    }
    msgCacheQueue.add(message);
}
 
Example #9
Source File: AbstractOMSProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public BytesMessage createBytesMessageToTopic(final String topic, final byte[] body) {
    BytesMessage bytesMessage = new BytesMessageImpl();
    bytesMessage.setBody(body);
    bytesMessage.headers().put(MessageHeader.TOPIC, topic);
    return bytesMessage;
}
 
Example #10
Source File: SimplePullConsumer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example #11
Source File: AbstractOMSProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public BytesMessage createBytesMessageToQueue(final String queue, final byte[] body) {
    BytesMessage bytesMessage = new BytesMessageImpl();
    bytesMessage.setBody(body);
    bytesMessage.headers().put(MessageHeader.QUEUE, queue);
    return bytesMessage;
}
 
Example #12
Source File: SimplePullConsumer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example #13
Source File: OMSUtil.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static BytesMessage msgConvert(org.apache.rocketmq.common.message.MessageExt rmqMsg) {
    BytesMessage omsMsg = new BytesMessageImpl();
    omsMsg.setBody(rmqMsg.getBody());

    KeyValue headers = omsMsg.headers();
    KeyValue properties = omsMsg.properties();

    final Set<Map.Entry<String, String>> entries = rmqMsg.getProperties().entrySet();

    for (final Map.Entry<String, String> entry : entries) {
        if (isOMSHeader(entry.getKey())) {
            headers.put(entry.getKey(), entry.getValue());
        } else {
            properties.put(entry.getKey(), entry.getValue());
        }
    }

    omsMsg.putHeaders(MessageHeader.MESSAGE_ID, rmqMsg.getMsgId());
    if (!rmqMsg.getProperties().containsKey(NonStandardKeys.MESSAGE_DESTINATION) ||
        rmqMsg.getProperties().get(NonStandardKeys.MESSAGE_DESTINATION).equals("TOPIC")) {
        omsMsg.putHeaders(MessageHeader.TOPIC, rmqMsg.getTopic());
    } else {
        omsMsg.putHeaders(MessageHeader.QUEUE, rmqMsg.getTopic());
    }
    omsMsg.putHeaders(MessageHeader.SEARCH_KEY, rmqMsg.getKeys());
    omsMsg.putHeaders(MessageHeader.BORN_HOST, String.valueOf(rmqMsg.getBornHost()));
    omsMsg.putHeaders(MessageHeader.BORN_TIMESTAMP, rmqMsg.getBornTimestamp());
    omsMsg.putHeaders(MessageHeader.STORE_HOST, String.valueOf(rmqMsg.getStoreHost()));
    omsMsg.putHeaders(MessageHeader.STORE_TIMESTAMP, rmqMsg.getStoreTimestamp());
    return omsMsg;
}
 
Example #14
Source File: OMSUtil.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static boolean isOMSHeader(String value) {
    for (Field field : MessageHeader.class.getDeclaredFields()) {
        try {
            if (field.get(MessageHeader.class).equals(value)) {
                return true;
            }
        } catch (IllegalAccessException e) {
            return false;
        }
    }
    return false;
}
 
Example #15
Source File: SequenceProducerImplTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testSend_WithCommit() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    SendResult sendResult = new SendResult();
    sendResult.setMsgId("TestMsgID");
    sendResult.setSendStatus(SendStatus.SEND_OK);
    when(rocketmqProducer.send(ArgumentMatchers.<Message>anyList())).thenReturn(sendResult);
    when(rocketmqProducer.getMaxMessageSize()).thenReturn(1024);
    final BytesMessage message = producer.createBytesMessageToTopic("HELLO_TOPIC", new byte[] {'a'});
    producer.send(message);
    producer.commit();
    assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("TestMsgID");
}
 
Example #16
Source File: SequenceProducerImplTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testRollback() {
    when(rocketmqProducer.getMaxMessageSize()).thenReturn(1024);
    final BytesMessage message = producer.createBytesMessageToTopic("HELLO_TOPIC", new byte[] {'a'});
    producer.send(message);
    producer.rollback();
    producer.commit(); //Commit nothing.
    assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo(null);
}
 
Example #17
Source File: ConsumerTester.java    From coding-snippets with MIT License 5 votes vote down vote up
@Override
public void run() {
    while (true) {
        try {
            BytesMessage message = (BytesMessage) consumer.poll();
            if (message == null) {
                break;
            }
            String queueOrTopic;
            if (message.headers().getString(MessageHeader.QUEUE) != null) {
                queueOrTopic = message.headers().getString(MessageHeader.QUEUE);
            } else {
                queueOrTopic = message.headers().getString(MessageHeader.TOPIC);
            }
            if (queueOrTopic == null || queueOrTopic.length() == 0) {
                throw new Exception("Queue or Topic name is empty");
            }
            String body = new String(message.getBody());
            int index = body.lastIndexOf("_");
            String producer = body.substring(0, index);
            final int expectedOffset = offsets.get(queueOrTopic).get(producer);
            int offset = Integer.parseInt(body.substring(index + 1));
            if (offset != expectedOffset) {
                logger.error("Offset not equal expected:{} actual:{} producer:{} queueOrTopic:{}",
                        expectedOffset, offset, producer, queueOrTopic);
                break;
            } else {
                offsets.get(queueOrTopic).put(producer, offset + 1);
            }
            pullNum++;
        } catch (Exception e) {
            logger.error("Error occurred in the consuming process", e);
            break;
        }
    }
}
 
Example #18
Source File: PullConsumerImplTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #19
Source File: ConsumerTester.java    From OpenMessageShaping with MIT License 5 votes vote down vote up
@Override
public void run() {
    while (true) {
        try {
            BytesMessage message = (BytesMessage) consumer.poll();
            if (message == null) {
                break;
            }
            String queueOrTopic;
            if (message.headers().getString(MessageHeader.QUEUE) != null) {
                queueOrTopic = message.headers().getString(MessageHeader.QUEUE);
            } else {
                queueOrTopic = message.headers().getString(MessageHeader.TOPIC);
            }
            if (queueOrTopic == null || queueOrTopic.length() == 0) {
                throw new Exception("Queue or Topic name is empty");
            }
            String body = new String(message.getBody());
            int index = body.lastIndexOf("_");
            String producer = body.substring(0, index);
            int offset = Integer.parseInt(body.substring(index + 1));
            if (offset != offsets.get(queueOrTopic).get(producer)) {
                logger.error("Offset not equal expected:{} actual:{} producer:{} queueOrTopic:{}",
                        offsets.get(producer), offset, producer, queueOrTopic);
                break;
            } else {
                offsets.get(queueOrTopic).put(producer, offset + 1);
            }
            pullNum++;
        } catch (Exception e) {
            logger.error("Error occurred in the consuming process", e);
            break;
        }
    }
}
 
Example #20
Source File: SimplePushConsumer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example #21
Source File: SimplePushConsumer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example #22
Source File: SequenceProducerImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Override
public void send(final Message message) {
    checkMessageType(message);
    org.apache.rocketmq.common.message.Message rmqMessage = OMSUtil.msgConvert((BytesMessage) message);
    try {
        Validators.checkMessage(rmqMessage, this.rocketmqProducer);
    } catch (MQClientException e) {
        throw checkProducerException(rmqMessage.getTopic(), message.headers().getString(MessageHeader.MESSAGE_ID), e);
    }
    msgCacheQueue.add(message);
}
 
Example #23
Source File: AbstractOMSProducer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Override
public BytesMessage createBytesMessageToTopic(final String topic, final byte[] body) {
    BytesMessage bytesMessage = new BytesMessageImpl();
    bytesMessage.setBody(body);
    bytesMessage.headers().put(MessageHeader.TOPIC, topic);
    return bytesMessage;
}
 
Example #24
Source File: AbstractOMSProducer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Override
public BytesMessage createBytesMessageToQueue(final String queue, final byte[] body) {
    BytesMessage bytesMessage = new BytesMessageImpl();
    bytesMessage.setBody(body);
    bytesMessage.headers().put(MessageHeader.QUEUE, queue);
    return bytesMessage;
}
 
Example #25
Source File: OMSUtil.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public static BytesMessage msgConvert(org.apache.rocketmq.common.message.MessageExt rmqMsg) {
    BytesMessage omsMsg = new BytesMessageImpl();
    omsMsg.setBody(rmqMsg.getBody());

    KeyValue headers = omsMsg.headers();
    KeyValue properties = omsMsg.properties();

    final Set<Map.Entry<String, String>> entries = rmqMsg.getProperties().entrySet();

    for (final Map.Entry<String, String> entry : entries) {
        if (isOMSHeader(entry.getKey())) {
            headers.put(entry.getKey(), entry.getValue());
        } else {
            properties.put(entry.getKey(), entry.getValue());
        }
    }

    omsMsg.putHeaders(MessageHeader.MESSAGE_ID, rmqMsg.getMsgId());
    if (!rmqMsg.getProperties().containsKey(NonStandardKeys.MESSAGE_DESTINATION) ||
        rmqMsg.getProperties().get(NonStandardKeys.MESSAGE_DESTINATION).equals("TOPIC")) {
        omsMsg.putHeaders(MessageHeader.TOPIC, rmqMsg.getTopic());
    } else {
        omsMsg.putHeaders(MessageHeader.QUEUE, rmqMsg.getTopic());
    }
    omsMsg.putHeaders(MessageHeader.SEARCH_KEY, rmqMsg.getKeys());
    omsMsg.putHeaders(MessageHeader.BORN_HOST, String.valueOf(rmqMsg.getBornHost()));
    omsMsg.putHeaders(MessageHeader.BORN_TIMESTAMP, rmqMsg.getBornTimestamp());
    omsMsg.putHeaders(MessageHeader.STORE_HOST, String.valueOf(rmqMsg.getStoreHost()));
    omsMsg.putHeaders(MessageHeader.STORE_TIMESTAMP, rmqMsg.getStoreTimestamp());
    return omsMsg;
}
 
Example #26
Source File: OMSUtil.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public static boolean isOMSHeader(String value) {
    for (Field field : MessageHeader.class.getDeclaredFields()) {
        try {
            if (field.get(MessageHeader.class).equals(value)) {
                return true;
            }
        } catch (IllegalAccessException e) {
            return false;
        }
    }
    return false;
}
 
Example #27
Source File: SequenceProducerImplTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Test
public void testSend_WithCommit() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    SendResult sendResult = new SendResult();
    sendResult.setMsgId("TestMsgID");
    sendResult.setSendStatus(SendStatus.SEND_OK);
    when(rocketmqProducer.send(ArgumentMatchers.<Message>anyList())).thenReturn(sendResult);
    when(rocketmqProducer.getMaxMessageSize()).thenReturn(1024);
    final BytesMessage message = producer.createBytesMessageToTopic("HELLO_TOPIC", new byte[] {'a'});
    producer.send(message);
    producer.commit();
    assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("TestMsgID");
}
 
Example #28
Source File: SequenceProducerImplTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Test
public void testRollback() {
    when(rocketmqProducer.getMaxMessageSize()).thenReturn(1024);
    final BytesMessage message = producer.createBytesMessageToTopic("HELLO_TOPIC", new byte[] {'a'});
    producer.send(message);
    producer.rollback();
    producer.commit(); //Commit nothing.
    assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo(null);
}
 
Example #29
Source File: PullConsumerImplTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #30
Source File: SimplePullConsumer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}