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

The following examples show how to use org.apache.rocketmq.client.producer.DefaultMQProducer#setRetryTimesWhenSendFailed() . 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: SecKillChargeOrderProducer.java    From seckill-rocketmq with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    defaultMQProducer =
            new DefaultMQProducer
                    (MessageProtocolConst.SECKILL_CHARGE_ORDER_TOPIC.getProducerGroup(),
                            new AclClientRPCHook(new SessionCredentials(aclAccessKey, aclAccessSecret)));
    defaultMQProducer.setNamesrvAddr(namesrvConfig.nameSrvAddr());
    // 发送失败重试次数
    defaultMQProducer.setRetryTimesWhenSendFailed(3);
    try {
        defaultMQProducer.start();
    } catch (MQClientException e) {
        LOGGER.error("[秒杀订单生产者]--SecKillChargeOrderProducer加载异常!e={}", LogExceptionWapper.getStackTrace(e));
        throw new RuntimeException("[秒杀订单生产者]--SecKillChargeOrderProducer加载异常!", e);
    }
    LOGGER.info("[秒杀订单生产者]--SecKillChargeOrderProducer加载完成!");
}
 
Example 2
Source File: RocketMQConfig.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
/**
 * 构建 producer 配置
 *
 * @param props    Properties
 * @param producer DefaultMQProducer
 */
public static void buildProducerConfigs(Properties props, DefaultMQProducer producer) {
    buildCommonConfigs(props, producer);

    String group = props.getProperty(PRODUCER_GROUP);
    if (StringUtils.isEmpty(group)) {
        group = UUID.randomUUID().toString();
    }
    producer.setProducerGroup(props.getProperty(PRODUCER_GROUP, group));

    producer.setRetryTimesWhenSendFailed(getInteger(props,
            PRODUCER_RETRY_TIMES, DEFAULT_PRODUCER_RETRY_TIMES));
    producer.setRetryTimesWhenSendAsyncFailed(getInteger(props,
            PRODUCER_RETRY_TIMES, DEFAULT_PRODUCER_RETRY_TIMES));
    producer.setSendMsgTimeout(getInteger(props,
            PRODUCER_TIMEOUT, DEFAULT_PRODUCER_TIMEOUT));
}
 
Example 3
Source File: CanalRocketMQProducer.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
@Override
public void init(MQProperties rocketMQProperties) {
    this.mqProperties = rocketMQProperties;
    RPCHook rpcHook = null;
    if (rocketMQProperties.getAliyunAccessKey().length() > 0
        && rocketMQProperties.getAliyunSecretKey().length() > 0) {
        SessionCredentials sessionCredentials = new SessionCredentials();
        sessionCredentials.setAccessKey(rocketMQProperties.getAliyunAccessKey());
        sessionCredentials.setSecretKey(rocketMQProperties.getAliyunSecretKey());
        rpcHook = new ClientRPCHook(sessionCredentials);
    }

    defaultMQProducer = new DefaultMQProducer(rocketMQProperties.getProducerGroup(), rpcHook);
    defaultMQProducer.setNamesrvAddr(rocketMQProperties.getServers());
    defaultMQProducer.setRetryTimesWhenSendFailed(rocketMQProperties.getRetries());
    defaultMQProducer.setVipChannelEnabled(false);
    logger.info("##Start RocketMQ producer##");
    try {
        defaultMQProducer.start();
    } catch (MQClientException ex) {
        throw new CanalServerException("Start RocketMQ producer error", ex);
    }
}
 
Example 4
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 5
Source File: RocketMQConfig.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
/**
 * 构建 producer 配置
 *
 * @param props    Properties
 * @param producer DefaultMQProducer
 */
