Java Code Examples for org.apache.rocketmq.client.producer.DefaultMQProducer#setRetryTimesWhenSendAsyncFailed()
The following examples show how to use
org.apache.rocketmq.client.producer.DefaultMQProducer#setRetryTimesWhenSendAsyncFailed() .
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: RocketMQConfig.java From flink-learning with Apache License 2.0 | 6 votes |
/** * 构建 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 2
Source File: RocketMQAutoConfiguration.java From rocketmq-spring-boot-starter with Apache License 2.0 | 6 votes |
@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 3
Source File: RocketMQConfig.java From flink-learning with Apache License 2.0 | 6 votes |
/** * 构建 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 4
Source File: RocketMQAutoConfiguration.java From spring-boot-starter-rocketmq with Apache License 2.0 | 6 votes |
@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 5
Source File: BatchProducer.java From javatech with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
public static void main(String[] args) throws Exception { // Instantiate with a producer group name. DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup"); producer.setNamesrvAddr(RocketConfig.HOST); producer.start(); producer.setRetryTimesWhenSendAsyncFailed(0); String topic = "BatchTest"; List<Message> messages = new ArrayList<>(); messages.add(new Message(topic, "TagA", "OrderID001", "Hello world 0".getBytes())); messages.add(new Message(topic, "TagA", "OrderID002", "Hello world 1".getBytes())); messages.add(new Message(topic, "TagA", "OrderID003", "Hello world 2".getBytes())); // then you could split the large list into small ones: ListSplitter splitter = new ListSplitter(messages); while (splitter.hasNext()) { List<Message> listItem = splitter.next(); producer.send(listItem); } // Shut down once the producer instance is not longer in use. producer.shutdown(); }
Example 6
Source File: AsyncProducer.java From blog with BSD 2-Clause "Simplified" License | 6 votes |
public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, UnsupportedEncodingException { DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName2"); producer.setRetryTimesWhenSendAsyncFailed(3); producer.setNamesrvAddr("192.168.237.128:9876"); producer.start(); for (int i = 0; i < 1; i++) { Message msg = new Message("TopicTest6", "TagA", ("Hello RocketMQ" + i).getBytes(RemotingHelper.DEFAULT_CHARSET)); producer.send(msg, new SendCallback() { @Override public void onSuccess(SendResult sendResult) { System.out.println(sendResult); } @Override public void onException(Throwable e) { e.printStackTrace(); } }); } }
Example 7
Source File: RocketMqAutoConfiguration.java From spring-boot-rocketmq-starter with Apache License 2.0 | 5 votes |
@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; }
Example 8
Source File: AsyncProducer.java From netty-chat with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { DefaultMQProducer producer = new DefaultMQProducer("aaa"); producer.setNamesrvAddr("127.0.0.1:9876"); producer.start(); producer.setRetryTimesWhenSendAsyncFailed(0); for (int i = 0; i < 10; i++) { int index = i; 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(); } }); } Thread.sleep(1000); producer.shutdown(); }
Example 9
Source File: AsyncProducer.java From blog with MIT License | 5 votes |
public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException { /** TODO:创建消息生产者 */ DefaultMQProducer defaultMQProducer = new DefaultMQProducer("Async_Producer"); /** TODO: 连接 nameserver */ defaultMQProducer.setNamesrvAddr("127.0.0.1:9876"); defaultMQProducer.start(); /** TODO: 设置重投次数,默认值为 2 */ defaultMQProducer.setRetryTimesWhenSendAsyncFailed(0); /** TODO: 异步发送消息 */ Message message = new Message("MyTopic", "Async Producer", "Async Hello World".getBytes()); defaultMQProducer.send( message, new SendCallback() { @Override public void onSuccess(SendResult sendResult) { System.out.printf("发送成功:%s", sendResult); defaultMQProducer.shutdown(); } @Override public void onException(Throwable e) { // TODO: 捕获发送的异常,对发送未成功的消息 进行重投 e.printStackTrace(); } }); }
Example 10
Source File: SimpleAsyncProducer.java From spring-boot-starter-samples with Apache License 2.0 | 5 votes |
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(); }
Example 11
Source File: AsyncProducer.java From javatech with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
public static void main(String[] args) throws Exception { // Instantiate with a producer group name. DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup"); producer.setNamesrvAddr(RocketConfig.HOST); // 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(); }
Example 12
Source File: RmqClusterProducer.java From DDMQ with Apache License 2.0 | 5 votes |
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 13
Source File: AsyncProducer.java From java-tutorial with MIT License | 5 votes |
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 14
Source File: SyncProducer.java From java-tutorial with MIT License | 5 votes |
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 15
Source File: RocketMQAutoConfiguration.java From rocketmq-spring with Apache License 2.0 | 5 votes |
@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 16
Source File: ExtProducerResetConfiguration.java From rocketmq-spring with Apache License 2.0 | 5 votes |
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 17
Source File: RmqClusterProducer.java From DDMQ with Apache License 2.0 | 5 votes |
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; }