Search in sources :

Example 1 with Command

use of com.moilioncircle.redis.replicator.cmd.Command in project rocketmq-externals by apache.

the class RocketMQRedisReplicatorTest method open.

@Test
public void open() throws Exception {
    Configure configure = new Configure(properties);
    Replicator replicator = new RocketMQRedisReplicator(configure);
    final RocketMQRedisProducer producer = new RocketMQRedisProducer(configure);
    producer.open();
    final AtomicInteger test = new AtomicInteger();
    replicator.addEventListener(new EventListener() {

        @Override
        public void onEvent(Replicator replicator, Event event) {
            if (event instanceof KeyValuePair<?, ?>) {
                try {
                    boolean success = producer.send(event);
                    if (success) {
                        test.incrementAndGet();
                    }
                } catch (Exception e) {
                    LOGGER.error("Fail to send KeyValuePair", e);
                }
            } else if (event instanceof Command) {
                try {
                    boolean success = producer.send(event);
                    if (success) {
                        test.incrementAndGet();
                    }
                } catch (Exception e) {
                    LOGGER.error("Fail to send command", e);
                }
            }
        }
    });
    replicator.addCloseListener(new CloseListener() {

        @Override
        public void handle(Replicator replicator) {
            producer.close();
        }
    });
    replicator.open();
    assertEquals(19, test.get());
}
Also used : Replicator(com.moilioncircle.redis.replicator.Replicator) RocketMQRedisProducer(org.apache.rocketmq.redis.replicator.mq.RocketMQRedisProducer) URISyntaxException(java.net.URISyntaxException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Command(com.moilioncircle.redis.replicator.cmd.Command) CloseListener(com.moilioncircle.redis.replicator.CloseListener) Configure(org.apache.rocketmq.redis.replicator.conf.Configure) Event(com.moilioncircle.redis.replicator.event.Event) EventListener(com.moilioncircle.redis.replicator.event.EventListener) Test(org.junit.Test)

Aggregations

CloseListener (com.moilioncircle.redis.replicator.CloseListener)1 Replicator (com.moilioncircle.redis.replicator.Replicator)1 Command (com.moilioncircle.redis.replicator.cmd.Command)1 Event (com.moilioncircle.redis.replicator.event.Event)1 EventListener (com.moilioncircle.redis.replicator.event.EventListener)1 URISyntaxException (java.net.URISyntaxException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Configure (org.apache.rocketmq.redis.replicator.conf.Configure)1 RocketMQRedisProducer (org.apache.rocketmq.redis.replicator.mq.RocketMQRedisProducer)1 Test (org.junit.Test)1