0

我们开发了一个通过 IBM Websphere MQ 进行通信的自定义 WCF 通道。

我们创建了一个通道工厂:

public class MqChannelFactory : ChannelFactoryBase<IRequestChannel>

它返回我们频道的实例:

public class MqRequestChannel : ChannelBase, IRequestChannel

连接到 IBM MQ 队列管理器是一项昂贵的操作。目前,我们在 Channel.OnOpen() 中执行此操作。

遵循正确使用通道的指南,我们每次需要通道时调用 ChannelFactory.CreateChannel(),发送消息,然后调用 Channel.Close()。

我们的假设是,ChannelFactory 执行了通道池,因此当 Channel.Close() 被调用时,通道实际上并没有关闭,而是返回到池中。但是,每次我们调用 ChannelFactory.CreateChannel 时,都会实例化一个新的通道,并且在发送请求时,会执行昂贵的通道打开。

所以,问题是:防止在每个请求上打开通道的最佳方法是什么?

我们正在研究的一些选项:

  • 无论如何通过配置来指定应该进行通道池吗?我们是否应该在 ChannelFactory 中实现自己的通道池?

  • 我们是否应该在应用程序的整个生命周期内保持通道打开,通过它发送所有请求?

  • 我们是否应该在通道工厂中执行昂贵的操作(连接到队列管理器),我们在应用程序的生命周期中缓存这些操作?

4

1 回答 1

1

对于最佳选择是什么,真的没有硬性规定。最初,我会说可能最简单的解决方案是在应用程序级别缓存客户端通道。顺便说一句,根据您的实现,这可能需要一些同步工作。

You could potentially pool connections at the ChannelFactory level. I'd be hesitant to pool entire channels (there's always some complexity there), but you could pool connections internally and have channels acquire connections when needed from the pool held by their channel factory.

This does have the advantage that ClientBase already caches ClientFactory instances as of .NET 3.0 SP1, so that might make it easier on the application code (if you're using it).

The downside, though, is that that this might get harder to implement if the endpoint address has information that is needed to open the connection to the queue manager because you can potentially create channels for different endpoint addresses from a single ChannelFactory object. Likely this means that you'll need to explicitly disallow this, or that your ChannelFactory implementation might need to keep multiple connection pools internally (one per endpoint address or something like that).

于 2010-01-13T04:10:48.423 回答