use of org.apache.rocketmq.connect.runtime.converter.RocketMQConverter in project rocketmq-externals by apache.
the class WorkerSourceTask method sendRecord.
/**
* Send list of sourceDataEntries to MQ.
*
* @param sourceDataEntries
*/
private void sendRecord(Collection<SourceDataEntry> sourceDataEntries) {
for (SourceDataEntry sourceDataEntry : sourceDataEntries) {
ByteBuffer partition = sourceDataEntry.getSourcePartition();
Optional<ByteBuffer> opartition = Optional.ofNullable(partition);
ByteBuffer position = sourceDataEntry.getSourcePosition();
Optional<ByteBuffer> oposition = Optional.ofNullable(position);
sourceDataEntry.setSourcePartition(null);
sourceDataEntry.setSourcePosition(null);
Message sourceMessage = new Message();
sourceMessage.setTopic(sourceDataEntry.getQueueName());
if (null == recordConverter || recordConverter instanceof RocketMQConverter) {
if (StringUtils.isNotEmpty(sourceDataEntry.getShardingKey())) {
MessageAccessor.putProperty(sourceMessage, RuntimeConfigDefine.CONNECT_SHARDINGKEY, sourceDataEntry.getShardingKey());
}
if (StringUtils.isNotEmpty(sourceDataEntry.getQueueName())) {
MessageAccessor.putProperty(sourceMessage, RuntimeConfigDefine.CONNECT_TOPICNAME, sourceDataEntry.getQueueName());
}
if (opartition.isPresent()) {
MessageAccessor.putProperty(sourceMessage, RuntimeConfigDefine.CONNECT_SOURCE_PARTITION, new String(opartition.get().array()));
}
if (oposition.isPresent()) {
MessageAccessor.putProperty(sourceMessage, RuntimeConfigDefine.CONNECT_SOURCE_POSITION, new String(oposition.get().array()));
}
EntryType entryType = sourceDataEntry.getEntryType();
Optional<EntryType> oentryType = Optional.ofNullable(entryType);
if (oentryType.isPresent()) {
MessageAccessor.putProperty(sourceMessage, RuntimeConfigDefine.CONNECT_ENTRYTYPE, oentryType.get().name());
}
Long timestamp = sourceDataEntry.getTimestamp();
Optional<Long> otimestamp = Optional.ofNullable(timestamp);
if (otimestamp.isPresent()) {
MessageAccessor.putProperty(sourceMessage, RuntimeConfigDefine.CONNECT_TIMESTAMP, otimestamp.get().toString());
}
Schema schema = sourceDataEntry.getSchema();
Optional<Schema> oschema = Optional.ofNullable(schema);
if (oschema.isPresent()) {
MessageAccessor.putProperty(sourceMessage, RuntimeConfigDefine.CONNECT_SCHEMA, JSON.toJSONString(oschema.get()));
}
Object[] payload = sourceDataEntry.getPayload();
if (null != payload && null != payload[0]) {
Object object = payload[0];
final byte[] messageBody = (String.valueOf(object)).getBytes();
if (messageBody.length > RuntimeConfigDefine.MAX_MESSAGE_SIZE) {
log.error("Send record, message size is greater than {} bytes, payload: {}", RuntimeConfigDefine.MAX_MESSAGE_SIZE, sourceDataEntry.getPayload());
return;
}
sourceMessage.setBody(messageBody);
}
} else {
byte[] payload = recordConverter.objectToByte(sourceDataEntry.getPayload());
Object[] newPayload = new Object[1];
newPayload[0] = Base64.getEncoder().encodeToString(payload);
sourceDataEntry.setPayload(newPayload);
final byte[] messageBody = JSON.toJSONString(sourceDataEntry).getBytes();
if (messageBody.length > RuntimeConfigDefine.MAX_MESSAGE_SIZE) {
log.error("Send record, message size is greater than {} bytes, payload: {}", RuntimeConfigDefine.MAX_MESSAGE_SIZE, sourceDataEntry.getPayload());
return;
}
sourceMessage.setBody(messageBody);
}
try {
producer.send(sourceMessage, new SendCallback() {
@Override
public void onSuccess(org.apache.rocketmq.client.producer.SendResult result) {
log.info("Successful send message to RocketMQ:{}", result.getMsgId());
try {
if (null != partition && null != position) {
positionManagementService.putPosition(partition, position);
}
} catch (Exception e) {
log.error("Source task save position info failed.", e);
}
}
@Override
public void onException(Throwable throwable) {
if (null != throwable) {
log.error("Source task send record failed {}.", throwable);
}
}
});
} catch (MQClientException e) {
log.error("Send message error. message: {}, error info: {}.", sourceMessage, e);
} catch (RemotingException e) {
log.error("Send message error. message: {}, error info: {}.", sourceMessage, e);
} catch (InterruptedException e) {
log.error("Send message error. message: {}, error info: {}.", sourceMessage, e);
}
}
}
use of org.apache.rocketmq.connect.runtime.converter.RocketMQConverter in project rocketmq-externals by apache.
the class WorkerSinkTask method convertToSinkDataEntry.
private SinkDataEntry convertToSinkDataEntry(MessageExt message) {
Map<String, String> properties = message.getProperties();
String queueName;
EntryType entryType;
Schema schema;
Long timestamp;
Object[] datas = new Object[1];
if (null == recordConverter || recordConverter instanceof RocketMQConverter) {
queueName = properties.get(RuntimeConfigDefine.CONNECT_TOPICNAME);
String connectEntryType = properties.get(RuntimeConfigDefine.CONNECT_ENTRYTYPE);
entryType = StringUtils.isNotEmpty(connectEntryType) ? EntryType.valueOf(connectEntryType) : null;
String connectTimestamp = properties.get(RuntimeConfigDefine.CONNECT_TIMESTAMP);
timestamp = StringUtils.isNotEmpty(connectTimestamp) ? Long.valueOf(connectTimestamp) : null;
String connectSchema = properties.get(RuntimeConfigDefine.CONNECT_SCHEMA);
schema = StringUtils.isNotEmpty(connectSchema) ? JSON.parseObject(connectSchema, Schema.class) : null;
datas = new Object[1];
datas[0] = message.getBody();
} else {
final byte[] messageBody = message.getBody();
final SourceDataEntry sourceDataEntry = JSON.parseObject(new String(messageBody), SourceDataEntry.class);
final Object[] payload = sourceDataEntry.getPayload();
final byte[] decodeBytes = Base64.getDecoder().decode((String) payload[0]);
Object recodeObject;
if (recordConverter instanceof JsonConverter) {
JsonConverter jsonConverter = (JsonConverter) recordConverter;
jsonConverter.setClazz(Object[].class);
recodeObject = recordConverter.byteToObject(decodeBytes);
datas = (Object[]) recodeObject;
}
schema = sourceDataEntry.getSchema();
entryType = sourceDataEntry.getEntryType();
queueName = sourceDataEntry.getQueueName();
timestamp = sourceDataEntry.getTimestamp();
}
DataEntryBuilder dataEntryBuilder = new DataEntryBuilder(schema);
dataEntryBuilder.entryType(entryType);
dataEntryBuilder.queue(queueName);
dataEntryBuilder.timestamp(timestamp);
List<Field> fields = schema.getFields();
if (null != fields && !fields.isEmpty()) {
for (Field field : fields) {
dataEntryBuilder.putFiled(field.getName(), datas[field.getIndex()]);
}
}
SinkDataEntry sinkDataEntry = dataEntryBuilder.buildSinkDataEntry(message.getQueueOffset());
return sinkDataEntry;
}
Aggregations