Search in sources :

Example 1 with CanalClientException

use of com.alibaba.otter.canal.protocol.exception.CanalClientException in project java-example by 1479005017.

the class CanalMessageHandler method doRun.

public void doRun() throws CanalClientException {
    while (true) {
        Message message = connector.getWithoutAck(100);
        if (message.getId() == -1 || message.getEntries().size() == 0) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            continue;
        }
        TransactionStatus transactionStatus = null;
        if (transactionManager != null) {
            DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED);
            transactionStatus = transactionManager.getTransaction(transactionDefinition);
        }
        try {
            handleMessage(message);
            if (transactionStatus != null)
                transactionManager.commit(transactionStatus);
            connector.ack(message.getId());
        } catch (Exception e) {
            e.printStackTrace();
            if (transactionStatus != null)
                transactionManager.rollback(transactionStatus);
            connector.rollback(message.getId());
        }
    }
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Message(com.alibaba.otter.canal.protocol.Message) TransactionStatus(org.springframework.transaction.TransactionStatus) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException)

Example 2 with CanalClientException

use of com.alibaba.otter.canal.protocol.exception.CanalClientException in project canal by alibaba.

the class SimpleAdminConnector method connect.

@Override
public void connect() throws ServiceException {
    try {
        if (connected) {
            return;
        }
        channel = SocketChannel.open();
        channel.socket().setSoTimeout(soTimeout);
        channel.connect(address);
        readableChannel = Channels.newChannel(channel.socket().getInputStream());
        writableChannel = Channels.newChannel(channel.socket().getOutputStream());
        Packet p = Packet.parseFrom(readNextPacket());
        if (p.getVersion() != 1) {
            throw new CanalClientException("unsupported version at this client.");
        }
        if (p.getType() != PacketType.HANDSHAKE) {
            throw new CanalClientException("expect handshake but found other type.");
        }
        Handshake handshake = Handshake.parseFrom(p.getBody());
        // seed for auth
        ByteString seed = handshake.getSeeds();
        String newPasswd = passwd;
        if (passwd != null) {
            // encode passwd
            newPasswd = SecurityUtil.byte2HexStr(SecurityUtil.scramble411(passwd.getBytes(), seed.toByteArray()));
        }
        ClientAuth ca = ClientAuth.newBuilder().setUsername(user != null ? user : "").setPassword(ByteString.copyFromUtf8(newPasswd != null ? newPasswd : "")).setNetReadTimeout(idleTimeout).setNetWriteTimeout(idleTimeout).build();
        writeWithHeader(Packet.newBuilder().setType(PacketType.CLIENTAUTHENTICATION).setBody(ca.toByteString()).build().toByteArray());
        // 
        Packet ack = Packet.parseFrom(readNextPacket());
        if (ack.getType() != PacketType.ACK) {
            throw new CanalClientException("unexpected packet type when ack is expected");
        }
        Ack ackBody = Ack.parseFrom(ack.getBody());
        if (ackBody.getCode() > 0) {
            throw new ServiceException("something goes wrong when doing authentication: " + ackBody.getMessage());
        }
        connected = true;
    } catch (IOException | NoSuchAlgorithmException e) {
        throw new ServiceException(e);
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.AdminPacket.Packet) ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) ByteString(com.google.protobuf.ByteString) Ack(com.alibaba.otter.canal.protocol.AdminPacket.Ack) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientAuth(com.alibaba.otter.canal.protocol.AdminPacket.ClientAuth) Handshake(com.alibaba.otter.canal.protocol.AdminPacket.Handshake)

Example 3 with CanalClientException

use of com.alibaba.otter.canal.protocol.exception.CanalClientException in project canal by alibaba.

the class CanalRabbitMQConsumer method connect.

@Override
public void connect() {
    ConnectionFactory factory = new ConnectionFactory();
    if (accessKey.length() > 0 && secretKey.length() > 0) {
        factory.setCredentialsProvider(new AliyunCredentialsProvider(accessKey, secretKey, resourceOwnerId));
    } else {
        factory.setUsername(username);
        factory.setPassword(password);
    }
    // 解析出端口 modified by 16075140
    if (nameServer != null && nameServer.contains(":")) {
        String[] serverHostAndPort = nameServer.split(":");
        factory.setHost(serverHostAndPort[0]);
        factory.setPort(Integer.parseInt(serverHostAndPort[1]));
    } else {
        factory.setHost(nameServer);
    }
    factory.setAutomaticRecoveryEnabled(true);
    factory.setNetworkRecoveryInterval(5000);
    factory.setVirtualHost(vhost);
    try {
        connect = factory.newConnection();
        channel = connect.createChannel();
    } catch (IOException | TimeoutException e) {
        throw new CanalClientException("Start RabbitMQ producer error", e);
    }
    // 不存在连接 则重新连接
    if (connect == null) {
        this.connect();
    }
    Consumer consumer = new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            if (body != null) {
                channel.basicAck(envelope.getDeliveryTag(), process(body));
            }
        }
    };
    try {
        channel.basicConsume(queueName, false, consumer);
    } catch (IOException e) {
        throw new CanalClientException("error", e);
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) CanalMsgConsumer(com.alibaba.otter.canal.connector.core.spi.CanalMsgConsumer) Consumer(com.rabbitmq.client.Consumer) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) AliyunCredentialsProvider(com.alibaba.otter.canal.connector.rabbitmq.producer.AliyunCredentialsProvider) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with CanalClientException

