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

The following examples show how to use org.apache.rocketmq.client.producer.DefaultMQProducer#setNamesrvAddr() . 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: ProducerTest.java    From message-queue-client-framework with Apache License 2.0 6 votes vote down vote up
@Test
    public void test() throws Exception {

        DefaultMQProducer defaultMQProducer = new DefaultMQProducer();
        defaultMQProducer.setNamesrvAddr("localhost:9876");
        defaultMQProducer.setProducerGroup("protest");
        defaultMQProducer.start();

        MessageProducer messageProducer = new MessageProducer();
        messageProducer.setDefaultMQProducer(defaultMQProducer);
        messageProducer.setMessageEncoder(new RocketmqMessageEncoderDemo());
        messageProducer.setTopic("QUEUE_TEST");

//        for (int i = 0; i < 100; i++)
//
//            messageProducer.sendWithKey(String.valueOf(i), "test"+i);

        defaultMQProducer.shutdown();
    }
 
Example 2
Source File: ProducerInstance.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
public static MQProducer getInstance(String nameServerAddress, String group) throws MQClientException {
    if (group == null) {
        group = DEFAULT_GROUP;
    }

    String genKey = genKey(nameServerAddress, group);
    MQProducer p = producerMap.get(genKey);
    if (p != null) {
        return p;
    }

    DefaultMQProducer defaultMQProducer = new DefaultMQProducer(group);
    defaultMQProducer.setNamesrvAddr(nameServerAddress);
    MQProducer beforeProducer = null;
    //cas put producer
    beforeProducer = producerMap.putIfAbsent(genKey, defaultMQProducer);
    if (beforeProducer != null) {
        return beforeProducer;
    }
    defaultMQProducer.start();
    return defaultMQProducer;
}
 
Example 3
Source File: ProducerInstance.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public MQProducer getInstance(String nameServerAddress, String group) throws MQClientException {
    if (StringUtils.isBlank(group)) {
        group = DEFAULT_GROUP;
    }

    String genKey = genKey(nameServerAddress, group);
    MQProducer p = getProducerInstance().producerMap.get(genKey);
    if (p != null) {
        return p;
    }

    DefaultMQProducer defaultMQProducer = new DefaultMQProducer(group);
    defaultMQProducer.setNamesrvAddr(nameServerAddress);
    MQProducer beforeProducer = null;
    beforeProducer = getProducerInstance().producerMap.putIfAbsent(genKey, defaultMQProducer);
    if (beforeProducer != null) {
        return beforeProducer;
    }
    defaultMQProducer.start();
    return defaultMQProducer;
}
 
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: Producer.java    From code with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {

        DefaultMQProducer producer = new DefaultMQProducer("test_quick_producer_name");

        producer.setNamesrvAddr(Const.NAMESRV_ADDR_SINGLE);

        producer.start();

        for (int i = 0; i < 5; i++) {
            //	1、创建消息
            Message message = new Message("test_quick_topic",    //	主题
                    "TagA", //	标签
                    "key" + i,    // 	用户自定义的key ,唯一的标识
                    ("Hello RocketMQ" + i).getBytes());    //	消息内容实体(byte[])
            // 2、发送消息
            SendResult sr = producer.send(message);
            System.err.println("消息发出:" + sr);
        }
        producer.shutdown();

    }
 
Example 6
Source File: BatchProducer.java    From javatech with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
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 7
Source File: ProducerInstance.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public MQProducer getInstance(String nameServerAddress, String group) throws MQClientException {
    if (StringUtils.isBlank(group)) {
        group = DEFAULT_GROUP;
    }

    String genKey = genKey(nameServerAddress, group);
    MQProducer p = getProducerInstance().producerMap.get(genKey);
    if (p != null) {
        return p;
    }

    DefaultMQProducer defaultMQProducer = new DefaultMQProducer(group);
    defaultMQProducer.setNamesrvAddr(nameServerAddress);
    MQProducer beforeProducer = null;
    beforeProducer = getProducerInstance().producerMap.putIfAbsent(genKey, defaultMQProducer);
    if (beforeProducer != null) {
        return beforeProducer;
    }
    defaultMQProducer.start();
    return defaultMQProducer;
}
 
Example 8
Source File: SyncProducer.java    From blog with MIT License 6 votes vote down vote up
public static void main(String[] args)
    throws MQClientException, RemotingException, InterruptedException, MQBrokerException {

  /** TODO: 创建 Producer 对象 */
  DefaultMQProducer defaultMQProducer = new DefaultMQProducer("Sync_Producer");

  /** TODO: 这是 nameserver 地址,多个地址以分号分隔 */
  defaultMQProducer.setNamesrvAddr("127.0.0.1:9876");
  defaultMQProducer.start();

  for (int i = 0; i < 10; i++) {
    /** TODO: 创建发送信息 topic:发送的消息的地址, body:发送的数据, */
    Message message = new Message("MyTopic", "Sync Producer", ("Sync Producer " + i).getBytes());

    /** TODO: 同步发送消息 并获得消息结果 */
    SendResult result = defaultMQProducer.send(message);
    System.out.println(result);
  }

  /** TODO: 断开连接 */
  defaultMQProducer.shutdown();
}
 
