Java Code Examples for org.apache.rocketmq.client.producer.TransactionMQProducer#setExecutorService()

The following examples show how to use org.apache.rocketmq.client.producer.TransactionMQProducer#setExecutorService() . 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: OrderStatusUpdateProducer.java    From order-charge-notify with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    // 初始化回查线程池
    executorService = new ThreadPoolExecutor(
            5,
            512,
            10000L,
            TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(512),
            runnable -> {
                Thread thread = new Thread(runnable);
                thread.setName(MessageProtocolConst.ORDER_STATUS_UPDATE_TOPIC.getProducerGroup() + "-check-thread");
                return null;
            });

    transactionMQProducer = new TransactionMQProducer(MessageProtocolConst.ORDER_STATUS_UPDATE_TOPIC.getProducerGroup());
    transactionMQProducer.setNamesrvAddr(nameSrvAddr);
    transactionMQProducer.setExecutorService(executorService);
    transactionMQProducer.setTransactionListener(transactionListener);
    try {
        transactionMQProducer.start();
    } catch (MQClientException e) {
        throw new RuntimeException("启动[订单状态修改生产者]OrderStatusUpdateProducer异常", e);
    }
    LOGGER.info("启动[订单状态修改生产者]OrderStatusUpdateProducer成功, topic={}", MessageProtocolConst.ORDER_STATUS_UPDATE_TOPIC.getTopic());
}
 
Example 2
Source File: ChargeOrderPaymentTranProducer.java    From order-charge-notify with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    // 初始化回查线程池
    executorService = new ThreadPoolExecutor(
            5,
            512,
            10000L,
            TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(512),
            runnable -> {
                Thread thread = new Thread(runnable);
                thread.setName(MessageProtocolConst.WALLET_PAYMENT_TOPIC.getProducerGroup() + "-check-thread");
                return null;
            });

    transactionMQProducer = new TransactionMQProducer(MessageProtocolConst.WALLET_PAYMENT_TOPIC.getProducerGroup());
    transactionMQProducer.setNamesrvAddr(nameSrvAddr);
    transactionMQProducer.setExecutorService(executorService);
    transactionMQProducer.setTransactionListener(transactionListener);
    try {
        transactionMQProducer.start();
    } catch (MQClientException e) {
        throw new RuntimeException("启动[扣款事务消息生产者]ChargeOrderPaymentTranProducer异常", e);
    }
    LOGGER.info("启动[扣款事务消息生产者]ChargeOrderPaymentTranProducer成功, topic={}", MessageProtocolConst.WALLET_PAYMENT_TOPIC.getTopic());
}
 
