Java Code Examples for com.alibaba.rocketmq.client.producer.TransactionMQProducer#start()

The following examples show how to use com.alibaba.rocketmq.client.producer.TransactionMQProducer#start() . 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: TransactionProducer.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {

        //事务决断处理类
        TransactionCheckListener transactionCheckListener = new TransactionCheckListenerImpl();
        TransactionMQProducer producer = new TransactionMQProducer("please_rename_unique_group_name");
        producer.setCheckThreadPoolMinSize(2);
        producer.setCheckThreadPoolMaxSize(2);
        producer.setCheckRequestHoldMax(2000);
        producer.setTransactionCheckListener(transactionCheckListener);
        producer.start();

        String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD", "TagE" };
        TransactionExecuterImpl tranExecuter = new TransactionExecuterImpl();
        for (int i = 0; i < 100; i++) {
            try {
                Message msg =
                        new Message("TopicTest", tags[i % tags.length], "KEY" + i,
                            ("Hello RocketMQ " + i).getBytes());
                SendResult sendResult = producer.sendMessageInTransaction(msg, tranExecuter, null);
                System.out.println(sendResult);

                Thread.sleep(10);
            }
            catch (MQClientException e) {
                e.printStackTrace();
            }
        }

        for (int i = 0; i < 100000; i++) {
            Thread.sleep(1000);
        }

        producer.shutdown();

    }
 
Example 2
Source File: MqProducer.java    From RocketMqCurrencyBoot with Apache License 2.0 5 votes vote down vote up
@PostConstruct
private void init() throws MQClientException
{
	if (transaction == null || transactionExecuter == null)
	{
		DefaultMQProducer defaultProducer = new DefaultMQProducer();
		// Producer 组名, 多个 Producer 如果属于一 个应用,发送同样的消息,则应该将它们 归为同一组
		defaultProducer.setProducerGroup(ProducerGroupName);
		// Name Server 地址列表
		defaultProducer.setNamesrvAddr(NamesrvAddr);
		// 生产者名称
		defaultProducer.setInstanceName(InstanceName);
		// 超时时间
		defaultProducer.setSendMsgTimeout(SendMsgTimeout);
		defaultProducer.start();
		producer = defaultProducer;
	}
	else
	{
		TransactionMQProducer transactionProducer = new TransactionMQProducer();
		// Producer 组名, 多个 Producer 如果属于一 个应用,发送同样的消息,则应该将它们 归为同一组
		transactionProducer.setProducerGroup(ProducerGroupName);
		// Name Server 地址列表
		transactionProducer.setNamesrvAddr(NamesrvAddr);
		// 生产者名称
		transactionProducer.setInstanceName(InstanceName);
		// 超时时间
		transactionProducer.setSendMsgTimeout(SendMsgTimeout);
		transactionProducer.setCheckThreadPoolMinSize(checkThreadPoolMinSize);
		transactionProducer.setCheckThreadPoolMaxSize(checkThreadPoolMaxSize);
		transactionProducer.setCheckRequestHoldMax(checkRequestHoldMax);
		transactionProducer.setTransactionCheckListener(transaction);
		transactionProducer.start();
		producer = transactionProducer;
	}

}
 
Example 3
Source File: TransactionProducer.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {

        TransactionCheckListener transactionCheckListener = new TransactionCheckListenerImpl();
        TransactionMQProducer producer = new TransactionMQProducer("please_rename_unique_group_name");
        // 事务回查最小并发数
        producer.setCheckThreadPoolMinSize(2);
        // 事务回查最大并发数
        producer.setCheckThreadPoolMaxSize(2);
        // 队列数
        producer.setCheckRequestHoldMax(2000);
        producer.setTransactionCheckListener(transactionCheckListener);
        producer.start();

        String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD", "TagE" };
        TransactionExecuterImpl tranExecuter = new TransactionExecuterImpl();
        for (int i = 0; i < 100; i++) {
            try {
                Message msg = new Message("TopicTest", tags[i % tags.length], "KEY" + i,
                    ("Hello RocketMQ " + i).getBytes());
                SendResult sendResult = producer.sendMessageInTransaction(msg, tranExecuter, null);
                System.out.println(sendResult);

                Thread.sleep(10);
            }
            catch (MQClientException e) {
                e.printStackTrace();
            }
        }

        for (int i = 0; i < 100000; i++) {
            Thread.sleep(1000);
        }

        producer.shutdown();

    }