扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年9月23日
关键字:
在本页阅读全文(共6页)
一个处理程序能够选择性地实现任何 RemoteModel 接口中的动作方法 ,采用两种方法之一:
RemoteModelProxy 类调用一个动作方法不能通过前面的 handler 类完成,那么基类 RemoteModelRequestHandler 类能够public class RMSCacheHandler extends RemoteModelRequestHandler {
// ...
public Movie getMovie(String movieKey)
throws ModelException,
ApplicationException {
IndexEntry indexEntry =
rmsAdapter.getIndexEntry(movieKey,
IndexEntry.TYPE_MOVIE, IndexEntry.MODE_ANY);
if (indexEntry != null) {
return rmsAdapter.loadMovie(indexEntry.getRecordId());
}
return super.getMovie(movieKey);
}
// ...
}
|
在模型层,RemoteModelProxy 类中的 HTTPCommunicationHandler 类通过 HTTP 连接上的二进制 RPC 协议调用服务器端的远程过程。该协议定义如下:
所有从客户端到服务器端的 RPC 请求都遵循同一基本模式。数据流的第一个字节指定服务器端的界面会话 bean 必须执行的动作方法,余下的字节编码为 UTF 数据串序列,它表示传递给远程方法的参数。HTTP 数据流包含 RPC 返回值。请求和响应的格式对每一种方法来说都是唯一的,所以,你必须查看每一种方法的源代码,以确定准确的格式。
进入请求数据流第一个字节的 RPC 编码在 MessageConstants 类中进行定义:
package com.sun.j2me.blueprints.smartticket.shared.midp;
public final class MessageConstants {
public static final byte OPERATION_LOGIN_USER = 0;
public static final byte OPERATION_CREATE_ACCOUNT = 1;
public static final byte OPERATION_UPDATE_ACCOUNT = 2;
public static final byte OPERATION_GET_THEATERS = 3;
public static final byte OPERATION_GET_THEATER_SCHEDULE = 4;
public static final byte OPERATION_GET_MOVIE = 5;
public static final byte OPERATION_GET_MOVIE_POSTER = 6;
public static final byte OPERATION_GET_MOVIE_SHOWTIMES = 7;
public static final byte OPERATION_GET_SEATING_PLAN = 8;
public static final byte OPERATION_RESERVE_SEATS = 9;
public static final byte OPERATION_PURCHASE_TICKETS = 10;
public static final byte OPERATION_CANCEL_SEAT_RESERVATION = 11;
public static final byte OPERATION_GET_LOCALES = 12;
public static final byte OPERATION_GET_RESOURCE_BUNDLE = 13;
public static final byte OPERATION_INITIATE_SYNCHRONIZATION = 14;
public static final byte OPERATION_SYNCHRONIZE_MOVIE_RATINGS = 15;
public static final byte OPERATION_COMMIT_MOVIE_RATINGS = 16;
public static final byte ERROR_NONE = 0;
public static final byte ERROR_UNKNOWN_OPERATION = 1;
public static final byte ERROR_SERVER_ERROR = 2;
public static final byte ERROR_MODEL_EXCEPTION = 3;
public static final byte ERROR_REQUEST_FORMAT = 4;
private MessageConstants() {}
}
|
下面的两个类解释了一个 RPC 往返过程,HTTPCommunicationHandler 类的动作方法请求指定电影的信息,并调用 Movie 类的一个方法来提取响应流中的返回值。
package com.sun.j2me.blueprints.smartticket.client.midp.model;
public class HTTPCommunicationHandler
extends RemoteModelRequestHandler {
// ...
public Movie getMovie(String movieKey)
throws ModelException,
ApplicationException {
HttpConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
try {
connection = openConnection();
updateProgress();
outputStream = openConnectionOutputStream(connection);
// The RPC request
outputStream.writeByte(MessageConstants.OPERATION_GET_MOVIE);
outputStream.writeUTF(movieKey);
outputStream.close();
updateProgress();
// unmarshal the return values
inputStream = openConnectionInputStream(connection);
Movie movie = Movie.deserialize(inputStream);
updateProgress();
return movie;
} catch (IOException ioe) {
throw new
ApplicationException(ErrorMessageCodes.ERROR_CANNOT_CONNECT);
} finally {
closeConnection(connection, outputStream, inputStream);
}
}
// Other action methods ...
}
package com.sun.j2me.blueprints.smartticket.shared.midp.model;
public class Movie {
private String primaryKey;
private String title;
private String summary;
private String rating;
private boolean alreadySeen = false;
transient private byte[] poster = null;
public static Movie deserialize(DataInputStream
dataStream) throws ApplicationException {
try {
Movie movie = new Movie();
// Read the RPC response stream
movie.primaryKey = dataStream.readUTF();
movie.title = dataStream.readUTF();
movie.summary = dataStream.readUTF();
movie.rating = dataStream.readUTF();
try {
movie.alreadySeen = dataStream.readBoolean();
} catch (IOException ioe) {
movie.alreadySeen = false;
}
try {
return
ModelObjectLoader.getInstance().getMovie(movie);
} catch (ModelException me) {
throw new ApplicationException();
}
} catch (IOException ioe) {
throw new ApplicationException(ioe);
}
}
// Other methods ...
}
|
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。