Search in sources :

Example 21 with CanalClientException

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

the class ProtocolTest method testSimple.

@Test(expected = CanalClientException.class)
public void testSimple() throws IOException {
    Header.Builder headerBuilder = Header.newBuilder();
    headerBuilder.setLogfileName("mysql-bin.000001");
    headerBuilder.setLogfileOffset(1024);
    headerBuilder.setExecuteTime(1024);
    Entry.Builder entryBuilder = Entry.newBuilder();
    entryBuilder.setHeader(headerBuilder.build());
    entryBuilder.setEntryType(EntryType.ROWDATA);
    Entry entry = entryBuilder.build();
    Message message = new Message(3, true, Arrays.asList(entry.toByteString()));
    byte[] body = buildData(message);
    Packet packet = Packet.parseFrom(body);
    switch(packet.getType()) {
        case MESSAGES:
            {
                if (!packet.getCompression().equals(Compression.NONE)) {
                    throw new CanalClientException("compression is not supported in this connector");
                }
                Messages messages = Messages.parseFrom(packet.getBody());
                Message result = new Message(messages.getBatchId());
                for (ByteString byteString : messages.getMessagesList()) {
                    result.addEntry(Entry.parseFrom(byteString));
                }
                System.out.println(result);
                break;
            }
        default:
            {
                throw new CanalClientException("unexpected packet type: " + packet.getType());
            }
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.CanalPacket.Packet) Entry(com.alibaba.otter.canal.protocol.CanalEntry.Entry) Messages(com.alibaba.otter.canal.protocol.CanalPacket.Messages) Header(com.alibaba.otter.canal.protocol.CanalEntry.Header) Message(com.alibaba.otter.canal.protocol.Message) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

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