Example 9
Source File: Producer.java    From code with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {

        DefaultMQProducer producer = new DefaultMQProducer("test_async_producer_name");

        producer.setNamesrvAddr(Const.NAMESRV_ADDR_SINGLE);

        producer.start();

        for (int i = 0; i < 5; i++) {
            //	1、创建消息
            Message message = new Message("test_quick_topic",    //	主题
                    "TagA", //	标签
                    "key" + i,    // 	用户自定义的key ,唯一的标识
                    ("Hello RocketMQ" + i).getBytes());    //	消息内容实体(byte[])
            // 2、发送消息
            //SendResult sr = producer.send(message);
            //System.err.println("消息发出:" + sr);
            producer.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.err.println("msgId: " + sendResult.getMsgId() + ", status: " + sendResult.getSendStatus());
                }

                @Override
                public void onException(Throwable e) {
                    e.printStackTrace();
                    System.err.println("------发送失败");
                }
            });

        }

        //producer.shutdown();

    }
 
Example 10
Source File: RMQAsyncSendProducer.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private void create() {
    producer = new DefaultMQProducer();
    producer.setProducerGroup(RandomUtil.getStringByUUID());
    producer.setInstanceName(RandomUtil.getStringByUUID());

    if (nsAddr != null) {
        producer.setNamesrvAddr(nsAddr);
    }

}
 
Example 11
Source File: RMQNormalProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
protected void create(boolean useTLS) {
    producer = new DefaultMQProducer();
    producer.setProducerGroup(getProducerGroupName());
    producer.setInstanceName(getProducerInstanceName());
    producer.setUseTLS(useTLS);

    if (nsAddr != null) {
        producer.setNamesrvAddr(nsAddr);
    }
}
 
Example 12
Source File: ProducerFactory.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public static DefaultMQProducer getRMQProducer(String ns) {
    DefaultMQProducer producer = new DefaultMQProducer(RandomUtil.getStringByUUID());
    producer.setNamesrvAddr(ns);
    try {
        producer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
    }

    return producer;
}
 
Example 13
Source File: ProducerFactory.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
public static DefaultMQProducer getRMQProducer(String ns) {
    DefaultMQProducer producer = new DefaultMQProducer(RandomUtil.getStringByUUID());
    producer.setNamesrvAddr(ns);
    try {
        producer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
    }

    return producer;
}
 
Example 14
Source File: RMQAsyncSendProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private void create() {
    producer = new DefaultMQProducer();
    producer.setProducerGroup(RandomUtil.getStringByUUID());
    producer.setInstanceName(RandomUtil.getStringByUUID());

    if (nsAddr != null) {
        producer.setNamesrvAddr(nsAddr);
    }

}
 
Example 15
Source File: OneWayProducer.java    From blog with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {

		DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName2");
		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.sendOneway(msg);
		}
		producer.shutdown();
	}
 
Example 16
Source File: OrderNotifySendProducer.java    From order-charge-notify with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {

    defaultMQProducer =
            new DefaultMQProducer(MessageProtocolConst.ORDER_RESULT_NOTIFY_TOPIC.getProducerGroup());
    defaultMQProducer.setNamesrvAddr(nameSrvAddr);
    try {
        defaultMQProducer.start();
    } catch (MQClientException e) {
        LOGGER.error("[订单通知发送生产者]--OrderNotifySendProducer加载异常!e={}", LogExceptionWapper.getStackTrace(e));
        throw new RuntimeException("[订单通知发送生产者]--OrderNotifySendProducer加载异常!", e);
    }
    LOGGER.info("[订单通知发送生产者]--OrderNotifySendProducer加载完成!");
}
 
Example 17
Source File: RMQAsyncSendProducer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
private void create() {
    producer = new DefaultMQProducer();
    producer.setProducerGroup(RandomUtil.getStringByUUID());
    producer.setInstanceName(RandomUtil.getStringByUUID());

    if (nsAddr != null) {
        producer.setNamesrvAddr(nsAddr);
    }

}
 
Example 18
Source File: AsyncProducer.java    From netty-chat with Apache License 2.0 5 votes vote down vote up
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 19
Source File: RocketmqOfflineInfoHelper.java    From netty-chat with Apache License 2.0 5 votes vote down vote up
public RocketmqOfflineInfoHelper() {
    producer = new DefaultMQProducer("send");
    producer.setNamesrvAddr("localhost:9876");
    producer.setVipChannelEnabled(false);
    try {
        producer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
    }
}
 
Example 20
Source File: OrderProducer.java    From blog with MIT License 4 votes vote down vote up
public static void main(String[] args)
    throws MQClientException, UnsupportedEncodingException, RemotingException,
        InterruptedException, MQBrokerException {
  /** TODO: 创建消息生产者 */
  DefaultMQProducer producer = new DefaultMQProducer("Order_Producer");
  /** TODO: 设置 nameserver 地址,并连接 */
  producer.setNamesrvAddr("127.0.0.1:9876");
  producer.start();

  /** TODO: 发送消息 */
  String[] tags = {"TagA", "TagB", "TagC"};
  for (int i = 0; i < 30; i++) {
    /** TODO: 设置消息信息 */
    Message message =
        new Message(
            "OrderTopic",
            tags[i % tags.length],
            "KEY_" + i,
            ("Order Message ID " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));

    /** TODO: 发送顺序消息 */
    SendResult result =
        producer.send(
            message,
            // 设置消息消息存入哪个队列
            new MessageQueueSelector() {
              @Override
              public MessageQueue select(
                  // 当前 topic 里包含的所有 queue
                  List<MessageQueue> mqs,
                  // 当前发送的消息
                  Message msg,
                  // 对应到 send 方法中的 arg
                  Object arg) {

                Integer id = (Integer) arg;
                int index = id % mqs.size();
                System.out.println(
                    "msg info " + new String(msg.getBody()) + "  queue index " + index);
                // 返回当前消息 存入的队列
                return mqs.get(index);
              }
            },
            i);
    System.out.println(result);
  }

  /** TODO: 断开连接 */
  producer.shutdown();
}