public static void buildProducerConfigs(Properties props, DefaultMQProducer producer) {
    buildCommonConfigs(props, producer);

    String group = props.getProperty(PRODUCER_GROUP);
    if (StringUtils.isEmpty(group)) {
        group = UUID.randomUUID().toString();
    }
    producer.setProducerGroup(props.getProperty(PRODUCER_GROUP, group));

    producer.setRetryTimesWhenSendFailed(getInteger(props,
            PRODUCER_RETRY_TIMES, DEFAULT_PRODUCER_RETRY_TIMES));
    producer.setRetryTimesWhenSendAsyncFailed(getInteger(props,
            PRODUCER_RETRY_TIMES, DEFAULT_PRODUCER_RETRY_TIMES));
    producer.setSendMsgTimeout(getInteger(props,
            PRODUCER_TIMEOUT, DEFAULT_PRODUCER_TIMEOUT));
}
 
Example 6
Source File: RocketmqConfig.java    From java-tutorial with MIT License 6 votes vote down vote up
/**
 * 初始化向rocketmq发送普通消息的生产者
 *
 * @return
 * @throws MQClientException
 */
@Bean
public DefaultMQProducer defaultMQProducer() throws MQClientException {
    DefaultMQProducer producer = new DefaultMQProducer("rock-test");
    producer.setNamesrvAddr(producerParma.getNamesrvAddr());
    producer.setMaxMessageSize(producerParma.getMaxMessageSize());
    producer.setSendMsgTimeout(producerParma.getSendMsgTimeout());
    //mq server版本小于3.5.8,请设置不使用
    /// producer.setVipChannelEnabled(true);
    producer.setRetryTimesWhenSendFailed(producerParma.getRetryTimesWhenSendFailed());

    //Producer对象start初始化
    producer.start();

    logger.info("RocketMQ defaultProducer Started.");
    return producer;
}
 
Example 7
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 8
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 9
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 10
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 11
Source File: SyncProducer.java    From java-tutorial with MIT License 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    //声明并初始化一个producer
    DefaultMQProducer producer = new DefaultMQProducer("SYNC_MQ_GROUP");
    //设置NameServer地址,此处应改为实际NameServer地址,多个地址之间用;分隔
    producer.setNamesrvAddr(NAMESRVADDR);
    producer.setInstanceName("SYNC_PRODUCER");
    // 设置重试次数,默认2
    producer.setRetryTimesWhenSendFailed(3);
    //大于3.5.3版本要设置为false 否则取不到topic
    producer.setVipChannelEnabled(false);
    //设置发送超时时间,默认是3000
    producer.setSendMsgTimeout(10000);
    //调用start()方法启动一个producer实例
    producer.start();
    producer.setRetryTimesWhenSendAsyncFailed(0);

    System.out.println("producer start");
    for (int i = 0; i < 10; i++) {
        try {
            Message msg = new Message("Sync_TopicA_Test",
                    "Sync_TagA",
                    ("RocketMQ message " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
            );
            //发送消息
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送完消息之后,调用shutdown()方法关闭producer
    producer.shutdown();

    System.out.println("producer shutdown");
}
 
Example 12
Source File: AsyncProducer.java    From java-tutorial with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    //Instantiate with a producer group name.
    DefaultMQProducer producer = new DefaultMQProducer("ASYNC_MQ_GROUP");
    // Specify name server addresses.
    producer.setNamesrvAddr(SyncProducer.NAMESRVADDR);
    producer.setInstanceName("ASYNC_PRODUCER");
    producer.setVipChannelEnabled(false);
    // 设置重试次数,默认2
    producer.setRetryTimesWhenSendFailed(3);
    //设置发送超时时间,默认是3000
    producer.setSendMsgTimeout(10000);

    //Launch the instance.
    producer.start();
    producer.setRetryTimesWhenSendAsyncFailed(0);

    for (int i = 0; i < 10; i++) {
        final int index = i;
        Message msg = new Message("Async_Topic_Test",
                "Async_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();
            }
        }, 10000);
    }
    //Shut down once the producer instance is not longer in use.
    producer.shutdown();
}
 
Example 13
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 14
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;
}