Search in sources :

Example 11 with CanalClientException

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

the class CanalPulsarMQConsumer method connect.

@Override
public void connect() {
    if (isConsumerActive()) {
        return;
    }
    // 连接创建客户端
    try {
        pulsarClient = PulsarClient.builder().serviceUrl(serviceUrl).authentication(AuthenticationFactory.token(roleToken)).build();
    } catch (PulsarClientException e) {
        throw new RuntimeException(e);
    }
    ConsumerBuilder<byte[]> builder = pulsarClient.newConsumer();
    if (MQUtil.isPatternTopic(this.topic)) {
        // 正则只支持一个
        builder.topicsPattern(this.topic);
    } else {
        // 多个topic
        builder.topic(this.topic);
    }
    // 为保证消息的有序性,仅支持单消费实例模式
    // 灾备模式,一个分区只能有一个消费者,如果当前消费者不可用,自动切换到其他消费者
    builder.subscriptionType(SubscriptionType.Failover);
    builder.negativeAckRedeliveryDelay(this.redeliveryDelaySeconds, TimeUnit.SECONDS).subscriptionName(this.subscriptName);
    if (this.isRetry) {
        DeadLetterPolicy.DeadLetterPolicyBuilder dlqBuilder = DeadLetterPolicy.builder().maxRedeliverCount(this.maxRedeliveryCount);
        // 指定重试队列,不是多个或通配符topic才能判断重试队列
        if (!MQUtil.isPatternTag(this.topic)) {
            String retryTopic = this.topic + (this.isRetryDLQUpperCase ? "-RETRY" : "-retry");
            dlqBuilder.retryLetterTopic(retryTopic);
            String dlqTopic = this.topic + (this.isRetryDLQUpperCase ? "-DLQ" : "-dlq");
            dlqBuilder.deadLetterTopic(dlqTopic);
        }
        // 默认关闭,如果需要重试则开启
        builder.enableRetry(true).deadLetterPolicy(dlqBuilder.build());
    }
    // ack超时
    builder.ackTimeout(this.ackTimeoutSeconds, TimeUnit.SECONDS);
    // pulsar批量获取消息设置
    builder.batchReceivePolicy(new BatchReceivePolicy.Builder().maxNumMessages(this.batchSize).timeout(this.getBatchTimeoutSeconds, TimeUnit.SECONDS).build());
    try {
        this.pulsarMQConsumer = builder.subscribe();
    } catch (PulsarClientException e) {
        throw new CanalClientException("Subscript pulsar consumer error", e);
    }
}
Also used : CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException)

Example 12 with CanalClientException

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

the class CanalPulsarMQConsumer method getMessage.

@SuppressWarnings("unchecked")
@Override
public List<CommonMessage> getMessage(Long timeout, TimeUnit unit) {
    List<CommonMessage> messageList = Lists.newArrayList();
    try {
        Messages<byte[]> messages = pulsarMQConsumer.batchReceive();
        if (null == messages || messages.size() == 0) {
            return messageList;
        }
        // 保存当前消费记录,用于ack和rollback
        this.lastGetBatchMessage = messages;
        for (org.apache.pulsar.client.api.Message<byte[]> msg : messages) {
            byte[] data = msg.getData();
            if (!this.flatMessage) {
                Message message = CanalMessageSerializerUtil.deserializer(data);
                List<CommonMessage> list = MessageUtil.convert(message);
                messageList.addAll(list);
            } else {
                CommonMessage commonMessage = JSON.parseObject(data, CommonMessage.class);
                messageList.add(commonMessage);
            }
        }
    } catch (PulsarClientException e) {
        throw new CanalClientException("Receive pulsar batch message error", e);
    }
    return messageList;
}
Also used : Message(com.alibaba.otter.canal.protocol.Message) CommonMessage(com.alibaba.otter.canal.connector.core.consumer.CommonMessage) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) CommonMessage(com.alibaba.otter.canal.connector.core.consumer.CommonMessage) org.apache.pulsar.client.api(org.apache.pulsar.client.api)

Example 13 with CanalClientException

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

the class SimpleCanalConnector method subscribe.

@Override
public void subscribe(String filter) throws CanalClientException {
    waitClientRunning();
    if (!running) {
        return;
    }
    try {
        writeWithHeader(Packet.newBuilder().setType(PacketType.SUBSCRIPTION).setBody(Sub.newBuilder().setDestination(clientIdentity.getDestination()).setClientId(String.valueOf(clientIdentity.getClientId())).setFilter(filter != null ? filter : "").build().toByteString()).build().toByteArray());
        // 
        Packet p = Packet.parseFrom(readNextPacket());
        Ack ack = Ack.parseFrom(p.getBody());
        if (ack.getErrorCode() > 0) {
            throw new CanalClientException("failed to subscribe with reason: " + ack.getErrorMessage());
        }
        clientIdentity.setFilter(filter);
    } 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 14 with CanalClientException

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

the class SimpleCanalConnector method getWithoutAck.

@Override
public Message getWithoutAck(int batchSize, Long timeout, TimeUnit unit) throws CanalClientException {
    waitClientRunning();
    if (!running) {
        return null;
    }
    try {
        int size = (batchSize <= 0) ? 1000 : batchSize;
        // -1代表不做timeout控制
        long time = (timeout == null || timeout < 0) ? -1 : timeout;
        if (unit == null) {
            unit = TimeUnit.MILLISECONDS;
        }
        writeWithHeader(Packet.newBuilder().setType(PacketType.GET).setBody(Get.newBuilder().setAutoAck(false).setDestination(clientIdentity.getDestination()).setClientId(String.valueOf(clientIdentity.getClientId())).setFetchSize(size).setTimeout(time).setUnit(unit.ordinal()).build().toByteString()).build().toByteArray());
        return receiveMessages();
    } catch (IOException e) {
        throw new CanalClientException(e);
    }
}
Also used : CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) IOException(java.io.IOException)

Example 15 with CanalClientException

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

the class SimpleCanalConnector method ack.

@Override
public void ack(long batchId) throws CanalClientException {
    waitClientRunning();
    if (!running) {
        return;
    }
    ClientAck ca = ClientAck.newBuilder().setDestination(clientIdentity.getDestination()).setClientId(String.valueOf(clientIdentity.getClientId())).setBatchId(batchId).build();
    try {
        writeWithHeader(Packet.newBuilder().setType(PacketType.CLIENTACK).setBody(ca.toByteString()).build().toByteArray());
    } catch (IOException e) {
        throw new CanalClientException(e);
    }
}
Also used : CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) IOException(java.io.IOException) ClientAck(com.alibaba.otter.canal.protocol.CanalPacket.ClientAck)

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