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());
}
}
}
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);
}
}
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);
}
}
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;
}
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);
}
}
Aggregations