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

The following examples show how to use org.apache.rocketmq.client.producer.DefaultMQProducer#setInstanceName() . 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: RocketMQSink.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
@Override
public void open(Configuration parameters) throws Exception {
    Validate.notEmpty(props, "Producer properties can not be empty");
    Validate.notNull(topicSelector, "TopicSelector can not be null");
    Validate.notNull(serializationSchema, "KeyValueSerializationSchema can not be null");

    producer = new DefaultMQProducer();
    producer.setInstanceName(String.valueOf(getRuntimeContext().getIndexOfThisSubtask()));
    RocketMQConfig.buildProducerConfigs(props, producer);

    batchList = new LinkedList<>();

    if (batchFlushOnCheckpoint && !((StreamingRuntimeContext) getRuntimeContext()).isCheckpointingEnabled()) {
        LOG.warn("Flushing on checkpoint is enabled, but checkpointing is not enabled. Disabling flushing.");
        batchFlushOnCheckpoint = false;
    }

    try {
        producer.start();
    } catch (MQClientException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: SendMsgStatusCommand.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    final DefaultMQProducer producer = new DefaultMQProducer("PID_SMSC", rpcHook);
    producer.setInstanceName("PID_SMSC_" + System.currentTimeMillis());

    try {
        producer.start();
        String brokerName = commandLine.getOptionValue('b').trim();
        int messageSize = commandLine.hasOption('s') ? Integer.parseInt(commandLine.getOptionValue('s')) : 128;
        int count = commandLine.hasOption('c') ? Integer.parseInt(commandLine.getOptionValue('c')) : 50;

        producer.send(buildMessage(brokerName, 16));

        for (int i = 0; i < count; i++) {
            long begin = System.currentTimeMillis();
            SendResult result = producer.send(buildMessage(brokerName, messageSize));
            System.out.printf("rt:" + (System.currentTimeMillis() - begin) + "ms, SendResult=%s", result);
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        producer.shutdown();
    }
}
 
Example 3
Source File: SendMsgStatusCommand.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) {
    final DefaultMQProducer producer = new DefaultMQProducer("PID_SMSC", rpcHook);
    producer.setInstanceName("PID_SMSC_" + System.currentTimeMillis());

    try {
        producer.start();
        String brokerName = commandLine.getOptionValue('b').trim();
        int messageSize = commandLine.hasOption('s') ? Integer.parseInt(commandLine.getOptionValue('s')) : 128;
        int count = commandLine.hasOption('c') ? Integer.parseInt(commandLine.getOptionValue('c')) : 50;

        producer.send(buildMessage(brokerName, 16));

        for (int i = 0; i < count; i++) {
            long begin = System.currentTimeMillis();
            SendResult result = producer.send(buildMessage(brokerName, messageSize));
            System.out.printf("rt:" + (System.currentTimeMillis() - begin) + "ms, SendResult=" + result);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        producer.shutdown();
    }
}
 
Example 4
Source File: SendMsgStatusCommand.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) {
    final DefaultMQProducer producer = new DefaultMQProducer("PID_SMSC", rpcHook);
    producer.setInstanceName("PID_SMSC_" + System.currentTimeMillis());

    try {
        producer.start();
        String brokerName = commandLine.getOptionValue('b').trim();
        int messageSize = commandLine.hasOption('s') ? Integer.parseInt(commandLine.getOptionValue('s')) : 128;
        int count = commandLine.hasOption('c') ? Integer.parseInt(commandLine.getOptionValue('c')) : 50;

        producer.send(buildMessage(brokerName, 16));

        for (int i = 0; i < count; i++) {
            long begin = System.currentTimeMillis();
            SendResult result = producer.send(buildMessage(brokerName, messageSize));
            System.out.printf("rt:" + (System.currentTimeMillis() - begin) + "ms, SendResult=" + result);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        producer.shutdown();
    }
}
 
Example 5
Source File: SendMsgStatusCommand.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    final DefaultMQProducer producer = new DefaultMQProducer("PID_SMSC", rpcHook);
    producer.setInstanceName("PID_SMSC_" + System.currentTimeMillis());

    try {
        producer.start();
        String brokerName = commandLine.getOptionValue('b').trim();
        int messageSize = commandLine.hasOption('s') ? Integer.parseInt(commandLine.getOptionValue('s')) : 128;
        int count = commandLine.hasOption('c') ? Integer.parseInt(commandLine.getOptionValue('c')) : 50;

        producer.send(buildMessage(brokerName, 16));

        for (int i = 0; i < count; i++) {
            long begin = System.currentTimeMillis();
            SendResult result = producer.send(buildMessage(brokerName, messageSize));
            System.out.printf("rt:" + (System.currentTimeMillis() - begin) + "ms, SendResult=%s", result);
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        producer.shutdown();
    }
}
 
Example 6
Source File: ExtProducerResetConfiguration.java    From rocketmq-spring with Apache License 2.0 6 votes vote down vote up
private void registerTemplate(String beanName, Object bean) {
    Class<?> clazz = AopProxyUtils.ultimateTargetClass(bean);

    if (!RocketMQTemplate.class.isAssignableFrom(bean.getClass())) {
        throw new IllegalStateException(clazz + " is not instance of " + RocketMQTemplate.class.getName());
    }

    ExtRocketMQTemplateConfiguration annotation = clazz.getAnnotation(ExtRocketMQTemplateConfiguration.class);
    GenericApplicationContext genericApplicationContext = (GenericApplicationContext) applicationContext;
    validate(annotation, genericApplicationContext);

    DefaultMQProducer mqProducer = createProducer(annotation);
    // Set instanceName same as the beanName
    mqProducer.setInstanceName(beanName);
    try {
        mqProducer.start();
    } catch (MQClientException e) {
        throw new BeanDefinitionValidationException(String.format("Failed to startup MQProducer for RocketMQTemplate {}",
            beanName), e);
    }
    RocketMQTemplate rocketMQTemplate = (RocketMQTemplate) bean;
    rocketMQTemplate.setProducer(mqProducer);
    rocketMQTemplate.setMessageConverter(rocketMQMessageConverter.getMessageConverter());
    log.info("Set real producer to :{} {}", beanName, annotation.value());
}
 
Example 7
Source File: RocketMQSink.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
@Override
public void open(Configuration parameters) throws Exception {
    Validate.notEmpty(props, "Producer properties can not be empty");
    Validate.notNull(topicSelector, "TopicSelector can not be null");
    Validate.notNull(serializationSchema, "KeyValueSerializationSchema can not be null");

    producer = new DefaultMQProducer();
    producer.setInstanceName(String.valueOf(getRuntimeContext().getIndexOfThisSubtask()));
    RocketMQConfig.buildProducerConfigs(props, producer);

    batchList = new LinkedList<>();

    if (batchFlushOnCheckpoint && !((StreamingRuntimeContext) getRuntimeContext()).isCheckpointingEnabled()) {
        LOG.warn("Flushing on checkpoint is enabled, but checkpointing is not enabled. Disabling flushing.");
        batchFlushOnCheckpoint = false;
    }

    try {
        producer.start();
    } catch (MQClientException e) {
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: Producer.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    CommandLine commandLine = buildCommandline(args);
    if (commandLine != null) {
        String group = commandLine.getOptionValue('g');
        String topic = commandLine.getOptionValue('t');
        String tags = commandLine.getOptionValue('a');
        String keys = commandLine.getOptionValue('k');
        String msgCount = commandLine.getOptionValue('c');

        DefaultMQProducer producer = new DefaultMQProducer(group);
        producer.setInstanceName(Long.toString(System.currentTimeMillis()));

        producer.start();

        for (int i = 0; i < Integer.parseInt(msgCount); i++) {
            try {
                Message msg = new Message(
                    topic,
                    tags,
                    keys,
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
                SendResult sendResult = producer.send(msg);
                System.out.printf("%-8d %s%n", i, sendResult);
            } catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(1000);
            }
        }

        producer.shutdown();
    }
}
 
Example 9
Source File: RMQAsyncSendProducer.java    From rocketmq 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 10
Source File: Producer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    CommandLine commandLine = buildCommandline(args);
    if (commandLine != null) {
        String group = commandLine.getOptionValue('g');
        String topic = commandLine.getOptionValue('t');
        String tags = commandLine.getOptionValue('a');
        String keys = commandLine.getOptionValue('k');
        String msgCount = commandLine.getOptionValue('c');

        DefaultMQProducer producer = new DefaultMQProducer(group);
        producer.setInstanceName(Long.toString(System.currentTimeMillis()));

        producer.start();

        for (int i = 0; i < Integer.parseInt(msgCount); i++) {
            try {
                Message msg = new Message(
                    topic,
                    tags,
                    keys,
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
                SendResult sendResult = producer.send(msg);
                System.out.printf("%-8d %s%n", i, sendResult);
            } catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(1000);
            }
        }

        producer.shutdown();
    }
}
 
Example 11
Source File: RMQAsyncSendProducer.java    From rocketmq_trans_message 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 12
Source File: RMQNormalProducer.java    From rocketmq-read 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 13
Source File: RMQNormalProducer.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
protected void create() {
    producer = new DefaultMQProducer();
    producer.setProducerGroup(getProducerGroupName());
    producer.setInstanceName(getProducerInstanceName());

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

}
 
Example 14
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 15
Source File: RMQNormalProducer.java    From rocketmq-4.3.0 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 16
Source File: Producer.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    CommandLine commandLine = buildCommandline(args);
    if (commandLine != null) {
        String group = commandLine.getOptionValue('g');
        String topic = commandLine.getOptionValue('t');
        String tags = commandLine.getOptionValue('a');
        String keys = commandLine.getOptionValue('k');
        String msgCount = commandLine.getOptionValue('c');

        DefaultMQProducer producer = new DefaultMQProducer(group);
        producer.setInstanceName(Long.toString(System.currentTimeMillis()));

        producer.start();

        for (int i = 0; i < Integer.parseInt(msgCount); i++) {
            try {
                Message msg = new Message(
                    topic,
                    tags,
                    keys,
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
                SendResult sendResult = producer.send(msg);
                System.out.printf("%-8d %s%n", i, sendResult);
            } catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(1000);
            }
        }

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

    return producer;
}
 
Example 18
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 19
Source File: RMQAsyncSendProducer.java    From rocketmq 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 20
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();
}