`
7wolfs
  • 浏览: 178206 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

JMS,ActiveMQ 内部实现的研究

 
阅读更多
问题1,AMQ 4的openWireFormat与AMQ 5的不兼容型问题。
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()) );
		
	}


问题1,JMS,ActiveMQ的发送消息原理。
比如,发送者发送一个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;
    }



分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics