不再需要时自动关闭 TIBCO EMS 连接
我们正在使用 ASP.NET 3.5 应用程序中的 TIBCO EMS 作为与外部系统的一个接口,它似乎工作得很好 - 除了运行另一端的人告诉我们,我们正在疯狂地建立连接,而且从来没有关闭它们......
我正在做的是通过一个类路由所有 TIBCO 流量,该类具有 TIBCO ConnectionFactory
和 Connection
本身的静态成员变量,告诉我构建它们非常耗费资源和时间:
private static ConnectionFactory Factory
{
get
{
if (HttpContext.Current.Application["EMSConnectionFactory"] == null)
{
ConnectionFactory connectionFactory = CreateTibcoFactory();
HttpContext.Current.Application["EMSConnectionFactory"] = connectionFactory;
}
return HttpContext.Current.Application["EMSConnectionFactory"] as ConnectionFactory;
}
}
private static Connection EMSConnection
{
get
{
if (HttpContext.Current.Application["EMSConnection"] == null)
{
Connection connection = Factory.CreateConnection(*username*, *password*);
connection.ExceptionHandler += new EMSExceptionHandler(TibcoConnectionExceptionHandler);
connection.Start();
HttpContext.Current.Application["EMSConnection"] = connection;
}
return HttpContext.Current.Application["EMSConnection"] as Connection;
}
}
现在我的麻烦是:在哪里以及如何
- 告诉 TIBCO 连接在不再需要时“自动关闭”(例如使用
SqlConnection
) - 发生错误时关闭 TIBCO 连接
- 在我们的 ASP.NET 应用程序完成(或用户注销)之前关闭 TIBCO 连接
我似乎没有真正找到有关如何从 C# / .NET 世界使用 TIBCO EMS 的有用信息。 ......有接受者吗?谢谢!!
We're using TIBCO EMS from our ASP.NET 3.5 app for one interface to an external system, and it appears to be working just fine - except that the guys running the other side tells us we're racking up connections like crazy and never closing them....
What I'm doing is routing all TIBCO traffic through a single class with static member variables for both the TIBCO ConnectionFactory
and the Connection
itself, having been told that constructing them is pretty resource- and time-intensive:
private static ConnectionFactory Factory
{
get
{
if (HttpContext.Current.Application["EMSConnectionFactory"] == null)
{
ConnectionFactory connectionFactory = CreateTibcoFactory();
HttpContext.Current.Application["EMSConnectionFactory"] = connectionFactory;
}
return HttpContext.Current.Application["EMSConnectionFactory"] as ConnectionFactory;
}
}
private static Connection EMSConnection
{
get
{
if (HttpContext.Current.Application["EMSConnection"] == null)
{
Connection connection = Factory.CreateConnection(*username*, *password*);
connection.ExceptionHandler += new EMSExceptionHandler(TibcoConnectionExceptionHandler);
connection.Start();
HttpContext.Current.Application["EMSConnection"] = connection;
}
return HttpContext.Current.Application["EMSConnection"] as Connection;
}
}
Now my trouble is: where and how could I
- tell the TIBCO connection to "auto-close" when no longer needed (like with the
SqlConnection
) - close the TIBCO connection on an error
- close the TIBCO connection before our ASP.NET app finishes (or the user logs off)
I don't really seem to find much useful information on how to use TIBCO EMS from the C# / .NET world...... any takers?? Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我不明白你怎么会耗尽连接。由于您将连接存储在应用程序中,因此整个 IIS 应用程序应该只有一个连接。
抛开这一点,我会执行以下操作:
DateTime
设置为DateTime.Now
;DateTime.Now
之间的差异。如果超过特定超时,则终止连接并将Application["EMSConnectionFactory"]
设置为 null;重置为
DateTime.Now`。这样,连接应该自动关闭。
请注意,您必须引入锁定。您可以使用
Application.Lock()
和Application.Unlock()
来实现此目的。关于关闭错误:我看到您已将异常处理程序附加到连接实例。你不能关闭它的连接吗?
Firstly, I don't understand how you could be running out of connections. Since you're storing the connection in the application, you should only have a single connection for the entire IIS application.
That put aside, I would do the following:
DateTime
toDateTime.Now
;DateTime.Now
. If that's longer than a specific timeout, kill the connection and setApplication["EMSConnectionFactory"]
to null;to
DateTime.Now`.This way, the connections should be closed automatically.
Note that you will have to introduce locking. You can use
Application.Lock()
andApplication.Unlock()
for this.Concerning closing on an error: I see that you've attached an exception handler to the connection instance. Can't you close the connection with that?