Search in sources :

Example 6 with BytesMessage

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

the class SequenceProducerImplTest method testRollback.

@Test
public void testRollback() {
    when(rocketmqProducer.getMaxMessageSize()).thenReturn(1024);
    final BytesMessage message = producer.createBytesMessageToTopic("HELLO_TOPIC", new byte[] { 'a' });
    producer.send(message);
    producer.rollback();
    // Commit nothing.
    producer.commit();
    assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo(null);
}
Also used : BytesMessage(io.openmessaging.BytesMessage) Test(org.junit.Test)

Example 7 with BytesMessage

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

the class SequenceProducerImplTest method testSend_WithCommit.

@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");
}
Also used : SendResult(org.apache.rocketmq.client.producer.SendResult) BytesMessage(io.openmessaging.BytesMessage) Test(org.junit.Test)

Example 8 with BytesMessage

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

the class AbstractOMSProducer method createBytesMessageToTopic.

@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;
}
Also used : BytesMessageImpl(io.openmessaging.rocketmq.domain.BytesMessageImpl) BytesMessage(io.openmessaging.BytesMessage)

Example 9 with BytesMessage

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

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

the class OMSUtil method msgConvert.

public static org.apache.rocketmq.common.message.Message msgConvert(BytesMessage omsMessage) {
    org.apache.rocketmq.common.message.Message rmqMessage = new org.apache.rocketmq.common.message.Message();
    rmqMessage.setBody(omsMessage.getBody());
    KeyValue headers = omsMessage.headers();
    KeyValue properties = omsMessage.properties();
    // All destinations in RocketMQ use Topic
    if (headers.containsKey(MessageHeader.TOPIC)) {
        rmqMessage.setTopic(headers.getString(MessageHeader.TOPIC));
        rmqMessage.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC");
    } else {
        rmqMessage.setTopic(headers.getString(MessageHeader.QUEUE));
        rmqMessage.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "QUEUE");
    }
    for (String key : properties.keySet()) {
        MessageAccessor.putProperty(rmqMessage, key, properties.getString(key));
    }
    // Headers has a high priority
    for (String key : headers.keySet()) {
        MessageAccessor.putProperty(rmqMessage, key, headers.getString(key));
    }
    return rmqMessage;
}
Also used : KeyValue(io.openmessaging.KeyValue) BytesMessage(io.openmessaging.BytesMessage)

Aggregations

BytesMessage (io.openmessaging.BytesMessage)18 Test (org.junit.Test)8 Message (io.openmessaging.Message)6 BytesMessageImpl (io.openmessaging.rocketmq.domain.BytesMessageImpl)6 KeyValue (io.openmessaging.KeyValue)4 SendResult (org.apache.rocketmq.client.producer.SendResult)4 MessageExt (org.apache.rocketmq.common.message.MessageExt)4 MessageListener (io.openmessaging.MessageListener)2 ReceivedMessageContext (io.openmessaging.ReceivedMessageContext)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2