SQL Service Broker 和 .NET Windows 服务 - 最佳实践?
我目前有一个从旧应用程序更新的数据库。 我想利用 SQL Service Broker 队列,以便在更新记录时,将一条消息放入队列中(使用触发器或其他东西)。
然后,我希望有一个长时间运行的应用程序(用 .NET 编写的 Windows 服务),它不断“监听”队列以获取消息并为另一个应用程序处理它们。
我在网上找到了一些示例代码,只是想了解一些关于代码是否可靠的信息。 那么,这是 Windows 服务类的缩写版本:
public class MyService
{
public void StartService()
{
Thread listener = new Thread(Listen);
listener.IsBackground = true;
listener.Start();
}
private void Listen()
{
while (true)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
string commandText = "WAITFOR ( RECEIVE * FROM MyQueue);";
using (SqlCommand command = new SqlCommand(commandText, connection))
{
connection.Open();
command.CommandTimeout = 0;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
// Process message
}
}
}
}
}
}
您觉得怎么样? 该代码完全按照我想要的方式工作。 但是,在无限循环内分出一个包含 SQL 命令的新线程永远不会超时的想法让我有点紧张。
I currently have a database that gets updated from a legacy application. I'd like to utilize a SQL Service Broker queue so that when a record is updated, a message is placed in the queue (using a trigger or something).
I would then like to have a long running application (Windows service written in .NET) that is constantly "listening" to the queue to grab the messages and process them for another application.
I've found some sample code on the web, and just wanted to get some input on whether the code is solid or not. So, here's an abbreviated version of the Windows service class:
public class MyService
{
public void StartService()
{
Thread listener = new Thread(Listen);
listener.IsBackground = true;
listener.Start();
}
private void Listen()
{
while (true)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
string commandText = "WAITFOR ( RECEIVE * FROM MyQueue);";
using (SqlCommand command = new SqlCommand(commandText, connection))
{
connection.Open();
command.CommandTimeout = 0;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
// Process message
}
}
}
}
}
}
What do you think? The code works exactly the way I want it to. But the idea of spinning off a new thread that contains a SQL command will never time out - inside an infinite loop - makes me a little nervous.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您处于无限循环中,您的服务将不会完全关闭。 您应该检查退出条件,在服务收到关闭消息时设置该退出条件。 您可以向 WAITFOR 添加超时,以便您可以检查是否应该关闭。 您还应该在处理的每一行上检查这一点。
我使用了 2 秒睡眠来实现延迟,但在 WAITFOR 上执行 2 秒超时也可以实现同样的效果。
服务有 30 秒的时间关闭,否则 Windows 将认为它卡住了。
Your service will not shutdown cleanly if you are in a infinite loop. You should be checking for an exit condition, set on when the service gets the shutdown message. You can add a timeout to the WAITFOR so that you can check if you should be shutting down. You should also check this on each row processed.
I have used a 2 second sleep for the delay, but doing a 2 second timeout on the WAITFOR would accomplish the same thing.
A service has 30 seconds to shutdown or Windows will consider it stuck.