Example 3
Source File: TransactionProducer.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 {
    TransactionListener transactionListener = new TransactionListenerImpl();
    TransactionMQProducer producer = new TransactionMQProducer("please_rename_unique_group_name");
    ExecutorService executorService = new ThreadPoolExecutor(2, 5, 100, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2000), new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("client-transaction-msg-check-thread");
            return thread;
        }
    });

    producer.setExecutorService(executorService);
    producer.setTransactionListener(transactionListener);
    producer.start();

    String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
    for (int i = 0; i < 10; i++) {
        try {
            Message msg =
                new Message("TopicTest1234", tags[i % tags.length], "KEY" + i,
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.sendMessageInTransaction(msg, null);
            System.out.printf("%s%n", sendResult);

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

    for (int i = 0; i < 100000; i++) {
        Thread.sleep(1000);
    }
    producer.shutdown();
}
 
Example 4
Source File: TransactionProducer.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    TransactionListener transactionListener = new TransactionListenerImpl();
    TransactionMQProducer producer = new TransactionMQProducer("please_rename_unique_group_name");
    ExecutorService executorService = new ThreadPoolExecutor(2, 5, 100, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2000), new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("client-transaction-msg-check-thread");
            return thread;
        }
    });

    producer.setExecutorService(executorService);
    producer.setTransactionListener(transactionListener);
    producer.start();

    String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
    for (int i = 0; i < 10; i++) {
        try {
            Message msg =
                new Message("TopicTest1234", tags[i % tags.length], "KEY" + i,
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.sendMessageInTransaction(msg, null);
            System.out.printf("%s%n", sendResult);

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

    for (int i = 0; i < 100000; i++) {
        Thread.sleep(1000);
    }
    producer.shutdown();
}
 
Example 5
Source File: RocketMQConfig.java    From SpringMVC-Project with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws MQClientException {
    clearUserProducer = new TransactionMQProducer(clearUserGroup);
    clearUserProducer.setNamesrvAddr(nameServerAddress);
    ExecutorService executorService = new ThreadPoolExecutor(2, 5, 100, TimeUnit.SECONDS, new ArrayBlockingQueue<>(2000), r -> {
        Thread thread = new Thread(r);
        thread.setName("client-transaction-msg-check-thread");
        return thread;
    });
    clearUserProducer.setExecutorService(executorService);
    clearUserProducer.setTransactionListener(applicationContext.getAutowireCapableBeanFactory().getBean(ClearUserTransactionListener.class));
    clearUserProducer.start();

    clearUserConsumer = new DefaultMQPushConsumer(clearUserGroup);
    clearUserConsumer.setNamesrvAddr(nameServerAddress);
    clearUserConsumer.subscribe(clearUserTopic, "*");
    clearUserConsumer.registerMessageListener(applicationContext.getAutowireCapableBeanFactory().getBean(ClearUserMessageListener.class));
    clearUserConsumer.start();

    chatRecordProducer = new DefaultMQProducer(chatRecordGroup);
    chatRecordProducer.setNamesrvAddr(nameServerAddress);
    chatRecordProducer.start();

    chatRecordConsumer = new DefaultMQPushConsumer(chatRecordGroup);
    chatRecordConsumer.setNamesrvAddr(nameServerAddress);
    chatRecordConsumer.subscribe(chatRecordTopic, chatRecordTags);
    chatRecordConsumer.registerMessageListener(applicationContext.getAutowireCapableBeanFactory().getBean(ChatRecordMessageListener.class));
    chatRecordConsumer.start();
}
 
Example 6
Source File: TransactionProducerTest.java    From blog with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
	TransactionListener transactionListener = new TransactionListenerImpl();
	TransactionMQProducer producer = new TransactionMQProducer("transactionProducerGroupName");
	producer.setNamesrvAddr("192.168.237.128:9876");
	ExecutorService executorService = new ThreadPoolExecutor(2, 5, 100, TimeUnit.SECONDS,
			new ArrayBlockingQueue<Runnable>(2000), new ThreadFactory() {
				@Override
				public Thread newThread(Runnable r) {
					Thread thread = new Thread(r);
					thread.setName("client-transaction-msg-check-thread");
					return thread;
				}
			});

	producer.setExecutorService(executorService);
	producer.setTransactionListener(transactionListener);
	producer.start();

	String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD", "TagE" };
	for (int i = 0; i < 1; i++) {
		try {
			Message msg = new Message("TopicTest1234", tags[i % tags.length], "KEY" + i,
					("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
			System.out.println("start send message " + msg);
			SendResult sendResult = producer.sendMessageInTransaction(msg, null);
			System.out.printf("%s%n", sendResult);

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

	for (int i = 0; i < 100000; i++) {
		Thread.sleep(1000);
	}
	producer.shutdown();
}
 
Example 7
Source File: TransactionProducer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    TransactionListener transactionListener = new TransactionListenerImpl();
    TransactionMQProducer producer = new TransactionMQProducer("please_rename_unique_group_name");
    ExecutorService executorService = new ThreadPoolExecutor(2, 5, 100, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2000), new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("client-transaction-msg-check-thread");
            return thread;
        }
    });

    producer.setExecutorService(executorService);
    producer.setTransactionListener(transactionListener);
    producer.start();

    String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
    for (int i = 0; i < 10; i++) {
        try {
            Message msg =
                new Message("TopicTest1234", tags[i % tags.length], "KEY" + i,
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.sendMessageInTransaction(msg, null);
            System.out.printf("%s%n", sendResult);

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

    for (int i = 0; i < 100000; i++) {
        Thread.sleep(1000);
    }
    producer.shutdown();
}
 
Example 8
Source File: TransactionProducer.java    From java-tutorial with MIT License 4 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {

        TransactionListener transactionListener = new TransactionListenerImpl();
        TransactionMQProducer producer = new TransactionMQProducer("transaction_Producer");
        producer.setNamesrvAddr(NAMESRVADDR);

        ExecutorService executorService = new ThreadPoolExecutor(2, 5, 100,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(2000), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("client-transaction-msg-check-thread");
                return thread;
            }
        });

        producer.setExecutorService(executorService);
        producer.setTransactionListener(transactionListener);
        producer.start();

        String[] tags = new String[]{"TagA", "TagB", "TagC", "TagD", "TagE"};
        for (int i = 0; i < 10; i++) {
            try {
                Message message = new Message("TopicTransactionTest", tags[i % tags.length],
                        "KEY" + i, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
                SendResult sendResult = producer.sendMessageInTransaction(message, null);
                System.out.printf("%s%n", sendResult);

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

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

        producer.shutdown();

    }