-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutgoingObjects.tt
148 lines (125 loc) · 3.79 KB
/
OutgoingObjects.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".sql" #>
<#@ SqlModelDirective processor="SqlModelDirectiveProcessor" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
/*
*
* For information about these objects, see: *.tt file.
*
*/
<#
List<String> objects = GetObjects();
foreach (var obj in objects)
{
#>
GO
/* ServiceBroker -> ReadIncomingQueue */
CREATE PROCEDURE [SB].[ReadOutgoingQueue_<#= obj #>]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @conversation_handle UNIQUEIDENTIFIER,
@service_name SYSNAME,
@service_contract_name SYSNAME,
@message_body XML,
@message_type_name SYSNAME,
@test_message_type_name sysname = '//ANY/ANY/Test_MessageType',
@real_message_type_name sysname = '//<#= obj.Replace("_", "/") #>_MessageType';
WHILE ( 1 = 1 )
BEGIN
BEGIN TRANSACTION;
BEGIN TRY
WAITFOR (
RECEIVE TOP (1)
@conversation_handle = [conversation_handle],
@message_body = CAST([message_body] AS XML),
@message_type_name = [message_type_name],
@service_name = [service_name],
@service_contract_name = [service_contract_name]
FROM [sb].[OutgoingQueue_<#= obj #>]
), TIMEOUT 1000;
IF (@@ROWCOUNT = 0 AND @@TRANCOUNT > 0)
BEGIN
ROLLBACK TRANSACTION;
BREAK;
END;
/*
-- Throw exception to test how error handling working.
RAISERROR ('Testing error handling...', 16, 1 );
*/
IF @message_type_name = N'http://schemas.microsoft.com/SQL/ServiceBroker/Error'
BEGIN
-- Receiver initiate "end conversation with error". Log the error and finish conversation as well.
EXEC [SB].[SendEmailWithServiceBrokerMessageDetails]
'Error message received',
@service_name,
@service_contract_name,
@message_type_name,
@message_body;
END;
END CONVERSATION @conversation_handle;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
/*
Test XACT_STATE for 0, 1, or -1.
If = -1 The transaction is uncommittable and should be rolled back.
If = 1 The transaction is committable.
If = 0 There is no active user transaction for the current request.
*/
DECLARE @error_number INT = ERROR_NUMBER(), @error_message NVARCHAR(MAX) = ERROR_MESSAGE();
IF ( XACT_STATE() ) = -1
BEGIN
ROLLBACK TRANSACTION;
END;
IF ( XACT_STATE() ) = 1
BEGIN
-- OptionA: Rollback transaction & send email if error happens during processing.
-- Message will stay in incoming queue. If rollback happens 5 times in a row the incoming queue will be disabled.
ROLLBACK TRANSACTION;
EXEC [SB].[SendEmailWithServiceBrokerMessageDetails]
'Message cannot be processed',
@service_name,
@service_contract_name,
@message_type_name,
@message_body,
@error_number,
@error_message;
/*
-- OptionB: Notify sender that message cannot be processed.
-- Conversation will be closed and sender should decide what to do with message (send again or nothing).
END CONVERSATION @conversation_handle WITH ERROR = @error_number DESCRIPTION = @error_message;
COMMIT;
*/
END;
BREAK;
END CATCH;
END;
END;
GO
CREATE QUEUE [SB].[OutgoingQueue_<#= obj #>]
WITH STATUS = ON,
ACTIVATION (
STATUS = ON,
PROCEDURE_NAME = [SB].[ReadOutgoingQueue_<#= obj #>],
MAX_QUEUE_READERS = 1,
EXECUTE AS N'dbo'
),
POISON_MESSAGE_HANDLING(STATUS = ON);
GO
CREATE SERVICE [//<#= obj.Replace("_", "/") #>_OutgoingService] AUTHORIZATION [dbo]
ON QUEUE [SB].[OutgoingQueue_<#= obj #>] ([//<#= obj.Replace("_", "/") #>_Contract]);
<#
}
#>
<#+
public List<String> GetObjects()
{
List<String> objects = new List<String>();
// Put message name here
// examlpe: SRC_DEST_MSGTYPE
objects.Add("Source_Destination_MessageType");
return objects;
}
#>