/** * hessian protocol * * @param message message * @return chat message * @throws Exception exception */ public ChatMessage hessian(ChatMessage message) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Hessian2Output output = new Hessian2Output(bos); output.writeObject(message); output.flush(); byte[] content = bos.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(content); Hessian2Input input = new Hessian2Input(bis); ChatMessage msg = (ChatMessage) input.readObject(ChatMessage.class); msg.setContentLength(content.length); return msg; }
/** * construct message * * @return chat message */ private ChatMessage constructMessage() { ChatMessage msg = new ChatMessage(); msg.setId(11343L); msg.setUserId(1134); msg.setUserNick("linux_china"); msg.setCreatedAt(new Date()); msg.setScore(2.0042); msg.setBody("这是聊天的内容"); msg.getHeaders().put("contentType", "text/plain"); msg.getHeaders().put("room", "123423"); msg.setContentLength(120); return msg; }
/** * json protocol with jackson * * @param message message * @return chat message * @throws Exception exception */ public ChatMessage jackson(ChatMessage message) throws Exception { byte[] jsonText = objectMapper.writeValueAsBytes(message); ChatMessage msg = objectMapper.readValue(jsonText, ChatMessage.class); msg.setContentLength(jsonText.length); return msg; }
/** * message pack * * @param message message * @return chat message * @throws Exception exception */ public ChatMessage msgpack(ChatMessage message) throws Exception { byte[] content = msgpack.write(message); ChatMessage msg = msgpack.read(content, ChatMessage.class); msg.setContentLength(content.length); return msg; }