和Deserializer类是:
public class XmlPatternPacketSerializerDeserializer implements Serializer<Document>,
Deserializer<Document> {
protected final Log logger = LogFactory.getLog(this.getClass());
@Override
public Document deserialize(InputStream inputStream) throws IOException {
logger.info("begin deserialize...");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
builderFactory.setNamespaceAware(true);
Document packetDoc = null;
InputSource in = readSocketInputStream(inputStream);
try {
DocumentBuilder builder = builderFactory.newDocumentBuilder();
packetDoc = builder.parse(in);
} catch (Exception e) {
logger.error(e);
}
System.out.println("build id >>> " + packetDoc.getElementsByTagName("building_id").item(0).getTextContent());
return packetDoc;
}
@Override
public void serialize(Document object, OutputStream outputStream)
throws IOException {
// TODO Auto-generated method stub
}
private InputSource readSocketInputStream(InputStream inputStream) throws IOException {
StringBuffer sb = new StringBuffer();
int i = -1;
while((i = inputStream.read())!=-1){
sb.append((char)i);
}
System.out.println(sb.toString());
return new InputSource(new StringReader(new String(sb.toString().getBytes("UTF-8"))));
}
}
数据报包是xml格式,当服务器重新接收数据时,我想反序列化为Document对象。但不幸的是,反序列化时线程被阻塞了。 请帮忙。 集成xml配置在这里:
<bean id="xmlPatternPacketSerializerDeserializer"
class="org.springframework.integration.samples.tcpclientserver.XmlPatternPacketSerializerDeserializer" />
<int-ip:tcp-connection-factory id="serverConnectionFactory"
type="server" using-nio="true" port="${availableServerSocket}" deserializer="xmlPatternPacketSerializerDeserializer"/>
<int-ip:tcp-inbound-channel-adapter id="tcpInbound" channel="tcpReceive"
connection-factory="serverConnectionFactory"/>
<int:service-activator id="tcpHandler" input-channel="tcpReceive"
ref="tcpListener"/>
数据报包在这里:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<common>
<building_id>320411D001</building_id>
<gateway_id>320411D00101</gateway_id>
<type>continuous</type>
</common>
<data operation="continuous">
<sequence>1</sequence>
<parser>yes</parser>
<total>5319</total>
<current>93</current>
<time>20130820160405</time>
<meter id="0001" type="000001">
<function id="01" coding="01D10" error="0" unit="kWh">301.83
</function>
</meter>
<meter id="0002" type="000001">
<function id="01" coding="01D10" error="0" unit="kWh">73.49
</function>
</meter>
<meter id="0003" type="000001">
<function id="01" coding="01D10" error="0" unit="kWh">5.16
</function>
</meter>
<meter id="0004" type="000001">
<function id="01" coding="01D10" error="0" unit="kWh">381.72
</function>
</meter>
<meter id="0005" type="000001">
<function id="01" coding="01D10" error="0" unit="kWh">27.67
</function>
</meter>
<meter id="0006" type="000001">
<function id="01" coding="01A10" error="0" unit="kWh">36.41
</function>
</meter>
<meter id="0007" type="000001">
<function id="01" coding="01A10" error="0" unit="kWh">110.03
</function>
</meter>
<meter id="0008" type="000001">
<function id="01" coding="01A30" error="0" unit="kWh">192.25
</function>
</meter>
<meter id="0009" type="000001">
<function id="01" coding="01A10" error="0" unit="kWh">308.17
</function>
</meter>
<meter id="0010" type="000001">
<function id="01" coding="01C20" error="0" unit="kWh">0.00
</function>
</meter>
<meter id="0011" type="000001">
<function id="01" coding="01A20" error="0" unit="kWh">1305.52
</function>
</meter>
<meter id="0012" type="000001">
<function id="01" coding="01C10" error="0" unit="kWh">0.00
</function>
</meter>
<meter id="0013" type="000001">
<function id="01" coding="01C10" error="0" unit="kWh">492.67
</function>
</meter>
<meter id="0014" type="000001">
<function id="01" coding="01D10" error="0" unit="kWh">6009.05
</function>
</meter>
<meter id="0015" type="000001">
<function id="01" coding="01D10" error="0" unit="kWh">62.60
</function>
</meter>
<meter id="0016" type="000002">
<function id="01" coding="01000" error="0" unit="kWh">237009.5
</function>
<function id="02" coding="00000" error="0" unit="">0.265
</function>
<function id="03" coding="00000" error="0" unit="">49.99
</function>
<function id="04" coding="00000" error="0" unit="W">500.0
</function>
<function id="05" coding="00000" error="0" unit="V">233.99
</function>
<function id="06" coding="00000" error="0" unit="V">233.69
</function>
<function id="07" coding="00000" error="0" unit="V">233.50
</function>
<function id="08" coding="00000" error="0" unit="A">0.000
</function>
<function id="09" coding="00000" error="0" unit="A">0.000
</function>
<function id="10" coding="00000" error="0" unit="A">8.600
</function>
</meter>
</data>
</root>
答案 0 :(得分:0)
这意味着解串器没有获得所有数据,而是等待更多数据。
在连接工厂上设置so-timeout以验证和/或使用调试器进入builder.parse(in);
。
答案 1 :(得分:0)
我的建议是首先将套接字流中的数据读入String
,如果流的末尾由</root>
表示,则在将读取数据(即字符串)传递给
Document document = builder.parse( new InputSource( new StringReader(string ) ) );
如果您的服务器继续传输许多xml消息,那么您可以通过关闭套接字来构建String对象的arrary,直到服务器完成。