Java Code Examples for org.apache.rocketmq.client.producer.DefaultMQProducer#setRetryAnotherBrokerWhenNotStoreOK()

The following examples show how to use org.apache.rocketmq.client.producer.DefaultMQProducer#setRetryAnotherBrokerWhenNotStoreOK() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RocketMQAutoConfiguration.java    From rocketmq-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnClass(DefaultMQProducer.class)
@ConditionalOnMissingBean(DefaultMQProducer.class)
@ConditionalOnProperty(prefix = "spring.rocketmq", value = {"nameServer", "producer.group"})
public DefaultMQProducer mqProducer(RocketMQProperties rocketMQProperties) {

    RocketMQProperties.Producer producerConfig = rocketMQProperties.getProducer();
    String groupName = producerConfig.getGroup();
    Assert.hasText(groupName, "[spring.rocketmq.producer.group] must not be null");

    DefaultMQProducer producer = new DefaultMQProducer(producerConfig.getGroup());
    producer.setNamesrvAddr(rocketMQProperties.getNameServer());
    producer.setSendMsgTimeout(producerConfig.getSendMsgTimeout());
    producer.setRetryTimesWhenSendFailed(producerConfig.getRetryTimesWhenSendFailed());
    producer.setRetryTimesWhenSendAsyncFailed(producerConfig.getRetryTimesWhenSendAsyncFailed());
    producer.setMaxMessageSize(producerConfig.getMaxMessageSize());
    producer.setCompressMsgBodyOverHowmuch(producerConfig.getCompressMsgBodyOverHowmuch());
    producer.setRetryAnotherBrokerWhenNotStoreOK(producerConfig.isRetryAnotherBrokerWhenNotStoreOk());

    return producer;
}
 
Example 2
Source File: RocketMQAutoConfiguration.java    From spring-boot-starter-rocketmq with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnClass(DefaultMQProducer.class)
@ConditionalOnMissingBean(DefaultMQProducer.class)
@ConditionalOnProperty(prefix = "spring.rocketmq", value = {"name-server", "producer.group"})
public DefaultMQProducer mqProducer(RocketMQProperties rocketMQProperties) {

    RocketMQProperties.Producer producerConfig = rocketMQProperties.getProducer();
    String groupName = producerConfig.getGroup();
    Assert.hasText(groupName, "[spring.rocketmq.producer.group] must not be null");

    DefaultMQProducer producer = new DefaultMQProducer(producerConfig.getGroup());
    producer.setNamesrvAddr(rocketMQProperties.getNameServer());
    producer.setSendMsgTimeout(producerConfig.getSendMsgTimeout());
    producer.setRetryTimesWhenSendFailed(producerConfig.getRetryTimesWhenSendFailed());
    producer.setRetryTimesWhenSendAsyncFailed(producerConfig.getRetryTimesWhenSendAsyncFailed());
    producer.setMaxMessageSize(producerConfig.getMaxMessageSize());
    producer.setCompressMsgBodyOverHowmuch(producerConfig.getCompressMsgBodyOverHowmuch());
    producer.setRetryAnotherBrokerWhenNotStoreOK(producerConfig.isRetryAnotherBrokerWhenNotStoreOk());

    return producer;
}
 
Example 3
Source File: RmqClusterProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private RoundRobinPickerList<DefaultMQProducer> buildRMQProducerList() throws Exception {
    RoundRobinPickerList<DefaultMQProducer> producers = new RoundRobinPickerList<>();

    RocketmqConfiguration config = configManager.getCarreraConfig().getRocketmqConfigurationMap().get(brokerCluster);
    if (config == null) {
        LogUtils.logError("RmqClusterProducer.buildRMQProducerList", "no cluster config, cluster name:" + brokerCluster);
        throw new Exception("no cluster config, cluster name:" + brokerCluster);
    }

    String producerGroupPrefix = config.getGroupPrefix() + "_" + String.valueOf(System.currentTimeMillis());
    for (int i = 0; i < configManager.getCarreraConfig().getRocketmqProducers(); i++) {
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer(producerGroupPrefix + i);
        defaultMQProducer.setInstanceName(producerGroupPrefix + i);
        defaultMQProducer.setNamesrvAddr(StringUtils.join(config.getNamesrvAddrs().iterator(), ";"));
        defaultMQProducer.setSendMsgTimeout(config.getSendMsgTimeout());
        defaultMQProducer.setCompressMsgBodyOverHowmuch(config.getCompressMsgBodyOverHowmuch());
        defaultMQProducer.setRetryAnotherBrokerWhenNotStoreOK(config.isRetryAnotherBrokerWhenNotStoreOK());
        defaultMQProducer.setMaxMessageSize(config.getMaxMessageSize());
        defaultMQProducer.setClientCallbackExecutorThreads(config.getClientCallbackExecutorThreads());
        defaultMQProducer.setPollNameServerInterval(config.getPollNameServerInterval());
        defaultMQProducer.setHeartbeatBrokerInterval(config.getHeartbeatBrokerInterval());
        defaultMQProducer.setPersistConsumerOffsetInterval(config.getPersistConsumerOffsetInterval());
        // 同步发送,禁止rmq client重试
        defaultMQProducer.setRetryTimesWhenSendFailed(0);
        // 异步发送,禁止rmq client重试
        defaultMQProducer.setRetryTimesWhenSendAsyncFailed(0);
        defaultMQProducer.start();

        producers.add(defaultMQProducer);
    }

    return producers;
}
 
