cn.flood.canal.configuration.autoconfigure.ClusterClientAutoConfiguration Maven / Gradle / Ivy
package cn.flood.canal.configuration.autoconfigure;
import cn.flood.canal.client.ClusterCanalClient;
import cn.flood.canal.configuration.properties.CanalProperties;
import cn.flood.canal.configuration.properties.CanalSimpleProperties;
import cn.flood.canal.factory.EntryColumnModelFactory;
import cn.flood.canal.handler.EntryHandler;
import cn.flood.canal.handler.MessageHandler;
import cn.flood.canal.handler.RowDataHandler;
import cn.flood.canal.handler.impl.AsyncMessageHandlerImpl;
import cn.flood.canal.handler.impl.RowDataHandlerImpl;
import cn.flood.canal.handler.impl.SyncMessageHandlerImpl;
import com.alibaba.otter.canal.protocol.CanalEntry;
import java.util.List;
import java.util.concurrent.ExecutorService;
import org.springframework.boot.autoconfigure.AutoConfiguration;
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.Import;
@AutoConfiguration
@EnableConfigurationProperties(CanalSimpleProperties.class)
@ConditionalOnBean(value = {EntryHandler.class})
@ConditionalOnProperty(value = CanalProperties.CANAL_MODE, havingValue = "cluster")
@Import(ThreadPoolAutoConfiguration.class)
public class ClusterClientAutoConfiguration {
private CanalSimpleProperties canalSimpleProperties;
public ClusterClientAutoConfiguration(CanalSimpleProperties canalSimpleProperties) {
this.canalSimpleProperties = canalSimpleProperties;
}
@Bean
public RowDataHandler rowDataHandler() {
return new RowDataHandlerImpl(new EntryColumnModelFactory());
}
@Bean
@ConditionalOnProperty(value = CanalProperties.CANAL_ASYNC, havingValue = "true", matchIfMissing = true)
public MessageHandler asyncMessageHandler(RowDataHandler rowDataHandler,
List entryHandlers,
ExecutorService executorService) {
return new AsyncMessageHandlerImpl(entryHandlers, rowDataHandler, executorService);
}
@Bean
@ConditionalOnProperty(value = CanalProperties.CANAL_ASYNC, havingValue = "false")
public MessageHandler syncMessageHandler(RowDataHandler rowDataHandler,
List entryHandlers) {
return new SyncMessageHandlerImpl(entryHandlers, rowDataHandler);
}
@Bean(initMethod = "start", destroyMethod = "stop")
public ClusterCanalClient clusterCanalClient(MessageHandler messageHandler) {
return ClusterCanalClient.Builder.builder().
canalServers(canalSimpleProperties.getServer())
.destination(canalSimpleProperties.getDestination())
.userName(canalSimpleProperties.getUserName())
.messageHandler(messageHandler)
.password(canalSimpleProperties.getPassword())
.batchSize(canalSimpleProperties.getBatchSize())
.filter(canalSimpleProperties.getFilter())
.timeout(canalSimpleProperties.getTimeout())
.unit(canalSimpleProperties.getUnit()).build();
}
}