Search in sources :

Example 56 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer 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)

Example 57 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer 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 58 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project spring-boot-starter-samples by vindell.

the class SimpleSyncProducer method main.

public static void main(String[] args) throws Exception {
    // Instantiate with a producer group name.
    DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
    // 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.
        SendResult sendResult = producer.send(msg);
        System.out.printf("%s%n", sendResult);
    }
    // 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)

Example 59 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project paascloud-master by paascloud.

the class MqProducerBeanFactory method buildProducerBean.

/**
 * Build producer bean.
 *
 * @param producerDto the producer dto
 */
public static void buildProducerBean(ReliableMessageRegisterDto producerDto) {
    String pid = producerDto.getProducerGroup();
    DefaultMQProducer mQProducer = DEFAULT_MQ_PRODUCER_MAP.get(pid);
    if (mQProducer == null) {
        String simpleName = producerDto.getProducerGroup();
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(DefaultMQProducer.class);
        beanDefinitionBuilder.setScope(BeanDefinition.SCOPE_SINGLETON);
        beanDefinitionBuilder.addPropertyValue("producerGroup", producerDto.getProducerGroup());
        beanDefinitionBuilder.addPropertyValue("namesrvAddr", producerDto.getNamesrvAddr());
        beanDefinitionBuilder.setInitMethodName("start");
        beanDefinitionBuilder.setDestroyMethodName("shutdown");
        SpringContextHolder.getDefaultListableBeanFactory().registerBeanDefinition(simpleName, beanDefinitionBuilder.getBeanDefinition());
        DEFAULT_MQ_PRODUCER_MAP.put(simpleName, SpringContextHolder.getBean(simpleName));
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Example 60 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer in project rocketmq-externals by apache.

the class TopicServiceImpl method sendTopicMessageRequest.

@Override
public SendResult sendTopicMessageRequest(SendTopicMessageRequest sendTopicMessageRequest) {
    DefaultMQProducer producer = new DefaultMQProducer(MixAll.SELF_TEST_PRODUCER_GROUP);
    producer.setInstanceName(String.valueOf(System.currentTimeMillis()));
    producer.setNamesrvAddr(rMQConfigure.getNamesrvAddr());
    try {
        producer.start();
        Message msg = new Message(sendTopicMessageRequest.getTopic(), sendTopicMessageRequest.getTag(), sendTopicMessageRequest.getKey(), sendTopicMessageRequest.getMessageBody().getBytes());
        return producer.send(msg);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    } finally {
        producer.shutdown();
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer)

Aggregations

DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)100 Message (org.apache.rocketmq.common.message.Message)68 SendResult (org.apache.rocketmq.client.producer.SendResult)41 MQClientException (org.apache.rocketmq.client.exception.MQClientException)31 Test (org.junit.Test)28 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)17 MessageQueueSelector (org.apache.rocketmq.client.producer.MessageQueueSelector)13 ArrayList (java.util.ArrayList)12 SendCallback (org.apache.rocketmq.client.producer.SendCallback)11 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)8 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)8 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 MQProducer (org.apache.rocketmq.client.producer.MQProducer)5 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Date (java.util.Date)3 CommandLine (org.apache.commons.cli.CommandLine)3 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2