Example 4
Source File: ExtProducerResetConfiguration.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
private DefaultMQProducer createProducer(ExtRocketMQTemplateConfiguration annotation) {

        RocketMQProperties.Producer producerConfig = rocketMQProperties.getProducer();
        if (producerConfig == null) {
            producerConfig = new RocketMQProperties.Producer();
        }
        String nameServer = environment.resolvePlaceholders(annotation.nameServer());
        String groupName = environment.resolvePlaceholders(annotation.group());
        groupName = StringUtils.isEmpty(groupName) ? producerConfig.getGroup() : groupName;

        String ak = environment.resolvePlaceholders(annotation.accessKey());
        ak = StringUtils.isEmpty(ak) ? producerConfig.getAccessKey() : ak;
        String sk = environment.resolvePlaceholders(annotation.secretKey());
        sk = StringUtils.isEmpty(sk) ? producerConfig.getSecretKey() : sk;
        boolean isEnableMsgTrace = annotation.enableMsgTrace();
        String customizedTraceTopic = environment.resolvePlaceholders(annotation.customizedTraceTopic());
        customizedTraceTopic = StringUtils.isEmpty(customizedTraceTopic) ? producerConfig.getCustomizedTraceTopic() : customizedTraceTopic;

        DefaultMQProducer producer = RocketMQUtil.createDefaultMQProducer(groupName, ak, sk, isEnableMsgTrace, customizedTraceTopic);

        producer.setNamesrvAddr(nameServer);
        producer.setSendMsgTimeout(annotation.sendMessageTimeout() == -1 ? producerConfig.getSendMessageTimeout() : annotation.sendMessageTimeout());
        producer.setRetryTimesWhenSendFailed(annotation.retryTimesWhenSendFailed() == -1 ? producerConfig.getRetryTimesWhenSendFailed() : annotation.retryTimesWhenSendFailed());
        producer.setRetryTimesWhenSendAsyncFailed(annotation.retryTimesWhenSendAsyncFailed() == -1 ? producerConfig.getRetryTimesWhenSendAsyncFailed() : annotation.retryTimesWhenSendAsyncFailed());
        producer.setMaxMessageSize(annotation.maxMessageSize() == -1 ? producerConfig.getMaxMessageSize() : annotation.maxMessageSize());
        producer.setCompressMsgBodyOverHowmuch(annotation.compressMessageBodyThreshold() == -1 ? producerConfig.getCompressMessageBodyThreshold() : annotation.compressMessageBodyThreshold());
        producer.setRetryAnotherBrokerWhenNotStoreOK(annotation.retryNextServer());

        return producer;
    }
 
Example 5
Source File: RocketMQAutoConfiguration.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(DefaultMQProducer.class)
@ConditionalOnProperty(prefix = "rocketmq", value = {"name-server", "producer.group"})
public DefaultMQProducer defaultMQProducer(RocketMQProperties rocketMQProperties) {
    RocketMQProperties.Producer producerConfig = rocketMQProperties.getProducer();
    String nameServer = rocketMQProperties.getNameServer();
    String groupName = producerConfig.getGroup();
    Assert.hasText(nameServer, "[rocketmq.name-server] must not be null");
    Assert.hasText(groupName, "[rocketmq.producer.group] must not be null");

    String accessChannel = rocketMQProperties.getAccessChannel();

    String ak = rocketMQProperties.getProducer().getAccessKey();
    String sk = rocketMQProperties.getProducer().getSecretKey();
    boolean isEnableMsgTrace = rocketMQProperties.getProducer().isEnableMsgTrace();
    String customizedTraceTopic = rocketMQProperties.getProducer().getCustomizedTraceTopic();

    DefaultMQProducer producer = RocketMQUtil.createDefaultMQProducer(groupName, ak, sk, isEnableMsgTrace, customizedTraceTopic);

    producer.setNamesrvAddr(nameServer);
    if (!StringUtils.isEmpty(accessChannel)) {
        producer.setAccessChannel(AccessChannel.valueOf(accessChannel));
    }
    producer.setSendMsgTimeout(producerConfig.getSendMessageTimeout());
    producer.setRetryTimesWhenSendFailed(producerConfig.getRetryTimesWhenSendFailed());
    producer.setRetryTimesWhenSendAsyncFailed(producerConfig.getRetryTimesWhenSendAsyncFailed());
    producer.setMaxMessageSize(producerConfig.getMaxMessageSize());
    producer.setCompressMsgBodyOverHowmuch(producerConfig.getCompressMessageBodyThreshold());
    producer.setRetryAnotherBrokerWhenNotStoreOK(producerConfig.isRetryNextServer());

    return producer;
}
 
