-------从我打印的log来看好像不是线程安全的... 也就是说会有多个线程调用doDeocde.这句话说错了,有多个线程调用并不代表就是非线程安全。是否线程安全是根据多个线程和单个线程调用的结果是否一致确定的。
ioseesion不是没用,是隐式用的,如果读不完,就把剩下的存放到iosession里。
ioseesion不是没用,是隐式用的,如果读不完,就把剩下的存放到iosession里。
解决方案 »
- 请教Java大牛给解释一下什么叫J2EE???
- 翻页——servlet-jsp-oracle
- Proxool连接池,读音怎么读?哎……
- 关于session的问题.....
- Hibernate中如何执行不等于查询?
- 在线急求 关于用 hibernate 多表取 并集的问题??祝大家情人节快乐,有情人终成眷属!
- hibernate 两个sessionfactory之间的通信
- ????非常困惑,请兄弟们指点(关于EJB参数问题)!!!!!!!!!!!!!!!!!!!!!!!
- 请大家帮忙想个毕业论文的方向
- MyEclipse10编辑JSP文件很卡
- ssh加extjs的企业信息查询添加个多条件查询求教
- extjs的问题 求各位大神帮帮小弟
嗯,对的,我说错了如果一次没有读完,假设这个ioseesion是A,如何保证下次来的数据是A的呢,其它线程也会写数据的..是不是自己要写同步方法维护?
//private IoBuffer buff = IoBuffer.allocate(320).setAutoExpand(true);
private final AttributeKey CONTEXT = new AttributeKey(getClass(), "context");
private Context getContext(IoSession session) {
Context context = (Context) session.getAttribute(CONTEXT);
if (null == context) {
context = new Context();
session.setAttribute(CONTEXT, context);
}
return context;
}
@Override
public boolean doDecode(IoSession iosession, IoBuffer iobuffer, ProtocolDecoderOutput out) throws Exception {
boolean complete = false;
Context ctx = this.getContext(iosession);
ctx.append(iobuffer);
IoBuffer buf = ctx.getBuffer();
buf.flip();
while (buf.hasRemaining()) {
byte b = buf.get();
/**
* CIMConstant.MESSAGE_SEPARATE 为消息界限
* 当一次收到多个消息时,以此分隔解析多个消息
*/
if (b == CIMConstant.MESSAGE_SEPARATE ) {
complete = true;
break;
} /*else {
buff.put(b);
}*/
}
if (complete) {
buf.flip();
byte[] bytes = new byte[buf.limit()-1];
buf.get(bytes);
String message = new String(bytes, charset);
buf.clear();
ctx.reset();
System.out.println("iosessionid = " + iosession.getId() +" &&&&&&&&&&&&& = " +message);
SentBody body = new SentBody();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(message.getBytes(charset)));
body.setKey(doc.getElementsByTagName("key").item(0).getTextContent());
NodeList items = doc.getElementsByTagName("data").item(0).getChildNodes();
for (int i = 0; i < items.getLength(); i++) {
Node node = items.item(i);
body.getData().put(node.getNodeName(), node.getTextContent());
}
out.write(body);
}
return complete;
}
private class Context {
private final IoBuffer innerBuffer; public Context() {
innerBuffer = IoBuffer.allocate(320).setAutoExpand(true);
} public void reset() {
this.innerBuffer.clear();
} public void append(IoBuffer in) {
innerBuffer.put(in); } public IoBuffer getBuffer() {
return this.innerBuffer;
}
}