问题
I have setup a mina(2.0.4) server on PC, it works fine since I have written a mina client on PC to communincation with it. Then I implemented a synchronize Mina Client on android, but the client can not decode the received response message.
I am new to Java and android, I have put 3 whole days effort on that. Anybody please help. thanks all in advance.
I summarized my part code here:
First setup mina client.
public boolean startMinaClient(){
MessageCodecFactory factory = new MessageCodecFactory();
factory.addMessageDecoder(ResponseDecoder.class);
factory.addMessageEncoder(TransRequest.class, RequestEncoder.class);
connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(30000L);
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(factory));
SocketSessionConfig cfg = (SocketSessionConfig) connector.getSessionConfig();
cfg.setUseReadOperation(true);
ConnectFuture future = connector.connect(new InetSocketAddress(server, Config.MINA_SERVER_PORT));
future.awaitUninterruptibly();
session = future.getSession();
}
then send message function.
public synchronized MessageBase send(MessageBase message) throws MinaException {
TransRequest request = new TransRequest(); //TransRequest is a wrapper class, with the to be sent message embedded inside.
request.setMessage(message); //This message can be RegisterRequest.
WriteFuture future = session.write(request); //write to server.
future.awaitUninterruptibly(); //wait server reply.
if(future.getException() != null){
throw new MinaException(future.getException().getMessage()); //
}
ReadFuture readFuture = session.read(); //server replied, and the client try to read the response.
readFuture.awaitUninterruptibly();
if(readFuture.getException() != null) {
throw new MinaException(readFuture.getException().getMessage());
}
TransResponse response = (TransResponse) readFuture.getMessage(); //read operation, need to call decoder.
if(response == null){
throw new MinaException("No response from MINA server.");
}
return response.getMessage();
}
and my decoder classes.
//base class of DecoderBase
public abstract class DecoderBase<T extends MessageBase> extends CodecBase implements MessageDecoder {
private static CharsetDecoder decoder = CodecBase.getCharset().newDecoder();
private Class<T> messageType;
private T decodingMessage;
protected CharsetDecoder getCharsetDecoder() {
return decoder;
}
public GeoDecoderBase() {
messageType = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public MessageDecoderResult decodable(IoSession session, IoBuffer in) {
try {
decodingMessage = messageType.newInstance();
Log.d(TAG, "Decoding messate type: " + messageType.getName()
+ ", content: " + decodingMessage.toString());
} catch (InstantiationException e) {
return MessageDecoderResult.NOT_OK;
} catch (IllegalAccessException e) {
return MessageDecoderResult.NOT_OK;
}
if (in.remaining() < 8/* message header length */) {
return MessageDecoderResult.NEED_DATA; //data not receive complete.
}
int messageCode = in.getInt();
if (messageCode != decodingMessage.getMessageCode()) {
return MessageDecoderResult.NOT_OK; //ensure receive the right message.
}
int messageLen = in.getInt();
if (in.remaining() < messageLen) {
return MessageDecoderResult.NEED_DATA;
}
return MessageDecoderResult.OK;
}
//start to decode message from iobuffer.
public MessageDecoderResult decode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
// First decode message header.
int messageCode = in.getInt();
decodingMessage.setMessageCode(messageCode); //decodingMessage is the message instance.
int messageLen = in.getInt();
int start = in.position();
DecodeMessageBody(in, decodingMessage); //decode the embedded message inside TransResponse.
int actlen = in.position() - start;
if (actlen != messageLen) {
Log.e(TAG, Config.ERROR_STRING + messageType.getName() + " decodes error length. actlen=" + actlen);
return MessageDecoderResult.NOT_OK;
}
out.write(decodingMessage);
return MessageDecoderResult.OK;
}
protected abstract void DecodeMessageBody(IoBuffer in, final T message)
throws Exception;
}
the Reponse message decoder.
public class ResponseDecoder extends DecoderBase<TransResponse> {
private String TAG = "ResponseDecoder";
@Override
protected void DecodeMessageBody(IoBuffer in, TransResponse message)
throws Exception {
int messageCode = in.getInt();
message.setCode(messageCode);
MessageBase base = null;
switch(messageCode){
case MessageCode.REGISTER_RESPONSE:
base = (MessageBase) in.getObject();
break;
default:
Log.e(TAG, Config.ERROR_STRING + "unknown Message Code:0x%x" + messageCode);
break;
}
message.setMessage(base);
}
I can send the TransRequest(RegisterRequest) from the android client to mina server, and can receive the TransResponse(RegisterResponse). But the client alway can not decode it, the error is :
java.lang.ClassNotFoundException: com.geoxy.message.user.RegisterResponse
(Hexdump: 12 00 00 2A 00 00 00 D4 12 04 00 02 00 00 00 CC AC ED 00 05 73 72 01 00 27 63 6F 6D 2E 67
65 6F 78 79 2E 6D 65 73 73 61 67 65 2E 75 73 65 72 2E 52 65 67 69 73 74 65 72 52 65 73 70 6F 6E 73
65 78 72 01 00 1D 63 6F 6D 2E 67 65 6F 78 79 2E 63 6F 6D 6D 6F 6E 2E 52 65 73 70 6F 6E 73 65 42 61
73 65 78 72 01 00 1C 63 6F 6D 2E 67 65 6F 78 79 2E 63 6F 6D 6D 6F 6E 2E 4D 65 73 73 61 67 65 42 61
73 65 78 70 12 04 00 02 73 72 01 00 2D 63 6F 6D 2E 67 65 6F 78 79 2E 63 6F 6D 6D 6F 6E 2E 4D 65 73
73 61 67 65 42 61 73 65 24 4D 65 73 73 61 67 65 53 69 67 6E 61 74 75 72 65 78 70 00 00 00 00 00 00
00 07 00 00 00 00 00 00 00 00 71 00 7E 00 03 00 70 00 00 00 00 00 00 00 07)
the message heander 12 00 00 2A 00 00 00 D4 12 04 00 02 00 00 00 CC is correct.(2 message code/length pair).
my message class.
public abstract class MessageBase implements Serializable {
private static final long serialVersionUID = -6083872909378830262L;
private MessageSignature signatures = new MessageSignature();
private int messageCode;
public MessageBase(){} //c-tor
//inner class.
private class MessageSignature implements Serializable {
private static final long serialVersionUID = -4028675440079310028L;
long para;
public MessageSignature(){} //c-tor
}
}
public abstract class ResponseBase extends MessageBase {
private static final long serialVersionUID = -1007022532151329442L;
protected byte result;
protected String indication = null;
public ResponseBase(){} //c-tor
}
public class RegisterResponse extends ResponseBase {
private long userId;
public RegisterResponse() {} //c-tor
}
I debugged into the mina source code. The exception code line is in the IoBuffer:getObject() method:
IoBuffer:getObject()
@Override
public Object getObject(final ClassLoader classLoader)
throws ClassNotFoundException {
**//classLoader is passed Thread.currentThread().getContextClassLoader()**
if (!prefixedDataAvailable(4)) {
throw new BufferUnderflowException();
}
int length = getInt();
if (length <= 4) {
throw new BufferDataException(
"Object length should be greater than 4: " + length);
}
int oldLimit = limit();
limit(position() + length);
try {
ObjectInputStream in = new ObjectInputStream(asInputStream()) {
@Override
protected ObjectStreamClass readClassDescriptor()
throws IOException, ClassNotFoundException {
int type = read();
if (type < 0) {
throw new EOFException();
}
switch (type) {
case 0: // NON-Serializable class or Primitive types
return super.readClassDescriptor();
case 1: // Serializable class
String className = readUTF(); //**className is correct. "com.....RegisterResponse"**
Class<?> clazz = Class.forName(className, true,
classLoader); //**run to exception.**
return ObjectStreamClass.lookup(clazz);
default:
throw new StreamCorruptedException(
"Unexpected class descriptor type: " + type);
}
}
@Override
protected Class<?> resolveClass(ObjectStreamClass desc)
throws IOException, ClassNotFoundException {
String name = desc.getName();
try {
return Class.forName(name, false, classLoader);
} catch (ClassNotFoundException ex) {
return super.resolveClass(desc);
}
}
};
return in.readObject();
} catch (IOException e) {
throw new BufferDataException(e);
} finally {
limit(oldLimit);
}
}
the exception code line: readClassDescriptor() function.
Class<?> clazz = Class.forName(className, true,
classLoader); //**run to exception.**
and I already put all the Request/Response classes under same package name as they are in Server side code. The sending/receing are run in the AsyncTask. I also have tried to include the mina source code in my package, that didn't resolve.
I doubted below points.
- I can sent Request out, which calls IoBuffer.putObject when do encoding, it can load the Request classes. Why can't not load the Response class.
- I found the Thread.currentThread().getContextClassLoader() seems to be weired. it is a PathClassLoader, which libPath is null, mLibPaths only contains "/system/lib/", path is ".", not like other class loader, which can load classed from .apk file. with this ClassLoader, I think it can not load the RegisterResponse class.is that a thread issue? --the decoding procedure runing in a nio-processor.
来源:https://stackoverflow.com/questions/10088492/run-synchronize-mina-client-on-android-how-to-resolve-classnotfoundexception