ตัวอย่างที่ผมใช้คือ การส่งข้อมูลผ่านเน็ตเวิร์ค ด้วยเทคโนโลยี Java Message Service (JMS) ซึ่งจะเป็นการส่งข้อมูล ที่เป็น Object บางอย่างไปยัง JMS Server และ JMS Server ก็จะทำการส่งข้อมูลนั้นไปยังปลายทางที่กำหนดอีกที
ทำไมถึงเลือกใช้ Hession ?
ผมมองเห็นข้อดีของมันอย่างนึงครับ นั่นคือการบีบอัดข้อมูล อันนี้เจ๋งมาก ผมเคยลงบีบอัดข้อมูล 1 แสน recored ทีแต่ละ record มีข้อมูลประมาณ 20 filed โดยข้อมูลไม่ซ้ำกัน มันบีบอัดได้ประมาณ 3 MB จากนั้นก็ส่งข้อมูลนั้นผ่าน network ได้สบายหมูเลยครับ ที่มันเจ๋งอีกอย่างคือ ถ้าข้อมูลเราซ้ำกันมากๆ มันก็ยิ่งบีบอัดได้น้อยมาก ตัวอย่างเดิมครับ ข้อมูล 1 แสน record ที่มีข้อมูลซ้ำกันทุก record มีแค่ record เดียวที่ไม่ซ้ำ นั่นคือ pid ที่เป็น primary key String ขนาด 13 หลัก บีบอัดทั้งหมดได้ 427 KB เองครับ ว้าววววววว เจ๋งเลย
package com.blogspot.na5cent.serializer;
import com.caucho.hessian.io.Deflation;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
/**
*
* @author redcrow
*/
public class Serializer {
public static byte[] toBytes(List<?> objectsGhrap) throws IOException {
Deflation envelope = new Deflation();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Hessian2Output out = new Hessian2Output(bos);
out = envelope.wrap(out);
out.startMessage();
out.writeObject(objectsGhrap);
out.completeMessage();
out.close();
return bos.toByteArray();
}
public static Object toObject(byte[] datas) throws IOException {
Deflation envelope = new Deflation();
ByteArrayInputStream bin = new ByteArrayInputStream(datas);
Hessian2Input in = new Hessian2Input(bin);
in = envelope.unwrap(in);
in.startMessage();
Object value = in.readObject();
in.completeMessage();
return value;
}
}
ตัวอย่างโค๊ดแปลงข้อมูล ในฝั่งส่งข้อมูล
...
...
@Override
public void send(List<Data> d) throws IOException {
final byte[] data = Serializer.toBytes(d); //*****************
MessageCreator messageCreator = new MessageCreator() {
@Override
public Message createMessage(Session sn) throws JMSException {
BytesMessage bytesMessage = sn.createBytesMessage();
bytesMessage.writeBytes(data);
return bytesMessage;
}
};
if (messageCreator != null) {
JmsTemplate jmsTemplate = getJmsTemplate();
jmsTemplate.setExplicitQosEnabled(true);
jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT);
jmsTemplate.setTimeToLive(project.getExpireTimeInMillisecs());
jmsTemplate.send(QueueUtils.JMS_QUEUE_NAME, messageCreator);
} else {
LOG.debug("message creator is null");
}
}
ตัวอย่างโค๊ดแปลงข้อมูล ในฝั่งรับข้อมูล
...
...
@Autowired
private DataService dataService;
@Override
public void onMessage(Message message, Session sn) throws JMSException {
if (message instanceof BytesMessage) {
try {
BytesMessage bytesMessage = (BytesMessage) message;
int messageLength = new Long(bytesMessage.getBodyLength()).intValue();
byte[] byteRead = new byte[messageLength];
bytesMessage.readBytes(byteRead, messageLength);
List<Data> data = (List<Data>) Serializer.toObject(byteRead); //***************
dataService.saveAllData(data);
LOG.debug("Recieve Data From JMS Server");
} catch (IOException ex) {
LOG.debug("{}", ex);
}
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น