All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.javatool.canal.client.spring.boot.autoconfigure.ZookeeperClientAutoConfiguration Maven / Gradle / Ivy

package top.javatool.canal.client.spring.boot.autoconfigure;


import com.alibaba.otter.canal.protocol.CanalEntry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import top.javatool.canal.client.client.ZookeeperClusterCanalClient;
import top.javatool.canal.client.factory.EntryColumnModelFactory;
import top.javatool.canal.client.handler.EntryHandler;
import top.javatool.canal.client.handler.MessageHandler;
import top.javatool.canal.client.handler.RowDataHandler;
import top.javatool.canal.client.handler.impl.AsyncMessageHandlerImpl;
import top.javatool.canal.client.handler.impl.RowDataHandlerImpl;
import top.javatool.canal.client.handler.impl.SyncMessageHandlerImpl;
import top.javatool.canal.client.spring.boot.properties.CanalZookeeperProperties;
import top.javatool.canal.client.spring.boot.properties.CanalProperties;

import java.util.List;
import java.util.concurrent.ExecutorService;

@Configuration
@EnableConfigurationProperties(CanalZookeeperProperties.class)
@ConditionalOnBean(value = {EntryHandler.class})
@ConditionalOnProperty(value = CanalProperties.CANAL_MODE, havingValue = "zookeeper")
@Import(ThreadPoolAutoConfiguration.class)
public class ZookeeperClientAutoConfiguration {


    private CanalZookeeperProperties canalZookeeperProperties;


    public ZookeeperClientAutoConfiguration(CanalZookeeperProperties canalZookeeperProperties) {
        this.canalZookeeperProperties = canalZookeeperProperties;
    }

    @Bean
    public RowDataHandler rowDataHandler() {
        return new RowDataHandlerImpl(new EntryColumnModelFactory());
    }

    @Bean
    @ConditionalOnProperty(value = CanalProperties.CANAL_ASYNC, havingValue = "true")
    public MessageHandler messageHandler(RowDataHandler rowDataHandler, List entryHandlers,
                                         ExecutorService executorService) {
        return new AsyncMessageHandlerImpl(entryHandlers, rowDataHandler, executorService);
    }


    @Bean
    @ConditionalOnProperty(value = CanalProperties.CANAL_ASYNC, havingValue = "false")
    public MessageHandler messageHandler(RowDataHandler rowDataHandler, List entryHandlers) {
        return new SyncMessageHandlerImpl(entryHandlers, rowDataHandler);
    }


    @Bean(initMethod = "start", destroyMethod = "stop")
    public ZookeeperClusterCanalClient zookeeperClusterCanalClient(MessageHandler messageHandler) {
        return ZookeeperClusterCanalClient.builder()
                .zkServers(canalZookeeperProperties.getZkServers())
                .destination(canalZookeeperProperties.getDestination())
                .userName(canalZookeeperProperties.getUserName())
                .password(canalZookeeperProperties.getPassword())
                .batchSize(canalZookeeperProperties.getBatchSize())
                .filter(canalZookeeperProperties.getFilter())
                .timeout(canalZookeeperProperties.getTimeout())
                .unit(canalZookeeperProperties.getUnit())
                .messageHandler(messageHandler)
                .build();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy