Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决部分bug #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
package top.javatool.canal.client.util;

import com.alibaba.google.common.base.Enums;
import org.apache.commons.lang.time.DateUtils;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
* @author yangpeng
*/
public class StringConvertUtil {


private static String[] PARSE_PATTERNS = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss",
private static String[] PARSE_PATTERNS = new String[]{
"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.SSS",
"yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss",
"yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
"yyyy.MM.dd HH:mm", "yyyy.MM"};

"yyyy.MM.dd HH:mm", "yyyy.MM", "yyyy-MM-dd"};
public StringConvertUtil() {
}

static Object convertType(Class<?> type, String columnValue) {
if (columnValue == null) {
static Object convertType(Class type, String columnValue) {
if (columnValue == null || columnValue.length() == 0) {
return null;
} else if (type.equals(String.class)) {
return columnValue;
} else if (type.equals(Integer.class)) {
return Integer.parseInt(columnValue);
} else if (type.equals(Long.class)) {
Expand All @@ -37,8 +43,36 @@ static Object convertType(Class<?> type, String columnValue) {
return Float.parseFloat(columnValue);
} else if (type.equals(Date.class)) {
return parseDate(columnValue);
} else if (type.equals(LocalDate.class)) {
return parseLocalDate(columnValue);
} else if (type.equals(LocalDateTime.class)) {
return parseLocalDateTime(columnValue);
} else if (type.equals(java.sql.Date.class)) {
return parseSqlDate(columnValue);
} else if (type.isArray()) {
// blob/binary
return columnValue.getBytes(StandardCharsets.ISO_8859_1);
} else {
// 枚举类型
return Enums.stringConverter(type).convert(columnValue);
}
}

private static Object parseLocalDateTime(String str) {
if (str == null) {
return null;
} else {
Instant instant = parseDate(str).toInstant();
return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
}
}

private static Object parseLocalDate(String str) {
if (str == null) {
return null;
} else {
return type.equals(java.sql.Date.class) ? parseSqlDate(columnValue) : columnValue;
Instant instant = parseDate(str).toInstant();
return instant.atZone(ZoneId.systemDefault()).toLocalDate();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

import com.alibaba.otter.canal.protocol.CanalEntry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
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.DependsOn;
import org.springframework.context.annotation.Import;
import top.javatool.canal.client.client.ClusterCanalClient;
import top.javatool.canal.client.factory.EntryColumnModelFactory;
Expand Down Expand Up @@ -42,21 +44,28 @@ public RowDataHandler<CanalEntry.RowData> rowDataHandler() {
return new RowDataHandlerImpl(new EntryColumnModelFactory());
}

@Bean
@ConditionalOnProperty(value = CanalProperties.CANAL_ASYNC, havingValue = "true", matchIfMissing = true)
public MessageHandler messageHandler(RowDataHandler<CanalEntry.RowData> rowDataHandler, List<EntryHandler> entryHandlers,
ExecutorService executorService) {
return new AsyncMessageHandlerImpl(entryHandlers, rowDataHandler, executorService);
@Configuration
@ConditionalOnProperty(value = CanalProperties.CANAL_MODE, havingValue = "cluster")
@ConditionalOnExpression(value = "${canal.async:true}")
public static class CanalAsyncMessageHandler{
@Bean
public MessageHandler messageHandler(RowDataHandler<CanalEntry.RowData> rowDataHandler, List<EntryHandler> entryHandlers,
ExecutorService executorService) {
return new AsyncMessageHandlerImpl(entryHandlers, rowDataHandler, executorService);
}
}


@Bean
@ConditionalOnProperty(value = CanalProperties.CANAL_ASYNC, havingValue = "false")
public MessageHandler messageHandler(RowDataHandler<CanalEntry.RowData> rowDataHandler, List<EntryHandler> entryHandlers) {
return new SyncMessageHandlerImpl(entryHandlers, rowDataHandler);
@Configuration
@ConditionalOnProperty(value = CanalProperties.CANAL_MODE, havingValue = "cluster")
@ConditionalOnExpression(value = "!${canal.async:true}")
public static class CanalSyncMessageHandler{
@Bean
public MessageHandler messageHandler(RowDataHandler<CanalEntry.RowData> rowDataHandler, List<EntryHandler> entryHandlers) {
return new SyncMessageHandlerImpl(entryHandlers, rowDataHandler);
}
}


@DependsOn("messageHandler")
@Bean(initMethod = "start", destroyMethod = "stop")
public ClusterCanalClient clusterCanalClient(MessageHandler messageHandler) {
return ClusterCanalClient.Builder.builder().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@


import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
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.DependsOn;
import org.springframework.context.annotation.Import;
import top.javatool.canal.client.client.KafkaCanalClient;
import top.javatool.canal.client.factory.MapColumnModelFactory;
Expand Down Expand Up @@ -43,22 +45,30 @@ public RowDataHandler<List<Map<String, String>>> rowDataHandler() {
return new MapRowDataHandlerImpl(new MapColumnModelFactory());
}

@Bean
@ConditionalOnProperty(value = CanalProperties.CANAL_ASYNC, havingValue = "true", matchIfMissing = true)
public MessageHandler messageHandler(RowDataHandler<List<Map<String, String>>> rowDataHandler,
List<EntryHandler> entryHandlers,
ExecutorService executorService) {
return new AsyncFlatMessageHandlerImpl(entryHandlers, rowDataHandler, executorService);
@Configuration
@ConditionalOnProperty(value = CanalProperties.CANAL_MODE, havingValue = "kafka")
@ConditionalOnExpression(value = "${canal.async:true}")
public static class CanalAsyncMessageHandler{
@Bean
public MessageHandler messageHandler(RowDataHandler<List<Map<String, String>>> rowDataHandler,
List<EntryHandler> entryHandlers,
ExecutorService executorService) {
return new AsyncFlatMessageHandlerImpl(entryHandlers, rowDataHandler, executorService);
}
}

@Configuration
@ConditionalOnProperty(value = CanalProperties.CANAL_MODE, havingValue = "kafka")
@ConditionalOnExpression(value = "!${canal.async:true}")
public static class CanalSyncMessageHandler{

@Bean
@ConditionalOnProperty(value = CanalProperties.CANAL_ASYNC, havingValue = "false")
public MessageHandler messageHandler(RowDataHandler<List<Map<String, String>>> rowDataHandler, List<EntryHandler> entryHandlers) {
return new SyncFlatMessageHandlerImpl(entryHandlers, rowDataHandler);
@Bean
public MessageHandler messageHandler(RowDataHandler<List<Map<String, String>>> rowDataHandler, List<EntryHandler> entryHandlers) {
return new SyncFlatMessageHandlerImpl(entryHandlers, rowDataHandler);
}
}


@DependsOn("messageHandler")
@Bean(initMethod = "start", destroyMethod = "stop")
public KafkaCanalClient kafkaCanalClient(MessageHandler messageHandler) {
return KafkaCanalClient.builder().servers(canalKafkaProperties.getServer())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

import com.alibaba.otter.canal.protocol.CanalEntry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
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.DependsOn;
import org.springframework.context.annotation.Import;
import top.javatool.canal.client.client.SimpleCanalClient;
import top.javatool.canal.client.factory.EntryColumnModelFactory;
Expand Down Expand Up @@ -42,22 +44,30 @@ public SimpleClientAutoConfiguration(CanalSimpleProperties canalSimpleProperties
public RowDataHandler<CanalEntry.RowData> rowDataHandler() {
return new RowDataHandlerImpl(new EntryColumnModelFactory());
}
@Configuration
@ConditionalOnProperty(value = CanalProperties.CANAL_MODE, havingValue = "simple", matchIfMissing = true)
@ConditionalOnExpression(value = "${canal.async:true}")
public static class CanalAsyncMessageHandler{
@Bean
public MessageHandler messageHandler(RowDataHandler<CanalEntry.RowData> rowDataHandler, List<EntryHandler> entryHandlers,
ExecutorService executorService) {
return new AsyncMessageHandlerImpl(entryHandlers, rowDataHandler, executorService);
}

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


@Bean
@ConditionalOnProperty(value = CanalProperties.CANAL_ASYNC, havingValue = "false")
public MessageHandler messageHandler(RowDataHandler<CanalEntry.RowData> rowDataHandler, List<EntryHandler> entryHandlers) {
return new SyncMessageHandlerImpl(entryHandlers, rowDataHandler);
@Configuration
@ConditionalOnProperty(value = CanalProperties.CANAL_MODE, havingValue = "simple", matchIfMissing = true)
@ConditionalOnExpression(value = "!${canal.async:true}")
public static class CanalSyncMessageHandler{

@Bean
public MessageHandler messageHandler(RowDataHandler<CanalEntry.RowData> rowDataHandler, List<EntryHandler> entryHandlers) {
return new SyncMessageHandlerImpl(entryHandlers, rowDataHandler);
}
}


@DependsOn("messageHandler")
@Bean(initMethod = "start", destroyMethod = "stop")
public SimpleCanalClient simpleCanalClient(MessageHandler messageHandler) {
String server = canalSimpleProperties.getServer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

import com.alibaba.otter.canal.protocol.CanalEntry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
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.DependsOn;
import org.springframework.context.annotation.Import;
import top.javatool.canal.client.client.ZookeeperClusterCanalClient;
import top.javatool.canal.client.factory.EntryColumnModelFactory;
Expand Down Expand Up @@ -42,21 +44,29 @@ public RowDataHandler<CanalEntry.RowData> rowDataHandler() {
return new RowDataHandlerImpl(new EntryColumnModelFactory());
}

@Bean
@ConditionalOnProperty(value = CanalProperties.CANAL_ASYNC, havingValue = "true",matchIfMissing = true)
public MessageHandler messageHandler(RowDataHandler<CanalEntry.RowData> rowDataHandler, List<EntryHandler> entryHandlers,
ExecutorService executorService) {
return new AsyncMessageHandlerImpl(entryHandlers, rowDataHandler, executorService);
@Configuration
@ConditionalOnProperty(value = CanalProperties.CANAL_MODE, havingValue = "zk")
@ConditionalOnExpression(value = "${canal.async:true}")
public static class CanalAsyncMessageHandler{
@Bean
public MessageHandler messageHandler(RowDataHandler<CanalEntry.RowData> rowDataHandler, List<EntryHandler> entryHandlers,
ExecutorService executorService) {
return new AsyncMessageHandlerImpl(entryHandlers, rowDataHandler, executorService);
}
}

@Configuration
@ConditionalOnProperty(value = CanalProperties.CANAL_MODE, havingValue = "zk")
@ConditionalOnExpression(value = "!${canal.async:true}")
public static class CanalSyncMessageHandler{

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


@DependsOn("messageHandler")
@Bean(initMethod = "start", destroyMethod = "stop")
public ZookeeperClusterCanalClient zookeeperClusterCanalClient(MessageHandler messageHandler) {
return ZookeeperClusterCanalClient.builder()
Expand Down