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);
}
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));
}
}
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();
}
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();
}
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();
}
Aggregations