Tips for Service broker pattern to prevent concurrency issues - sql-server

A single process in my ASP.NET Website will be fired from different locations at ~ same time.
Trying to use SQL Server Broker to Queue the requests and execute one by one.
Enabled SQL Broker and created a Queue and Service in the database. On receiving a request; I begin a dialog conversation using only the 1 Service and log the conversation token in a table.
Have written an activated procedure to access the data passed into Queue and Initiate processing.
End conversation inside activated procedure after Processing is complete.
I have doubts regarding the pattern I am following. The conversation does not get closed correctly. In some examples; I have seen an Initiator and Target Queue pattern where conversation is closed at both endpoints. Please help me to figure out the pattern needed in this case.
UPDATE
Sorry for not updating; got busy with some other work. I changed to using 2 each of Queues(Initiator and Target), Services and the corresponding Activated procs.
The connections get closed properly now. When we insert into the Target queue while 1st request is being processed; do we need to specify any setting or command to ensure that 1st request gets completed before starting the 2nd one?

Related

Service Broker message stuck, are there any alternative solutions that don't involve reassigning a new broker?

Good afternoon,
I have a production service broker queue which stopped processing messages about a month ago. After clearing out the messages that had backed up there remains 8 messages that will not leave the queue. I assume it is these messages that are blocking me from disabling or deleting the queue which would hopefully allow the whole system to start up again.
If I select from the queue I see the 8 messages and they all have a message_type_name of "http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog".
If I match these messages up with sys.conversation_endpoints I get a state_desc of "DISCONNECTED_INBOUND"
Using ssbdiagnose on a specific conversation handle I receive the following error:
Service broker received an END CONVERSATION message on this conversation. Service Broker will not transmit the message; it will be held until the application ends the conversation.
If I try to end the conversation using:
END CONVERSATION ... WITH CLEANUP;
The query just spins, (currently over 20 min).
I get the impression that my next course of action is the following command:
ALTER DATABASE ... SET NEW_BROKER WITH ROLLBACK IMMEDIATE;
Not a fan of this after reading some of the fallout but if that is the only option then we'll have to deal with it.
So, thoughts on next steps / any other option than the new broker? If additional information is necessary just let me know.
Thanks!

SQL Service Broker, send messages back to initiator

in my scenario, I want to have some services to be fixed (as in not needing to be updated) and as time goes by adding other services. (I'm using one DB instance, but it shouldn't matter in service broker)
I want to set up the fixed ones in a way to be able to send back a message to the initiator of any message in its queue without me changing its logic and procedures every time I add another service.
is it even possible or do I have to add more logic as new services are created?
If I'm understanding your question correctly, this is how Service Broker works by default. Which is to say that a conversation is between two parties (initiator and target). Once that conversation is established, either party can send messages on it and they will go to the other party. So, if you want to send a message back to the initiator, just send a message on the same conversation handle as the message was received on and you should be good to go.

HTTP response following long process

The current project is in Node.js with the Expressjs framework. We have an application with client/prospect information, authenticated users are allowed to modify the database and initiate long-running processes on the server. As an example, printing a 30 pg document could be one process.
The application user needs two main things:
A response when the process begins.
A response (notification) when the process ends.
We currently handle need #1 in standard express fashion by ensuring the process starts followed by res.json({msg: 'Process Started']); back to the Angular front end. Need #2 is currently handed with an email to the user that initiated the process with a process status report.
I would like to improve how we handle need #2. Specifically, I would like to send a JSON string to the user to display in the interface.
Questions:
Is it possible to do this over HTTP?
Does this functionality exist within Express or a well-know middleware.
Assuming 1 & 2 are false. My solution is to run a TCP socket server to maintain a socket with the required users. When a process ends a message is sent to the user with a status update. Can anyone comment on the issues my solution presents?
Yes to both 1 and 2. Essentially what you seek to achieve here is to push from the server to the client. The need to do this is pretty ubiquitous in web applications and there have been various solutions for it over the years with various fancy names. You might like to read up on Ajax, Comet, Long-polling, Websockets.
For your node application, take a look at socket.io. In a nutshell, what this framework does is it abstracts the complexities of Ajax, Websockets, etc. into a single API. Put another way, socket.io gives you bi-directional communications between your node application and front end.

SQL Server Service Broker queue event notification not raised

Environment: SQL Server 2012 Express edition.
Goal: Setup SQL Server Service Broker with external activation, calling a command line app.
What I have done so far: created message types, contracts, initiator and receiver queues and corresponding services. I also did set up a notification queue, notification service and created event notification for the TargetQ. The event notification is configured so that when an event is raised, it should (I thnk) call the NotifySvc letting it know that there is work to be processed in TargetQ. Please feel free to correct me at any point – this is very new to me.
What happens: I have a trigger on a table that (upon insert) creates a message and calls the TargetSvc. The message arrives at the TargetQ happily. And this is where everything stops. I am not sure if the event notification for queue activation is never triggered or what, but the message never makes it to the NotifyQ. Therefore my EA app is never called.
I realize I am skipping a lot of detail around setup and configuration but, as the topic is new to me, I am hoping that you guys see something obvious. Any help is much appreciated.

Apache Camel - Listening to route events like completion of a route

I’m stuck at one particular requirement in camel , I’m processing a set of files and when any exception occurs i send a message to an exception queue , and from the exception queue im sending an email about the failure , the functionality is working fine but I end up sending many mails, like if 10 files fail im sending 10 emails, is there a way to send only one mail , like I would want to wait for the entire route to finish , then go look the exception queue and send a single mail stating what has failed (by processing the exception queue )
I'm open for suggestions.
I had to do this scenario once (inversed though - mail on success). I had a handy MySQL database configured and ready, so I just added each event from the queue to the database. Then once every now and then, extracted all info (and deleted it)- simply select * from events; delete from events; from the database and created a mail.
You could process the error queue with the aggregator pattern, it is very nice for these tasks. http://camel.apache.org/aggregate-example.html . You still need to know WHEN the aggregator should fire off a message. If you can, trigger a "finish, send mail" event such as in the example in the link above.
The most simple way would be to time schedule these mail notifications. Take a look at: http://camel.apache.org/simplescheduledroutepolicy.html . You can set it to run your route for some good choice of time, then when it fires off, you set the aggreator to complete upon timeout, and make the timeout good enough to empty any reasonable queue size of errors, but not too large.
At least that's my suggestions to your issue

Resources