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