Exemplo n.º 1
0
 public void unsubscribe() throws CanalClientException {
   waitClientRunning();
   try {
     writeWithHeader(
         channel,
         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(channel));
     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);
   }
 }
Exemplo n.º 2
0
  private InetSocketAddress doConnect() throws CanalClientException {
    try {
      channel = SocketChannel.open();
      channel.socket().setSoTimeout(soTimeout);
      channel.connect(address);
      Packet p = Packet.parseFrom(readNextPacket(channel));
      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());
      supportedCompressions.addAll(handshake.getSupportedCompressionsList());
      //
      ClientAuth ca =
          ClientAuth.newBuilder()
              .setUsername(username)
              .setNetReadTimeout(10000)
              .setNetWriteTimeout(10000)
              .build();
      writeWithHeader(
          channel,
          Packet.newBuilder()
              .setType(PacketType.CLIENTAUTHENTICATION)
              .setBody(ca.toByteString())
              .build()
              .toByteArray());
      //
      Packet ack = Packet.parseFrom(readNextPacket(channel));
      if (ack.getType() != PacketType.ACK) {
        throw new CanalClientException("unexpected packet type when ack is expected");
      }

      Ack ackBody = Ack.parseFrom(ack.getBody());
      if (ackBody.getErrorCode() > 0) {
        throw new CanalClientException(
            "something goes wrong when doing authentication: " + ackBody.getErrorMessage());
      }

      return new InetSocketAddress(
          channel.socket().getLocalAddress(), channel.socket().getLocalPort());
    } catch (IOException e) {
      throw new CanalClientException(e);
    }
  }
Exemplo n.º 3
0
  public Message getWithoutAck(int batchSize) throws CanalClientException {
    waitClientRunning();
    try {
      int size = (batchSize <= 0) ? 1000 : batchSize;
      writeWithHeader(
          channel,
          Packet.newBuilder()
              .setType(PacketType.GET)
              .setBody(
                  Get.newBuilder()
                      .setDestination(clientIdentity.getDestination())
                      .setClientId(String.valueOf(clientIdentity.getClientId()))
                      .setFetchSize(size)
                      .build()
                      .toByteString())
              .build()
              .toByteArray());
      //
      Packet p = Packet.parseFrom(readNextPacket(channel));
      switch (p.getType()) {
        case MESSAGES:
          {
            if (!p.getCompression().equals(Compression.NONE)) {
              throw new CanalClientException("compression is not supported in this connector");
            }

            Messages messages = Messages.parseFrom(p.getBody());
            Message result = new Message(messages.getBatchId());
            for (ByteString byteString : messages.getMessagesList()) {
              result.addEntry(Entry.parseFrom(byteString));
            }
            return result;
          }
        case ACK:
          {
            Ack ack = Ack.parseFrom(p.getBody());
            throw new CanalClientException(
                "something goes wrong with reason: " + ack.getErrorMessage());
          }
        default:
          {
            throw new CanalClientException("unexpected packet type: " + p.getType());
          }
      }
    } catch (IOException e) {
      throw new CanalClientException(e);
    }
  }
Exemplo n.º 4
0
 public void rollback(long batchId) throws CanalClientException {
   waitClientRunning();
   ClientRollback ca =
       ClientRollback.newBuilder()
           .setDestination(clientIdentity.getDestination())
           .setClientId(String.valueOf(clientIdentity.getClientId()))
           .setBatchId(batchId)
           .build();
   try {
     writeWithHeader(
         channel,
         Packet.newBuilder()
             .setType(PacketType.CLIENTROLLBACK)
             .setBody(ca.toByteString())
             .build()
             .toByteArray());
   } catch (IOException e) {
     throw new CanalClientException(e);
   }
 }