-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueMessage.bas
More file actions
148 lines (123 loc) · 5.46 KB
/
queueMessage.bas
File metadata and controls
148 lines (123 loc) · 5.46 KB
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
Attribute VB_Name = "queueMessage"
Option Explicit
Option Compare Text
Option Base 0
'============================================================================================================================
'
'
' Author : John Greenan
' Email :
' Company : Alignment Systems Limited
' Date : 28th March 2014
'
' Purpose : Matching Engine in Excel VBA for Alignment Systems Limited
'
' References : Add a reference to C:\windows\system32\winhttp.dll
' References : https://issues.apache.org/jira/browse/AMQ-5579
' References : Winhttp code is cut-and-paste with simplifications from
' : http://www.808.dk/?code-simplewinhttprequest
'
' To replicate: 1. Download this code
' 2. Create a new 64bit Excel 2013 workbook running on Windows 8.1 64 bit
' 3. Save the workbook as a .xlsm workbook with macros enabled
' 4. I run activemq locally so there is clearly no issue with firewalls or anything
' 5. Install ActiveMQ at cd "c:\Program Files (x86)\apache-activemq-5.10.0"
' 6. From a command prompt running as administrator execute cd "c:\Program Files (x86)\apache-activemq-5.10.0"
' 7. From that command prompt execute bin\win64\activemq.bat
' 8. Have a look at C:\Program Files (x86)\apache-activemq-5.10.0\data
' 8. If ActiveMQ has started you'll see a line like this:
' jvm 1 | INFO | Apache ActiveMQ 5.10.0 (localhost, ID:apple-50841-1427852327092-0:1) started
' 9. Run an instance of http://localhost:8161/admin/queues.jsp in a browser on the machine
' 10. In http://localhost:8161/admin/queues.jsp create a queue called TEST.A
' 11. Now, run DoStuffWorking in the VBA code.
' 12. In http://localhost:8161/admin/queues.jsp you will see a message has arrived on the queue
' 13. In the VBA debug window you will see "queueMessage.DoStuffWorking Message sent"
' 14. Now, run DoStuffNotWorking in the VBA code.
' 15. In the VBA debug window you will see "queueMessage.DoStuffNotWorking HTTP 500 STREAMED"
' 16. In http://localhost:8161/admin/queues.jsp you will see a message has not arrived on the queue
' 17. I created a variable called lngFlavourOfWorkAroundToTry. You can see a few workaround I have tried.
' 18. Default for lngFlavourOfWorkAroundToTry is ZERO
'============================================================================================================================
'Constants
Const mstrTargetURL As String = "http://localhost:8161/api/message?destination=queue://TEST.A"
Const mstrPayload = "Hello World"
Dim varReturnFromWinHttp As Variant
'Variables
Function DoStuffWorking() As Boolean
'Constants
Const strMethodName As String = "queueMessage.DoStuffWorking "
varReturnFromWinHttp = GetDataFromURL(mstrTargetURL, "POST", "")
Debug.Print strMethodName & CStr(varReturnFromWinHttp)
End Function
Function DoStuffNotWorking() As Boolean
'Constants
Const strMethodName As String = "queueMessage.DoStuffNotWorking "
varReturnFromWinHttp = GetDataFromURL(mstrTargetURL, "POST", mstrPayload)
Debug.Print strMethodName & CStr(varReturnFromWinHttp)
End Function
Function GetDataFromURL(strURL, strMethod, strPostData)
'Constants
Const strMethodName As String = "queueMessage.GetDataFromURL "
'Variables
Dim lngTimeout
Dim strUserAgentString
Dim intSslErrorIgnoreFlags
Dim blnEnableRedirects
Dim blnEnableHttpsToHttpRedirects
Dim strHostOverride
Dim strLogin
Dim strPassword
Dim strResponseText
Dim objWinHttp As WinHttp.WinHttpRequest
lngTimeout = 1000
strUserAgentString = "http_requester/0.1"
intSslErrorIgnoreFlags = 13056 ' 13056: ignore all err, 0: accept no err
blnEnableRedirects = True
blnEnableHttpsToHttpRedirects = True
strHostOverride = ""
strLogin = "admin"
strPassword = "admin"
Set objWinHttp = New WinHttp.WinHttpRequest
objWinHttp.SetTimeouts lngTimeout, lngTimeout, lngTimeout, lngTimeout
objWinHttp.Open strMethod, strURL
If strMethod = "POST" Then
objWinHttp.SetRequestHeader "Content-type", _
"application/x-www-form-urlencoded"
End If
'If strHostOverride <> "" Then
' objWinHttp.SetRequestHeader "Host", strHostOverride
'End If
objWinHttp.Option(0) = strUserAgentString
objWinHttp.Option(4) = intSslErrorIgnoreFlags
objWinHttp.Option(6) = blnEnableRedirects
objWinHttp.Option(12) = blnEnableHttpsToHttpRedirects
If (strLogin <> "") And (strPassword <> "") Then
objWinHttp.SetCredentials strLogin, strPassword, 0
End If
On Error Resume Next
Const lngFlavourOfWorkAroundToTry As Long = 0
If Len(strPostData) > 0 Then
Select Case lngFlavourOfWorkAroundToTry
Case 0
objWinHttp.Send strPostData
Case 1
objWinHttp.Send ("Body=" & strPostData)
Case 2
objWinHttp.Send "OneWord"
Case Else
End Select
Else
objWinHttp.Send
End If
If Err.Number = 0 Then
If objWinHttp.Status = "200" Then
GetDataFromURL = objWinHttp.ResponseText
Else
GetDataFromURL = "HTTP " & objWinHttp.Status & " " & objWinHttp.StatusText
End If
Else
GetDataFromURL = "Error " & Err.Number & " " & Err.Source & " " & Err.Description
End If
On Error GoTo 0
Set objWinHttp = Nothing
End Function