Search in sources :

Example 1 with RocketMQTemplate

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);
    }
}
Also used : RocketMQTemplate(org.apache.rocketmq.spring.core.RocketMQTemplate) RTUnsupportedEncodingException(com.ecfront.dew.common.exception.RTUnsupportedEncodingException) RTUnsupportedEncodingException(com.ecfront.dew.common.exception.RTUnsupportedEncodingException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 2 with RocketMQTemplate

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());
}
Also used : RocketMQTransactionListener(org.apache.rocketmq.spring.annotation.RocketMQTransactionListener) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) RocketMQLocalTransactionListener(org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RocketMQTemplate(org.apache.rocketmq.spring.core.RocketMQTemplate) TransactionMQProducer(org.apache.rocketmq.client.producer.TransactionMQProducer)

Example 3 with RocketMQTemplate

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);
    });
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) SendResult(org.apache.rocketmq.client.producer.SendResult) ArrayList(java.util.ArrayList) RocketMQTemplate(org.apache.rocketmq.spring.core.RocketMQTemplate) Test(org.junit.Test)

Example 4 with RocketMQTemplate

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);
    }
}
Also used : RocketMQTemplate(org.apache.rocketmq.spring.core.RocketMQTemplate) RTUnsupportedEncodingException(com.ecfront.dew.common.exception.RTUnsupportedEncodingException) RTUnsupportedEncodingException(com.ecfront.dew.common.exception.RTUnsupportedEncodingException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 5 with RocketMQTemplate

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;
}
Also used : RocketMQTemplate(org.apache.rocketmq.spring.core.RocketMQTemplate) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Conditional(org.springframework.context.annotation.Conditional) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

RocketMQTemplate (org.apache.rocketmq.spring.core.RocketMQTemplate)7 MQClientException (org.apache.rocketmq.client.exception.MQClientException)4 RTUnsupportedEncodingException (com.ecfront.dew.common.exception.RTUnsupportedEncodingException)2 BeanDefinitionValidationException (org.springframework.beans.factory.support.BeanDefinitionValidationException)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 ArrayList (java.util.ArrayList)1 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 DefaultLitePullConsumer (org.apache.rocketmq.client.consumer.DefaultLitePullConsumer)1 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)1 SendResult (org.apache.rocketmq.client.producer.SendResult)1 TransactionMQProducer (org.apache.rocketmq.client.producer.TransactionMQProducer)1 ExtRocketMQConsumerConfiguration (org.apache.rocketmq.spring.annotation.ExtRocketMQConsumerConfiguration)1 ExtRocketMQTemplateConfiguration (org.apache.rocketmq.spring.annotation.ExtRocketMQTemplateConfiguration)1 RocketMQTransactionListener (org.apache.rocketmq.spring.annotation.RocketMQTransactionListener)1 RocketMQLocalTransactionListener (org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener)1 Test (org.junit.Test)1 BeansException (org.springframework.beans.BeansException)1 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1