Search in sources :

Example 6 with CanalClientException

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

the class SimpleCanalConnector method unsubscribe.

@Override
public void unsubscribe() throws CanalClientException {
    waitClientRunning();
    if (!running) {
        return;
    }
    try {
        writeWithHeader(Packet.newBuilder().setType(PacketType.UNSUBSCRIPTION).setBody(Unsub.newBuilder().setDestination(clientIdentity.getDestination()).setClientId(String.valueOf(clientIdentity.getClientId())).build().toByteString()).build().toByteArray());
        // 
        Packet p = Packet.parseFrom(readNextPacket());
        Ack ack = Ack.parseFrom(p.getBody());
        if (ack.getErrorCode() > 0) {
            throw new CanalClientException("failed to unSubscribe with reason: " + ack.getErrorMessage());
        }
    } catch (IOException e) {
        throw new CanalClientException(e);
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.CanalPacket.Packet) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) Ack(com.alibaba.otter.canal.protocol.CanalPacket.Ack) ClientAck(com.alibaba.otter.canal.protocol.CanalPacket.ClientAck) IOException(java.io.IOException)

Example 7 with CanalClientException

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

the class RocketMQCanalConnector method process.

private boolean process(List<MessageExt> messageExts) {
    if (logger.isDebugEnabled()) {
        logger.debug("Get Message: {}", messageExts);
    }
    List messageList = new ArrayList<>();
    for (MessageExt messageExt : messageExts) {
        byte[] data = messageExt.getBody();
        if (data != null) {
            try {
                if (!flatMessage) {
                    Message message = CanalMessageDeserializer.deserializer(data);
                    messageList.add(message);
                } else {
                    FlatMessage flatMessage = JSON.parseObject(data, FlatMessage.class);
                    messageList.add(flatMessage);
                }
            } catch (Exception ex) {
                logger.error("Add message error", ex);
                throw new CanalClientException(ex);
            }
        } else {
            logger.warn("Received message data is null");
        }
    }
    ConsumerBatchMessage batchMessage;
    if (!flatMessage) {
        batchMessage = new ConsumerBatchMessage<Message>(messageList);
    } else {
        batchMessage = new ConsumerBatchMessage<FlatMessage>(messageList);
    }
    try {
        messageBlockingQueue.put(batchMessage);
    } catch (InterruptedException e) {
        logger.error("Put message to queue error", e);
        throw new RuntimeException(e);
    }
    boolean isCompleted;
    try {
        isCompleted = batchMessage.waitFinish(batchProcessTimeout);
    } catch (InterruptedException e) {
        logger.error("Interrupted when waiting messages to be finished.", e);
        throw new RuntimeException(e);
    }
    boolean isSuccess = batchMessage.isSuccess();
    return isCompleted && isSuccess;
}
Also used : ConsumerBatchMessage(com.alibaba.otter.canal.client.ConsumerBatchMessage) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) Message(com.alibaba.otter.canal.protocol.Message) ArrayList(java.util.ArrayList) MQClientException(org.apache.rocketmq.client.exception.MQClientException) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) MessageExt(org.apache.rocketmq.common.message.MessageExt) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) ArrayList(java.util.ArrayList) List(java.util.List) ConsumerBatchMessage(com.alibaba.otter.canal.client.ConsumerBatchMessage)

Example 8 with CanalClientException

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

the class ClusterCanalConnector method restart.

private void restart() throws CanalClientException {
    disconnect();
    try {
        Thread.sleep(retryInterval);
    } catch (InterruptedException e) {
        throw new CanalClientException(e);
    }
    connect();
}
Also used : CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException)

Example 9 with CanalClientException

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

the class CanalRocketMQConsumer method process.

private boolean process(List<MessageExt> messageExts) {
    if (logger.isDebugEnabled()) {
        logger.debug("Get Message: {}", messageExts);
    }
    List<CommonMessage> messageList = new ArrayList<>();
    for (MessageExt messageExt : messageExts) {
        byte[] data = messageExt.getBody();
        if (data != null) {
            try {
                if (!flatMessage) {
                    Message message = CanalMessageSerializerUtil.deserializer(data);
                    messageList.addAll(MessageUtil.convert(message));
                } else {
                    CommonMessage commonMessage = JSON.parseObject(data, CommonMessage.class);
                    messageList.add(commonMessage);
                }
            } catch (Exception ex) {
                logger.error("Add message error", ex);
                throw new CanalClientException(ex);
            }
        } else {
            logger.warn("Received message data is null");
        }
    }
    ConsumerBatchMessage<CommonMessage> batchMessage = new ConsumerBatchMessage<>(messageList);
    try {
        messageBlockingQueue.put(batchMessage);
    } catch (InterruptedException e) {
        logger.error("Put message to queue error", e);
        throw new RuntimeException(e);
    }
    boolean isCompleted;
    try {
        isCompleted = batchMessage.waitFinish(batchProcessTimeout);
    } catch (InterruptedException e) {
        logger.error("Interrupted when waiting messages to be finished.", e);
        throw new RuntimeException(e);
    }
    boolean isSuccess = batchMessage.isSuccess();
    return isCompleted && isSuccess;
}
Also used : Message(com.alibaba.otter.canal.protocol.Message) CommonMessage(com.alibaba.otter.canal.connector.core.consumer.CommonMessage) ArrayList(java.util.ArrayList) MQClientException(org.apache.rocketmq.client.exception.MQClientException) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) MessageExt(org.apache.rocketmq.common.message.MessageExt) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) CommonMessage(com.alibaba.otter.canal.connector.core.consumer.CommonMessage)

Example 10 with CanalClientException

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

the class CanalPulsarMQConsumer method disconnect.

@Override
public void disconnect() {
    if (null == this.pulsarMQConsumer || !this.pulsarMQConsumer.isConnected()) {
        return;
    }
    try {
        this.pulsarMQConsumer.unsubscribe();
        this.pulsarClient.close();
    } catch (PulsarClientException e) {
        throw new CanalClientException("Disconnect pulsar consumer error", e);
    }
}
Also used : CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException)

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