use of org.apache.rocketmq.spring.core.RocketMQTemplate in project dew by gudaoxuri.
the class RocketClusterMQ method doPublish.
@Override
protected boolean doPublish(String topic, String message, Optional<Map<String, Object>> header, boolean confirm) {
RocketMQTemplate rocketMQTemplate = rocketAdapter.getRocketMQTemplate();
Object funResult = null;
if (confirm) {
throw new RTUnsupportedEncodingException("Rocket doesn't support confirm mode");
}
try {
Map<String, Object> sendHeader = getMQHeader(topic);
header.ifPresent(sendHeader::putAll);
Message<?> msg = MessageBuilder.withPayload(message).copyHeaders(sendHeader).build();
funResult = sendBeforeFun.invoke(topic, sendHeader);
rocketMQTemplate.syncSend(topic, msg);
return true;
} catch (Exception e) {
logger.error("[MQ] Rocket publish error.", e);
sendErrorFun.invoke(e, funResult);
return false;
} finally {
sendFinishFun.invoke(funResult);
}
}
use of org.apache.rocketmq.spring.core.RocketMQTemplate in project rocketmq-spring by apache.
the class RocketMQTransactionConfiguration method registerTransactionListener.
private void registerTransactionListener(String beanName, Object bean) {
Class<?> clazz = AopProxyUtils.ultimateTargetClass(bean);
if (!RocketMQLocalTransactionListener.class.isAssignableFrom(bean.getClass())) {
throw new IllegalStateException(clazz + " is not instance of " + RocketMQLocalTransactionListener.class.getName());
}
RocketMQTransactionListener annotation = clazz.getAnnotation(RocketMQTransactionListener.class);
RocketMQTemplate rocketMQTemplate = (RocketMQTemplate) applicationContext.getBean(annotation.rocketMQTemplateBeanName());
if (((TransactionMQProducer) rocketMQTemplate.getProducer()).getTransactionListener() != null) {
throw new IllegalStateException(annotation.rocketMQTemplateBeanName() + " already exists RocketMQLocalTransactionListener");
}
((TransactionMQProducer) rocketMQTemplate.getProducer()).setExecutorService(new ThreadPoolExecutor(annotation.corePoolSize(), annotation.maximumPoolSize(), annotation.keepAliveTime(), TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(annotation.blockingQueueSize())));
((TransactionMQProducer) rocketMQTemplate.getProducer()).setTransactionListener(RocketMQUtil.convert((RocketMQLocalTransactionListener) bean));
log.debug("RocketMQLocalTransactionListener {} register to {} success", clazz.getName(), annotation.rocketMQTemplateBeanName());
}
use of org.apache.rocketmq.spring.core.RocketMQTemplate in project rocketmq-spring by apache.
the class RocketMQAutoConfigurationTest method testBatchSendMessage.
@Test
public void testBatchSendMessage() {
runner.withPropertyValues("rocketmq.name-server=127.0.0.1:9876", "rocketmq.producer.group=spring_rocketmq").run((context) -> {
RocketMQTemplate rocketMQTemplate = context.getBean(RocketMQTemplate.class);
List<GenericMessage<String>> batchMessages = new ArrayList<GenericMessage<String>>();
String errorMsg = null;
try {
SendResult customSendResult = rocketMQTemplate.syncSend("test", batchMessages, 60000);
} catch (IllegalArgumentException e) {
// it will be throw IllegalArgumentException: `messages` can not be empty
errorMsg = e.getMessage();
}
// that means the rocketMQTemplate.syncSend is chosen the correct type method
Assert.assertEquals("`messages` can not be empty", errorMsg);
});
}
use of org.apache.rocketmq.spring.core.RocketMQTemplate in project dew by gudaoxuri.
the class RocketClusterMQ method doRequest.
@Override
protected boolean doRequest(String address, String message, Optional<Map<String, Object>> header, boolean confirm) {
RocketMQTemplate rocketMQTemplate = rocketAdapter.getRocketMQTemplate();
Object funResult = null;
if (confirm) {
throw new RTUnsupportedEncodingException("Rocket doesn't support confirm mode");
}
try {
Map<String, Object> sendHeader = getMQHeader(address);
header.ifPresent(sendHeader::putAll);
funResult = sendBeforeFun.invoke(address, sendHeader);
Message<?> msg = MessageBuilder.withPayload(message).copyHeaders(sendHeader).build();
rocketMQTemplate.syncSend(address, msg);
return true;
} catch (Exception e) {
logger.error("[MQ] Rocket publish error.", e);
sendErrorFun.invoke(e, funResult);
return false;
} finally {
sendFinishFun.invoke(funResult);
}
}
use of org.apache.rocketmq.spring.core.RocketMQTemplate in project rocketmq-spring by apache.
the class RocketMQAutoConfiguration method rocketMQTemplate.
@Bean(destroyMethod = "destroy")
@Conditional(ProducerOrConsumerPropertyCondition.class)
@ConditionalOnMissingBean(name = ROCKETMQ_TEMPLATE_DEFAULT_GLOBAL_NAME)
public RocketMQTemplate rocketMQTemplate(RocketMQMessageConverter rocketMQMessageConverter) {
RocketMQTemplate rocketMQTemplate = new RocketMQTemplate();
if (applicationContext.containsBean(PRODUCER_BEAN_NAME)) {
rocketMQTemplate.setProducer((DefaultMQProducer) applicationContext.getBean(PRODUCER_BEAN_NAME));
}
if (applicationContext.containsBean(CONSUMER_BEAN_NAME)) {
rocketMQTemplate.setConsumer((DefaultLitePullConsumer) applicationContext.getBean(CONSUMER_BEAN_NAME));
}
rocketMQTemplate.setMessageConverter(rocketMQMessageConverter.getMessageConverter());
return rocketMQTemplate;
}
Aggregations