Search in sources :

Example 6 with Message

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

the class RocketMQProducer method push.

public long push(String json) throws Exception {
    LOGGER.debug(json);
    Message message = new Message(config.mqTopic, json.getBytes("UTF-8"));
    SendResult sendResult = producer.send(message);
    return sendResult.getQueueOffset();
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult)

Example 7 with Message

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

the class RocketMQServerMock method prepareDataTo.

public void prepareDataTo(String topic, int times) throws Exception {
    // publish test message
    DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
    producer.setNamesrvAddr(nameserverAddr);
    String sendMsg = "\"Hello Rocket\"" + "," + DateFormatUtils.format(new Date(), "yyyy-MM-DD hh:mm:ss");
    try {
        producer.start();
        for (int i = 0; i < times; i++) {
            producer.send(new Message(topic, sendMsg.getBytes("UTF-8")));
        }
    } catch (Exception e) {
        throw new MQClientException("Failed to publish messages", e);
    } finally {
        producer.shutdown();
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Date(java.util.Date) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 8 with Message

use of org.apache.rocketmq.common.message.Message in project spring-boot-starter-samples by vindell.

the class ScheduledMessageProducer method main.

public static void main(String[] args) throws Exception {
    // Instantiate a producer to send scheduled messages
    DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
    // Launch producer
    producer.start();
    int totalMessagesToSend = 100;
    for (int i = 0; i < totalMessagesToSend; i++) {
        Message message = new Message("TestTopic", ("Hello scheduled message " + i).getBytes());
        // This message will be delivered to consumer 10 seconds later.
        message.setDelayTimeLevel(3);
        // Send the message
        producer.send(message);
    }
    // Shutdown producer after use.
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Example 9 with Message

use of org.apache.rocketmq.common.message.Message in project spring-boot-starter-samples by vindell.

the class SimpleAsyncProducer method main.

public static void main(String[] args) throws Exception {
    // Instantiate with a producer group name.
    DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
    // Launch the instance.
    producer.start();
    producer.setRetryTimesWhenSendAsyncFailed(0);
    for (int i = 0; i < 100; i++) {
        final int index = i;
        // Create a message instance, specifying topic, tag and message body.
        Message msg = new Message("TopicTest", "TagA", "OrderID188", "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
        producer.send(msg, new SendCallback() {

            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId());
            }

            @Override
            public void onException(Throwable e) {
                System.out.printf("%-10d Exception %s %n", index, e);
                e.printStackTrace();
            }
        });
    }
    // Shut down once the producer instance is not longer in use.
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) SendCallback(org.apache.rocketmq.client.producer.SendCallback)

Example 10 with Message

use of org.apache.rocketmq.common.message.Message in project spring-boot-starter-samples by vindell.

the class SimpleOnewayProducer method main.

public static void main(String[] args) throws Exception {
    // Instantiate with a producer group name.
    DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
    // Launch the instance.
    producer.start();
    for (int i = 0; i < 100; i++) {
        // Create a message instance, specifying topic, tag and message body.
        Message msg = new Message("TopicTest", /* Topic */
        "TagA", /* Tag */
        ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
        // Call send message to deliver message to one of brokers.
        producer.sendOneway(msg);
    }
    // Shut down once the producer instance is not longer in use.
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

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