@Test public void serialize() { RetryMessageModel retry = new RetryMessageModel(); retry.setBusinessId("business"); retry.setTopic("topic"); retry.setApp("app"); retry.setPartition((short) 255); retry.setIndex(100L); retry.setBrokerMessage(new byte[168]); retry.setException(new byte[16]); retry.setSendTime(SystemClock.now()); ByteBuffer serialize = RetrySerializerUtil.serialize(retry); RetryMessageModel model = RetrySerializerUtil.deserialize(ByteBufAllocator.DEFAULT.buffer().writeBytes(serialize.array())); Assert.assertEquals(model.getBusinessId(), retry.getBusinessId()); Assert.assertEquals(model.getTopic(), retry.getTopic()); Assert.assertEquals(model.getApp(), retry.getApp()); Assert.assertEquals(model.getPartition(), retry.getPartition()); Assert.assertEquals(model.getIndex(), retry.getIndex()); Assert.assertEquals(model.getSendTime(), retry.getSendTime()); Assert.assertArrayEquals(model.getBrokerMessage(), retry.getBrokerMessage()); Assert.assertArrayEquals(model.getException(), retry.getException()); }
public static ByteBuffer serialize(RetryMessageModel retryMessageModel) { int size = size(retryMessageModel); ByteBuffer allocate = ByteBuffer.allocate(size); String businessId = retryMessageModel.getBusinessId(); if (StringUtils.isNotEmpty(businessId)) { byte[] bytes = businessId.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short) 0); } String topic = retryMessageModel.getTopic(); if (StringUtils.isNotEmpty(topic)) { byte[] bytes = topic.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short)0); } String app = retryMessageModel.getApp(); if (StringUtils.isNotEmpty(app)) { byte[] bytes = app.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short)0); } short partition = retryMessageModel.getPartition(); allocate.putShort(partition); long index = retryMessageModel.getIndex(); allocate.putLong(index); byte[] brokerMessage = retryMessageModel.getBrokerMessage(); allocate.putInt(brokerMessage.length); allocate.put(brokerMessage); byte[] exception = retryMessageModel.getException(); if (exception != null) { allocate.putShort((short)exception.length); allocate.put(exception); } else { allocate.putShort((short)0); } long sendTime = retryMessageModel.getSendTime(); allocate.putLong(sendTime); allocate.flip(); return allocate; }
RetrySerializerUtil { public static ByteBuffer serialize(RetryMessageModel retryMessageModel) { int size = size(retryMessageModel); ByteBuffer allocate = ByteBuffer.allocate(size); String businessId = retryMessageModel.getBusinessId(); if (StringUtils.isNotEmpty(businessId)) { byte[] bytes = businessId.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short) 0); } String topic = retryMessageModel.getTopic(); if (StringUtils.isNotEmpty(topic)) { byte[] bytes = topic.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short)0); } String app = retryMessageModel.getApp(); if (StringUtils.isNotEmpty(app)) { byte[] bytes = app.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short)0); } short partition = retryMessageModel.getPartition(); allocate.putShort(partition); long index = retryMessageModel.getIndex(); allocate.putLong(index); byte[] brokerMessage = retryMessageModel.getBrokerMessage(); allocate.putInt(brokerMessage.length); allocate.put(brokerMessage); byte[] exception = retryMessageModel.getException(); if (exception != null) { allocate.putShort((short)exception.length); allocate.put(exception); } else { allocate.putShort((short)0); } long sendTime = retryMessageModel.getSendTime(); allocate.putLong(sendTime); allocate.flip(); return allocate; } }
RetrySerializerUtil { public static ByteBuffer serialize(RetryMessageModel retryMessageModel) { int size = size(retryMessageModel); ByteBuffer allocate = ByteBuffer.allocate(size); String businessId = retryMessageModel.getBusinessId(); if (StringUtils.isNotEmpty(businessId)) { byte[] bytes = businessId.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short) 0); } String topic = retryMessageModel.getTopic(); if (StringUtils.isNotEmpty(topic)) { byte[] bytes = topic.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short)0); } String app = retryMessageModel.getApp(); if (StringUtils.isNotEmpty(app)) { byte[] bytes = app.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short)0); } short partition = retryMessageModel.getPartition(); allocate.putShort(partition); long index = retryMessageModel.getIndex(); allocate.putLong(index); byte[] brokerMessage = retryMessageModel.getBrokerMessage(); allocate.putInt(brokerMessage.length); allocate.put(brokerMessage); byte[] exception = retryMessageModel.getException(); if (exception != null) { allocate.putShort((short)exception.length); allocate.put(exception); } else { allocate.putShort((short)0); } long sendTime = retryMessageModel.getSendTime(); allocate.putLong(sendTime); allocate.flip(); return allocate; }  }
RetrySerializerUtil { public static ByteBuffer serialize(RetryMessageModel retryMessageModel) { int size = size(retryMessageModel); ByteBuffer allocate = ByteBuffer.allocate(size); String businessId = retryMessageModel.getBusinessId(); if (StringUtils.isNotEmpty(businessId)) { byte[] bytes = businessId.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short) 0); } String topic = retryMessageModel.getTopic(); if (StringUtils.isNotEmpty(topic)) { byte[] bytes = topic.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short)0); } String app = retryMessageModel.getApp(); if (StringUtils.isNotEmpty(app)) { byte[] bytes = app.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short)0); } short partition = retryMessageModel.getPartition(); allocate.putShort(partition); long index = retryMessageModel.getIndex(); allocate.putLong(index); byte[] brokerMessage = retryMessageModel.getBrokerMessage(); allocate.putInt(brokerMessage.length); allocate.put(brokerMessage); byte[] exception = retryMessageModel.getException(); if (exception != null) { allocate.putShort((short)exception.length); allocate.put(exception); } else { allocate.putShort((short)0); } long sendTime = retryMessageModel.getSendTime(); allocate.putLong(sendTime); allocate.flip(); return allocate; }  static RetryMessageModel deserialize(ByteBuf buffer); static ByteBuffer serialize(RetryMessageModel retryMessageModel); }
RetrySerializerUtil { public static ByteBuffer serialize(RetryMessageModel retryMessageModel) { int size = size(retryMessageModel); ByteBuffer allocate = ByteBuffer.allocate(size); String businessId = retryMessageModel.getBusinessId(); if (StringUtils.isNotEmpty(businessId)) { byte[] bytes = businessId.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short) 0); } String topic = retryMessageModel.getTopic(); if (StringUtils.isNotEmpty(topic)) { byte[] bytes = topic.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short)0); } String app = retryMessageModel.getApp(); if (StringUtils.isNotEmpty(app)) { byte[] bytes = app.getBytes(Charset.forName("UTF-8")); allocate.putShort((short) bytes.length); allocate.put(bytes); } else { allocate.putShort((short)0); } short partition = retryMessageModel.getPartition(); allocate.putShort(partition); long index = retryMessageModel.getIndex(); allocate.putLong(index); byte[] brokerMessage = retryMessageModel.getBrokerMessage(); allocate.putInt(brokerMessage.length); allocate.put(brokerMessage); byte[] exception = retryMessageModel.getException(); if (exception != null) { allocate.putShort((short)exception.length); allocate.put(exception); } else { allocate.putShort((short)0); } long sendTime = retryMessageModel.getSendTime(); allocate.putLong(sendTime); allocate.flip(); return allocate; }  static RetryMessageModel deserialize(ByteBuf buffer); static ByteBuffer serialize(RetryMessageModel retryMessageModel);  }