我有一个服务器客户端模型,客户端发送一个问候消息,我希望服务器发回一个包含某些字段的对象。我正在使用Avro进行通信。
Avro架构:
"hello": {"request":[{"name": "name", "type": "string"}],
"response": "bytes" }
ByteBuffer hello(CharSequence name) throws AvroRemoteException, IOException;
服务器端:
public ByteBuffer hello(CharSequence name) throws IOException {
Log.info("[Server]: Method 'hello' was invoked...");
Log.info("[Server]: Name: "+ name.toString());
Obj obj = new Obj();
obj.setDate(new Date());
obj.setName("bla hello");
return ByteBuffer.wrap(ByteTransformation.getBytesFromObject(obj));
//return ByteBuffer.wrap(new Utf8("UO").getBytes());
}
客户端:
ByteBuffer response = client.sayHello("Jo");
System.out.println(response.remaining());
Obj obj;
try {
obj =(Obj)ByteTransformation.getObjectFromBytes(response.array());
System.out.println(obj.name);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//========================================================
public ByteBuffer sayHello(String name) throws IOException {
Log.info("[Client] : Method 'sayHello' was invoked...");
if (client==null)
return null;
Mail proxy = (Mail) SpecificRequestor.getClient(Mail.class, client);
return proxy.hello(new Utf8(name));
}
序列化/反序列化
public class ByteTransformation {
public static byte [] getBytesFromObject(Object object) throws IOException {
byte [] result;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(object);
result = bos.toByteArray();
} finally {
try {
if(out!=null)
out.close();
} catch(IOException ex) {
//DO NOTHING
}
try {
bos.close();
} catch(IOException ex) {
//DO NOTHING
}
}
return result;
}
public static Object getObjectFromBytes(byte [] bytes) throws IOException, ClassNotFoundException {
Object result = null;
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
result = in.readObject();
} finally {
try {
if(in!=null)
in.close();
} catch(IOException ex) {
//DO NOTHING
}
try {
bis.close();
} catch(IOException ex) {
//DO NOTHING
}
}
return result;
}
问题:java.io.NotSerializableException:org.thesis.avrorpcserver.MailImpl $ Obj
到目前为止,我只是尝试发送像字符串一样简单的工作人员,当我去发送一个我得到的对象时,它有用吗?为什么?