use of com.alibaba.otter.canal.protocol.exception.CanalClientException in project canal by alibaba.

the class PulsarMQCanalConnector method getListWithoutAck.

/**
 * 获取泛型数据,供其他方法调用
 * <p>
 * 不支持多线程调用
 * </p>
 *
 * @return java.util.List<T>
 * @date 2021/9/14 15:20
 * @author chad
 * @since 1 by chad at 2021/9/14 供{@link PulsarMQCanalConnector#getListWithoutAck(Long, TimeUnit)}
 * 和{@link PulsarMQCanalConnector#getFlatListWithoutAck(Long, TimeUnit)}调用
 */
private <T> List<T> getListWithoutAck() {
    if (null != this.lastGetBatchMessage) {
        throw new CanalClientException("mq get/ack not support concurrent & async ack");
    }
    List messageList = Lists.newArrayList();
    try {
        this.lastGetBatchMessage = consumer.batchReceive();
        if (null == this.lastGetBatchMessage || this.lastGetBatchMessage.size() < 1) {
            this.lastGetBatchMessage = null;
            return messageList;
        }
    } catch (PulsarClientException e) {
        logger.error("Receiver Pulsar MQ message error", e);
        throw new CanalClientException(e);
    }
    for (org.apache.pulsar.client.api.Message<byte[]> msgExt : this.lastGetBatchMessage) {
        byte[] data = msgExt.getData();
        if (data == null) {
            logger.warn("Received message data is null");
            continue;
        }
        try {
            if (isFlatMessage) {
                FlatMessage flatMessage = JSON.parseObject(data, FlatMessage.class);
                messageList.add(flatMessage);
            } else {
                Message message = CanalMessageDeserializer.deserializer(data);
                messageList.add(message);
            }
        } catch (Exception ex) {
            logger.error("Add message error", ex);
            throw new CanalClientException(ex);
        }
    }
    return messageList;
}
Also used : FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) Message(com.alibaba.otter.canal.protocol.Message) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) List(java.util.List) org.apache.pulsar.client.api(org.apache.pulsar.client.api) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException)

Example 5 with CanalClientException

use of com.alibaba.otter.canal.protocol.exception.CanalClientException in project canal by alibaba.

the class RabbitMQCanalConnector method connect.

public void connect() throws CanalClientException {
    ConnectionFactory factory = new ConnectionFactory();
    if (accessKey.length() > 0 && secretKey.length() > 0) {
        factory.setCredentialsProvider(new AliyunCredentialsProvider(accessKey, secretKey, resourceOwnerId));
    } else {
        factory.setUsername(username);
        factory.setPassword(password);
    }
    factory.setHost(nameServer);
    factory.setAutomaticRecoveryEnabled(true);
    factory.setNetworkRecoveryInterval(5000);
    factory.setVirtualHost(vhost);
    try {
        connect = factory.newConnection();
        channel = connect.createChannel();
    } catch (IOException | TimeoutException e) {
        throw new CanalClientException("Start RabbitMQ producer error", e);
    }
}
Also used : CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

CanalClientException (com.alibaba.otter.canal.protocol.exception.CanalClientException)21 IOException (java.io.IOException)9 Message (com.alibaba.otter.canal.protocol.Message)6 ClientAck (com.alibaba.otter.canal.protocol.CanalPacket.ClientAck)4 Packet (com.alibaba.otter.canal.protocol.CanalPacket.Packet)4 Ack (com.alibaba.otter.canal.protocol.CanalPacket.Ack)3 ByteString (com.google.protobuf.ByteString)3 CommonMessage (com.alibaba.otter.canal.connector.core.consumer.CommonMessage)2 FlatMessage (com.alibaba.otter.canal.protocol.FlatMessage)2 SocketAddress (java.net.SocketAddress)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 TimeoutException (java.util.concurrent.TimeoutException)2 org.apache.pulsar.client.api (org.apache.pulsar.client.api)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 MessageExt (org.apache.rocketmq.common.message.MessageExt)2 ServiceException (com.alibaba.otter.canal.admin.common.exception.ServiceException)1 ConsumerBatchMessage (com.alibaba.otter.canal.client.ConsumerBatchMessage)1 ServerNotFoundException (com.alibaba.otter.canal.client.impl.ServerNotFoundException)1