Search in sources :

Example 26 with DefaultMQProducer

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

the class RocketMQAutoConfigurationTests method rocketMQTemplate.

@Test
public void rocketMQTemplate() {
    load("spring.rocketmq.nameServer=127.0.0.1:9876", "spring.rocketmq.producer.group=my_group", "spring.rocketmq.producer.send-msg-timeout=30000", "spring.rocketmq.producer.retry-times-when-send-async-failed=1", "spring.rocketmq.producer.compress-msg-body-over-howmuch=1024", "spring.rocketmq.producer.max-message-size=10240", "spring.rocketmq.producer.retry-another-broker-when-not-store-ok=true", "spring.rocketmq.producer.retry-times-when-send-failed=1");
    assertThat(this.context.containsBean("rocketMQMessageObjectMapper")).isTrue();
    assertThat(this.context.containsBean("mqProducer")).isTrue();
    assertThat(this.context.containsBean("rocketMQTemplate")).isTrue();
    assertThat(this.context.getBeansOfType(DefaultRocketMQListenerContainer.class)).isEmpty();
    RocketMQTemplate rocketMQTemplate = this.context.getBean(RocketMQTemplate.class);
    ObjectMapper objectMapper = this.context.getBean("rocketMQMessageObjectMapper", ObjectMapper.class);
    assertThat(rocketMQTemplate.getObjectMapper()).isEqualTo(objectMapper);
    DefaultMQProducer defaultMQProducer = rocketMQTemplate.getProducer();
    assertThat(defaultMQProducer.getNamesrvAddr()).isEqualTo("127.0.0.1:9876");
    assertThat(defaultMQProducer.getProducerGroup()).isEqualTo("my_group");
    assertThat(defaultMQProducer.getSendMsgTimeout()).isEqualTo(30000);
    assertThat(defaultMQProducer.getRetryTimesWhenSendAsyncFailed()).isEqualTo(1);
    assertThat(defaultMQProducer.getCompressMsgBodyOverHowmuch()).isEqualTo(1024);
    assertThat(defaultMQProducer.getMaxMessageSize()).isEqualTo(10240);
    assertThat(defaultMQProducer.isRetryAnotherBrokerWhenNotStoreOK()).isTrue();
    assertThat(defaultMQProducer.getRetryTimesWhenSendFailed()).isEqualTo(1);
}
Also used : RocketMQTemplate(org.apache.rocketmq.spring.starter.core.RocketMQTemplate) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 27 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 28 with DefaultMQProducer

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

Aggregations

DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)90 Message (org.apache.rocketmq.common.message.Message)63 SendResult (org.apache.rocketmq.client.producer.SendResult)36 MQClientException (org.apache.rocketmq.client.exception.MQClientException)31 Test (org.junit.Test)27 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)17 MessageQueueSelector (org.apache.rocketmq.client.producer.MessageQueueSelector)13 SendCallback (org.apache.rocketmq.client.producer.SendCallback)11 ArrayList (java.util.ArrayList)10 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 CommandLine (org.apache.commons.cli.CommandLine)3 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2 Date (java.util.Date)2 LinkedList (java.util.LinkedList)2