Search in sources :

Example 1 with Message

use of org.apache.rocketmq.common.message.Message in project ignite by apache.

the class RocketMQStreamerTest method produceData.

/**
 * Adds data to RocketMQ.
 *
 * @throws Exception If fails.
 */
private void produceData() throws Exception {
    initTopic(TOPIC_NAME, TEST_IP + ":" + NAME_SERVER_PORT);
    DefaultMQProducer producer = new DefaultMQProducer("testProducerGrp");
    producer.setNamesrvAddr(TEST_IP + ":" + NAME_SERVER_PORT);
    try {
        producer.start();
        for (int i = 0; i < EVT_NUM; i++) producer.send(new Message(TOPIC_NAME, "", String.valueOf(i).getBytes("UTF-8")));
    } catch (Exception e) {
        throw new Exception(e);
    } finally {
        producer.shutdown();
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException)

Example 2 with Message

use of org.apache.rocketmq.common.message.Message in project rocketmq-externals by apache.

the class RocketMQConsoleTestBase method sendTestTopicMessage.

protected SendResult sendTestTopicMessage() throws InterruptedException {
    if (producer == null) {
        startTestMQProducer();
    }
    Message message = new Message(TEST_CONSOLE_TOPIC, TEST_TOPIC_MESSAGE_BODY.getBytes());
    message.setKeys(Lists.<String>newArrayList(TEST_TOPIC_MESSAGE_KEY));
    for (int i = 0; i < 3; i++) {
        try {
            return producer.send(message);
        } catch (Exception ignore) {
            Thread.sleep(500);
        }
    }
    throw new RuntimeException("send message error");
}
Also used : Message(org.apache.rocketmq.common.message.Message)

Example 3 with Message

use of org.apache.rocketmq.common.message.Message in project rocketmq-externals by apache.

the class RocketMQSink method invoke.

@Override
public void invoke(IN input, Context context) throws Exception {
    Message msg = prepareMessage(input);
    if (batchFlushOnCheckpoint) {
        batchList.add(msg);
        return;
    }
    if (async) {
        // async sending
        try {
            producer.send(msg, new SendCallback() {

                @Override
                public void onSuccess(SendResult sendResult) {
                    LOG.debug("Async send message success! result: {}", sendResult);
                }

                @Override
                public void onException(Throwable throwable) {
                    if (throwable != null) {
                        LOG.error("Async send message failure!", throwable);
                    }
                }
            });
        } catch (Exception e) {
            LOG.error("Async send message failure!", e);
        }
    } else {
        // sync sending, will return a SendResult
        try {
            SendResult result = producer.send(msg);
            LOG.debug("Sync send message result: {}", result);
        } catch (Exception e) {
            LOG.error("Sync send message failure!", e);
        }
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) SendCallback(org.apache.rocketmq.client.producer.SendCallback) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 4 with Message

use of org.apache.rocketmq.common.message.Message in project rocketmq-externals by apache.

the class ProducerTest method main.

public static void main(String[] args) {
    DefaultMQProducer producer = new DefaultMQProducer("p001");
    producer.setNamesrvAddr("localhost:9876");
    try {
        producer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
    }
    for (int i = 0; i < 10000; i++) {
        Message msg = new Message("flink-source2", "", "id_" + i, ("country_X province_" + i).getBytes());
        try {
            producer.send(msg);
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("send " + i);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 5 with Message

use of org.apache.rocketmq.common.message.Message in project rocketmq-externals by apache.

the class MessageConverter method convert2RMQMessage.

public static Message convert2RMQMessage(JmsBaseMessage jmsMsg) throws Exception {
    Message rocketmqMsg = new MessageExt();
    // 1. Transform message body
    rocketmqMsg.setBody(MessageConverter.getContentFromJms(jmsMsg));
    // 2. Transform topic and messageType
    JmsBaseTopic destination = (JmsBaseTopic) jmsMsg.getHeaders().get(JmsBaseConstant.JMS_DESTINATION);
    String topic = destination.getMessageTopic();
    rocketmqMsg.setTopic(topic);
    String messageType = destination.getMessageType();
    Preconditions.checkState(!messageType.contains("||"), "'||' can not be in the destination when sending a message");
    rocketmqMsg.setTags(messageType);
    // 3. Transform message properties
    Properties properties = initRocketMQHeaders(jmsMsg, topic, messageType);
    for (String name : properties.stringPropertyNames()) {
        String value = properties.getProperty(name);
        if (MessageConst.PROPERTY_KEYS.equals(name)) {
            rocketmqMsg.setKeys(value);
        } else if (MessageConst.PROPERTY_TAGS.equals(name)) {
            rocketmqMsg.setTags(value);
        } else if (MessageConst.PROPERTY_DELAY_TIME_LEVEL.equals(name)) {
            rocketmqMsg.setDelayTimeLevel(Integer.parseInt(value));
        } else if (MessageConst.PROPERTY_WAIT_STORE_MSG_OK.equals(name)) {
            rocketmqMsg.setWaitStoreMsgOK(Boolean.parseBoolean(value));
        } else if (MessageConst.PROPERTY_BUYER_ID.equals(name)) {
            rocketmqMsg.setBuyerId(value);
        } else {
            rocketmqMsg.putUserProperty(name, value);
        }
    }
    return rocketmqMsg;
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) JmsObjectMessage(org.apache.rocketmq.jms.domain.message.JmsObjectMessage) JmsBytesMessage(org.apache.rocketmq.jms.domain.message.JmsBytesMessage) Message(org.apache.rocketmq.common.message.Message) JmsTextMessage(org.apache.rocketmq.jms.domain.message.JmsTextMessage) TextMessage(javax.jms.TextMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) JmsBaseMessage(org.apache.rocketmq.jms.domain.message.JmsBaseMessage) JmsBaseTopic(org.apache.rocketmq.jms.domain.JmsBaseTopic) Properties(java.util.Properties)

Aggregations

Message (org.apache.rocketmq.common.message.Message)29 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)13 SendResult (org.apache.rocketmq.client.producer.SendResult)12 MQClientException (org.apache.rocketmq.client.exception.MQClientException)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 Channel (org.apache.flume.Channel)2 Event (org.apache.flume.Event)2 EventDeliveryException (org.apache.flume.EventDeliveryException)2 Transaction (org.apache.flume.Transaction)2 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)2 SendCallback (org.apache.rocketmq.client.producer.SendCallback)2 MessageExt (org.apache.rocketmq.common.message.MessageExt)2 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)2 Test (org.junit.Test)2 BusinessException (com.paascloud.base.exception.BusinessException)1 MqMessage (com.paascloud.core.mq.MqMessage)1 TpcBizException (com.paascloud.provider.exceptions.TpcBizException)1 List (java.util.List)1