org.apache.rocketmq.client.producer.TransactionMQProducer Java Examples

The following examples show how to use org.apache.rocketmq.client.producer.TransactionMQProducer. 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: RocketMQTransactionConfiguration.java    From rocketmq-spring with Apache License 2.0 6 votes vote down vote up
private void registerTransactionListener(String beanName, Object bean) {
    Class<?> clazz = AopProxyUtils.ultimateTargetClass(bean);

    if (!RocketMQLocalTransactionListener.class.isAssignableFrom(bean.getClass())) {
        throw new IllegalStateException(clazz + " is not instance of " + RocketMQLocalTransactionListener.class.getName());
    }
    RocketMQTransactionListener annotation = clazz.getAnnotation(RocketMQTransactionListener.class);
    RocketMQTemplate rocketMQTemplate = (RocketMQTemplate) applicationContext.getBean(annotation.rocketMQTemplateBeanName());
    if (((TransactionMQProducer) rocketMQTemplate.getProducer()).getTransactionListener() != null) {
        throw new IllegalStateException(annotation.rocketMQTemplateBeanName() + " already exists RocketMQLocalTransactionListener");
    }
    ((TransactionMQProducer) rocketMQTemplate.getProducer()).setExecutorService(new ThreadPoolExecutor(annotation.corePoolSize(), annotation.maximumPoolSize(),
        annotation.keepAliveTime(), TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(annotation.blockingQueueSize())));
    ((TransactionMQProducer) rocketMQTemplate.getProducer()).setTransactionListener(RocketMQUtil.convert((RocketMQLocalTransactionListener) bean));
    log.debug("RocketMQLocalTransactionListener {} register to {} success", clazz.getName(), annotation.rocketMQTemplateBeanName());
}
 
Example #3
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 #4
Source File: BootstrapConfiguration.java    From j360-boot-app-all with Apache License 2.0 6 votes vote down vote up
@Bean
public TransactionMQProducer transactionMQProducer() throws MQClientException {
    TransactionMQProducer producer = new TransactionMQProducer();
    RocketMQProperties.Producer producerConfig = rocketMQProperties.getProducer();
    String groupName = producerConfig.getGroup();
    Assert.hasText(groupName, "[spring.rocketmq.producer.group] must not be null");

    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());
    producer.setTransactionListener(new TxListener());
    producer.setProducerGroup(rocketMQProperties.getProducer().getGroup());
    producer.start();
    return producer;
}
 
Example #5
Source File: MQProducerAutoConfiguration.java    From rocketmq-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void configTransactionProducer() {
    Map<String, Object> beans = applicationContext.getBeansWithAnnotation(MQTransactionProducer.class);
    if(CollectionUtils.isEmpty(beans)){
        return;
    }
    ExecutorService executorService = new ThreadPoolExecutor(beans.size(), beans.size()*2, 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;
        }
    });
    Environment environment = applicationContext.getEnvironment();
    beans.entrySet().forEach( transactionProducer -> {
        try {
            AbstractMQTransactionProducer beanObj = AbstractMQTransactionProducer.class.cast(transactionProducer.getValue());
            MQTransactionProducer anno = beanObj.getClass().getAnnotation(MQTransactionProducer.class);

            TransactionMQProducer producer = new TransactionMQProducer(environment.resolvePlaceholders(anno.producerGroup()));
            producer.setNamesrvAddr(mqProperties.getNameServerAddress());
            producer.setExecutorService(executorService);
            producer.setTransactionListener(beanObj);
            producer.start();
            beanObj.setProducer(producer);
        } catch (Exception e) {
            log.error("build transaction producer error : {}", e);
        }
    });
}
 