Example 6
Source File: RmqClusterProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private RoundRobinPickerList<DefaultMQProducer> buildRMQProducerList() throws Exception {
    RoundRobinPickerList<DefaultMQProducer> producers = new RoundRobinPickerList<>();

    RocketmqConfiguration config = configManager.getCarreraConfig().getRocketmqConfigurationMap().get(brokerCluster);
    if (config == null) {
        LogUtils.logError("RmqClusterProducer.buildRMQProducerList", "no cluster config, cluster name:" + brokerCluster);
        throw new Exception("no cluster config, cluster name:" + brokerCluster);
    }

    String producerGroupPrefix = config.getGroupPrefix() + "_" + String.valueOf(System.currentTimeMillis());
    for (int i = 0; i < configManager.getCarreraConfig().getRocketmqProducers(); i++) {
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer(producerGroupPrefix + i);
        defaultMQProducer.setInstanceName(producerGroupPrefix + i);
        defaultMQProducer.setNamesrvAddr(StringUtils.join(config.getNamesrvAddrs().iterator(), ";"));
        defaultMQProducer.setSendMsgTimeout(config.getSendMsgTimeout());
        defaultMQProducer.setCompressMsgBodyOverHowmuch(config.getCompressMsgBodyOverHowmuch());
        defaultMQProducer.setRetryAnotherBrokerWhenNotStoreOK(config.isRetryAnotherBrokerWhenNotStoreOK());
        defaultMQProducer.setMaxMessageSize(config.getMaxMessageSize());
        defaultMQProducer.setClientCallbackExecutorThreads(config.getClientCallbackExecutorThreads());
        defaultMQProducer.setPollNameServerInterval(config.getPollNameServerInterval());
        defaultMQProducer.setHeartbeatBrokerInterval(config.getHeartbeatBrokerInterval());
        defaultMQProducer.setPersistConsumerOffsetInterval(config.getPersistConsumerOffsetInterval());
        // 同步发送,禁止rmq client重试
        defaultMQProducer.setRetryTimesWhenSendFailed(0);
        // 异步发送,禁止rmq client重试
        defaultMQProducer.setRetryTimesWhenSendAsyncFailed(0);
        defaultMQProducer.start();

        producers.add(defaultMQProducer);
    }

    return producers;
}
 
Example 7
Source File: RocketMqAutoConfiguration.java    From spring-boot-rocketmq-starter with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnClass(DefaultMQProducer.class)
@ConditionalOnMissingBean(DefaultMQProducer.class)
public DefaultMQProducer mqProducer() {
    DefaultMQProducer producer = new DefaultMQProducer();
    producer.setProducerGroup(rocketMqProperties.getProducerGroupName());
    producer.setNamesrvAddr(rocketMqProperties.getNameServer());

    producer.setSendMsgTimeout(rocketMqProperties.getProducerSendMsgTimeout());
    producer.setRetryTimesWhenSendFailed(rocketMqProperties.getProducerRetryTimesWhenSendFailed());
    producer.setRetryTimesWhenSendAsyncFailed(rocketMqProperties.getProducerRetryTimesWhenSendAsyncFailed());
    producer.setMaxMessageSize(rocketMqProperties.getProducerMaxMessageSize());
    producer.setCompressMsgBodyOverHowmuch(rocketMqProperties.getProducerCompressMsgBodyOverHowMuch());
    producer.setRetryAnotherBrokerWhenNotStoreOK(rocketMqProperties.isProducerRetryAnotherBrokerWhenNotStoreOk());

    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        LOGGER.info("producer shutdown");
        producer.shutdown();
        LOGGER.info("producer has shutdown");
    }));

    try {
        producer.start();
        LOGGER.info("rocketmq producer started, nameserver:{}, group:{}", rocketMqProperties.getNameServer(),
                rocketMqProperties.getProducerGroupName());
    } catch (MQClientException e) {
        LOGGER.error("producer start error, nameserver:{}, group:{}", rocketMqProperties.getNameServer(),
                rocketMqProperties.getProducerGroupName(), e);
    }

    return producer;
}