JMS,ActiveMQ 内部实现的研究

4 篇文章 0 订阅
[b]问题1,AMQ 4的openWireFormat与AMQ 5的不兼容型问题。[/b]
AMQ 4的客户端与AMQ 5的服务端在初次通信时,通过比对自身openWireFormat与AMQ 5发送过来的openWireFormat两者的版本,来确定AMQ 4的客户端选用哪个版本的openWire。现在AMQ 4采用两者之间的最大值,这会造成AMQ 4去使用自身不存在的较高版本的openWire。代码如下:
org.apache.activemq.openwire.OpenWireFormat.renegociatWireFormat 569th line

public void renegociatWireFormat(WireFormatInfo info) throws IOException {

if( preferedWireFormatInfo==null )
throw new IllegalStateException("Wireformat cannot not be renegociated.");

this.setVersion(Math.max(preferedWireFormatInfo.getVersion(), info.getVersion()) );

}

应该采用两者之间的最小值,代码如下:

public void renegociatWireFormat(WireFormatInfo info) throws IOException {

if( preferedWireFormatInfo==null )
throw new IllegalStateException("Wireformat cannot not be renegociated.");

this.setVersion(min(preferedWireFormatInfo.getVersion(), info.getVersion()) );

}


[b]问题1,JMS,ActiveMQ的发送消息原理。[/b]
比如,发送者发送一个TextMessage对象通过JMS中间件发送到JMS server,JMS server如何拿到这个TextMessage,TextMessage在整个流程中做了哪些处理。

TextMessage的内部处理 (针对TCP OpenFire 协议)
org.apache.activemq.openwire.v1.MessageMarshaller
org.apache.activemq.openwire.v1.BaseCommandMarshaller
这两个类完成Object到bytes的转换。


ActiveMQ 将TextMessage里的text转成byte流,使用socket方式发送二进制流。其中会调用:
org.apache.activemq.command.ActiveMQTextMessage.beforeMarshall(WireFormat wireFormat)

TextMessage中的text转成byte流会调用其中下面一个方法

org.apache.activemq.openwire.v1.BaseDataStreamMarshaller

protected void tightMarshalByteSequence2(ByteSequence data, DataOutput dataOut, BooleanStream bs)
throws IOException {
if (bs.readBoolean()) {
dataOut.writeInt(data.getLength());
dataOut.write(data.getData(), data.getOffset(), data.getLength());
}
}




ActiveMQ 接收时将 bytes二进制流转成ByteSequence对象。会调用下面的方法:
ActiveMQ将ByteSequence放入TextMessage的content,其内容会作为ActiveMQTextMessage.getText() 中的输入源的内容。



org.apache.activemq.command.ActiveMQTextMessage.getText()


org.apache.activemq.openwire.v1.BaseDataStreamMarshaller.tightUnmarshalByteSequence(DataInput dataIn, BooleanStream bs)

protected ByteSequence tightUnmarshalByteSequence(DataInput dataIn, BooleanStream bs) throws IOException {
ByteSequence rc = null;
if (bs.readBoolean()) {
int size = dataIn.readInt();
byte[] t = new byte[size];
dataIn.readFully(t);
return new ByteSequence(t, 0, size);
}
return rc;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值