Example #6
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 {
    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(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.sendMessageInTransaction(msg, tranExecuter, 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 DDMQ 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(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.sendMessageInTransaction(msg, tranExecuter, 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: DefaultMQProducerImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void initTransactionEnv() {
    TransactionMQProducer producer = (TransactionMQProducer) this.defaultMQProducer;
    this.checkRequestQueue = new LinkedBlockingQueue<Runnable>(producer.getCheckRequestHoldMax());
    this.checkExecutor = new ThreadPoolExecutor(
        producer.getCheckThreadPoolMinSize(),
        producer.getCheckThreadPoolMaxSize(),
        1000 * 60,
        TimeUnit.MILLISECONDS,
        this.checkRequestQueue);
}
 
Example #9
Source File: DefaultMQProducerImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public TransactionCheckListener checkListener() {
    if (this.defaultMQProducer instanceof TransactionMQProducer) {
        TransactionMQProducer producer = (TransactionMQProducer) defaultMQProducer;
        return producer.getTransactionCheckListener();
    }

    return null;
}
 
Example #10
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 #11
Source File: TransactionProducer.java    From rocketmq-all-4.1.0-incubating 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(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.sendMessageInTransaction(msg, tranExecuter, 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 #12
Source File: DefaultMQProducerImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public void initTransactionEnv() {
    TransactionMQProducer producer = (TransactionMQProducer) this.defaultMQProducer;
    this.checkRequestQueue = new LinkedBlockingQueue<Runnable>(producer.getCheckRequestHoldMax());
    this.checkExecutor = new ThreadPoolExecutor(//
        producer.getCheckThreadPoolMinSize(), //
        producer.getCheckThreadPoolMaxSize(), //
        1000 * 60, //
        TimeUnit.MILLISECONDS, //
        this.checkRequestQueue);
}
 
Example #13
Source File: DefaultMQProducerImpl.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Override
public TransactionCheckListener checkListener() {
    if (this.defaultMQProducer instanceof TransactionMQProducer) {
        TransactionMQProducer producer = (TransactionMQProducer) defaultMQProducer;
        return producer.getTransactionCheckListener();
    }

    return null;
}
 
Example #14
Source File: DefaultMQProducerImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void initTransactionEnv() {
    TransactionMQProducer producer = (TransactionMQProducer) this.defaultMQProducer;
    this.checkRequestQueue = new LinkedBlockingQueue<Runnable>(producer.getCheckRequestHoldMax());
    this.checkExecutor = new ThreadPoolExecutor(
        producer.getCheckThreadPoolMinSize(),
        producer.getCheckThreadPoolMaxSize(),
        1000 * 60,
        TimeUnit.MILLISECONDS,
        this.checkRequestQueue);
}
 
Example #15
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 #16
Source File: DefaultMQProducerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void initTransactionEnv() {
    TransactionMQProducer producer = (TransactionMQProducer) this.defaultMQProducer;
    if (producer.getExecutorService() != null) {
        this.checkExecutor = producer.getExecutorService();
    } else {
        this.checkRequestQueue = new LinkedBlockingQueue<Runnable>(producer.getCheckRequestHoldMax());
        this.checkExecutor = new ThreadPoolExecutor(
            producer.getCheckThreadPoolMinSize(),
            producer.getCheckThreadPoolMaxSize(),
            1000 * 60,
            TimeUnit.MILLISECONDS,
            this.checkRequestQueue);
    }
}
 
Example #17
Source File: DefaultMQProducerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * This method will be removed in the version 5.0.0 and <code>getCheckListener</code> is recommended.
 *
 * @return
 */
@Override
@Deprecated
public TransactionCheckListener checkListener() {
    if (this.defaultMQProducer instanceof TransactionMQProducer) {
        TransactionMQProducer producer = (TransactionMQProducer) defaultMQProducer;
        return producer.getTransactionCheckListener();
    }

    return null;
}
 
Example #18
Source File: DefaultMQProducerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public TransactionListener getCheckListener() {
    if (this.defaultMQProducer instanceof TransactionMQProducer) {
        TransactionMQProducer producer = (TransactionMQProducer) defaultMQProducer;
        return producer.getTransactionListener();
    }
    return null;
}
 
Example #19
Source File: RMQTransactionalProducer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
protected void create(boolean useTLS, TransactionListener transactionListener) {
    producer = new TransactionMQProducer();
    producer.setProducerGroup(getProducerGroupName());
    producer.setInstanceName(getProducerInstanceName());
    producer.setTransactionListener(transactionListener);
    producer.setUseTLS(useTLS);

    if (nsAddr != null) {
        producer.setNamesrvAddr(nsAddr);
    }
}
 
Example #20
Source File: RocketmqFactory.java    From framework with Apache License 2.0 5 votes vote down vote up
/**
 * Description: 初始化向rocketmq发送事务消息的生产者<br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @return TransactionMQProducer
 * @throws MQClientException <br>
 */
public static TransactionMQProducer getTransactionProducer() throws MQClientException {

    /*
     * 一个应用创建一个Producer,由应用来维护此对象,可以设置为全局对象或者单例<br> 注意:ProducerGroupName需要由应用来保证唯一<br>
     * ProducerGroup这个概念发送普通的消息时,作用不大,但是发送分布式事务消息时,比较关键, 因为服务器会回查这个Group下的任意一个Producer
     */
    // Producer Group Name
    TransactionMQProducer producer = new TransactionMQProducer(
        PropertyHolder.getProperty("message.rocketmq.producer.transactionProducerGroupName"));

    // Name service address
    producer.setNamesrvAddr(PropertyHolder.getProperty("message.rocketmq.namesrvAddr"));

    // Defalut value ip@pid when not set , this key used for cluster
    // producer.setInstanceName(properties.getProducerTranInstanceName());

    // Retry times
    producer.setRetryTimesWhenSendAsyncFailed(RETRY_TIMES);

    // 事务回查最小并发数
    // producer.setCheckThreadPoolMinSize(2);
    // 事务回查最大并发数
    // producer.setCheckThreadPoolMaxSize(2);
    // 队列数
    // producer.setCheckRequestHoldMax(2000);

    // TODO 由于社区版本的服务器阉割调了消息回查的功能,所以这个地方没有意义
    // TransactionCheckListener transactionCheckListener = new
    // TransactionCheckListenerImpl();
    // producer.setTransactionCheckListener(transactionCheckListener);

    /*
     * Producer对象在使用之前必须要调用start初始化,初始化一次即可<br> 注意:切记不可以在每次发送消息时,都调用start方法
     */
    producer.start();

    LOG.debug("RocketMq TransactionMQProducer Started.");
    return producer;
}
 
Example #21
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 #22
Source File: TransactionProducer.java    From DDMQ 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(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.sendMessageInTransaction(msg, tranExecuter, 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 #23
Source File: DefaultMQProducerImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public TransactionCheckListener checkListener() {
    if (this.defaultMQProducer instanceof TransactionMQProducer) {
        TransactionMQProducer producer = (TransactionMQProducer) defaultMQProducer;
        return producer.getTransactionCheckListener();
    }

    return null;
}
 
Example #24
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 #25
Source File: DefaultMQProducerImpl.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public void initTransactionEnv() {
    TransactionMQProducer producer = (TransactionMQProducer) this.defaultMQProducer;
    if (producer.getExecutorService() != null) {
        this.checkExecutor = producer.getExecutorService();
    } else {
        this.checkRequestQueue = new LinkedBlockingQueue<Runnable>(2000);
        this.checkExecutor = new ThreadPoolExecutor(
            1,
            1,
            1000 * 60,
            TimeUnit.MILLISECONDS,
            this.checkRequestQueue);
    }
}
 
Example #26
Source File: DefaultMQProducerImpl.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
@Override
public TransactionListener checkListener() {
    if (this.defaultMQProducer instanceof TransactionMQProducer) {
        TransactionMQProducer producer = (TransactionMQProducer) defaultMQProducer;
        return producer.getTransactionListener();
    }

    return null;
}
 
Example #27
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 #28
Source File: DefaultMQProducerImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化事务环境
 */
public void initTransactionEnv() {
    TransactionMQProducer producer = (TransactionMQProducer) this.defaultMQProducer;
    if (producer.getExecutorService() != null) {
        this.checkExecutor = producer.getExecutorService();
    } else {
        this.checkRequestQueue = new LinkedBlockingQueue<Runnable>(producer.getCheckRequestHoldMax());
        this.checkExecutor = new ThreadPoolExecutor(
            producer.getCheckThreadPoolMinSize(),
            producer.getCheckThreadPoolMaxSize(),
            1000 * 60,
            TimeUnit.MILLISECONDS,
            this.checkRequestQueue);
    }
}
 
Example #29
Source File: DefaultMQProducerImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * This method will be removed in the version 5.0.0 and <code>getCheckListener</code> is recommended.
 * @return
 */
@Override
@Deprecated
public TransactionCheckListener checkListener() {
    if (this.defaultMQProducer instanceof TransactionMQProducer) {
        TransactionMQProducer producer = (TransactionMQProducer) defaultMQProducer;
        return producer.getTransactionCheckListener();
    }

    return null;
}
 
Example #30
Source File: DefaultMQProducerImpl.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 获取事务回查checklistener
 * @return ;
 */
@Override
public TransactionListener getCheckListener() {
    if (this.defaultMQProducer instanceof TransactionMQProducer) {
        TransactionMQProducer producer = (TransactionMQProducer) defaultMQProducer;
        return producer.getTransactionListener();
    }
    return null;
}