Skip to content

Commit c7baa10

Browse files
committed
Improvements in work with streams
1 parent e5c0e26 commit c7baa10

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

RabbitMQ/InboundAdapter.cls

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Property BodyClass As %Dictionary.CacheClassname;
66

77
Parameter SETTINGS = "BodyClass:Basic";
88

9-
/// Establish gateway connectionand init java API
9+
/// Establish gateway connection and init java API.
1010
Method OnInit() As %Status
1111
{
1212
Set sc = $$$OK
@@ -18,28 +18,31 @@ Method OnInit() As %Status
1818
Quit sc
1919
}
2020

21-
/// Close connection
21+
/// Close connection.
2222
Method OnTearDown() As %Status
2323
{
2424
Do ..API.close()
2525
Quit $$$OK
2626
}
2727

28-
/// default InboundAdapter behavior: always call ProcessInput on CallInterval
28+
/// Get Messages from RabbitMQ queue.
2929
Method OnTask() As %Status
3030
{
3131
Set sc = $$$OK
3232

3333
Set messageCount = 1
3434

3535
While messageCount > 0 {
36+
// List containing metainformation and possibly body (in the case of string interaction) of the RabbitMQ message
3637
#Dim messageList As %ListOfDataTypes
3738

3839
If ..BodyClass = "" {
3940
Set messageList = ..API.readMessageString()
4041
} Else {
41-
Set tempStream = ..GetTempStream()
42-
Set messageList = ..API.readMessageStream(.tempStream)
42+
#Dim tempStream As %Library.GlobalBinaryStream
43+
Set messageList = ##class(%ListOfDataTypes).%New()
44+
For i=1:1:15 Do messageList.Insert("")
45+
Set tempStream = ..API.readMessageStream(.messageList)
4346
}
4447

4548
Set messageLength = messageList.GetAt(1)
@@ -49,11 +52,13 @@ Method OnTask() As %Status
4952
#Dim message As RabbitMQ.Message
5053
Set message = ..ListToMessage(messageList)
5154
If ..BodyClass = "" {
52-
Set message.Body = ..DecodeMessageBody(messageList.GetAt(16))
55+
Set message.BodyString = ..DecodeMessageBody(messageList.GetAt(16))
5356
} Else {
54-
Set message.Body = $classmethod(..BodyClass, "%New")
55-
Do message.Body.Write(..DecodeMessageBody(tempStream.Read(messageLength)))
56-
Do message.Body.Rewind()
57+
Set message.BodyStream = $classmethod(..BodyClass, "%New")
58+
While 'tempStream.AtEnd {
59+
Do message.BodyStream.Write(..DecodeMessageBody(tempStream.Read($$$MaxStringLength)))
60+
}
61+
Do message.BodyStream.Rewind()
5762
}
5863
Set sc = ..BusinessHost.ProcessInput(message)
5964
} Else {
@@ -65,6 +70,7 @@ Method OnTask() As %Status
6570
Quit sc
6671
}
6772

73+
/// Convert list containing metainformation into RabbitMQ message
6874
ClassMethod ListToMessage(list As %ListOfDataTypes) As RabbitMQ.Message
6975
{
7076
Set message = ##class(RabbitMQ.Message).%New()
@@ -86,27 +92,12 @@ ClassMethod ListToMessage(list As %ListOfDataTypes) As RabbitMQ.Message
8692
Quit message
8793
}
8894

95+
/// Decode message body. May be full body or only a piece.
8996
Method DecodeMessageBody(body As %String) As %String
9097
{
91-
If ..Encoding '= "" {
92-
If $isObject(body) {
93-
// TODO streams
94-
} Else {
95-
Set body = $zcvt(body, "O", ..Encoding)
96-
}
97-
}
98+
Set:..Encoding'="" body = $zcvt(body, "O", ..Encoding)
9899
Quit body
99100
}
100101

101-
ClassMethod GetTempStream() As %GlobalBinaryStream
102-
{
103-
Set stream=##class(%GlobalBinaryStream).%New()
104-
// TODO - work around that
105-
// we need to 'reserve' a number of bytes since we are passing the stream
106-
// by reference (Java's equivalent is byte[] ba = new byte[max];)
107-
For i=1:1:32000 Do stream.Write("0")
108-
Quit stream
109-
}
110-
111102
}
112103

RabbitMQ/Message.cls

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,18 @@ Property Priority As %String;
2828
Property Timestamp As %String;
2929

3030
/// Could be either string or stream
31-
Property Body As %String;
31+
Property Body(MAXLEN = "") [ Transient ];
32+
33+
Method BodyGet() [ CodeMode = expression ]
34+
{
35+
$select(..BodyString'="":..BodyString, 1:..BodyStream)
36+
}
37+
38+
/// Body if it's a string
39+
Property BodyString As %String(MAXLEN = "");
40+
41+
/// Body if it's a stream
42+
Property BodyStream As %Stream.GlobalCharacter;
3243

3344
Storage Default
3445
{
@@ -78,6 +89,12 @@ Storage Default
7889
<Value name="15">
7990
<Value>Body</Value>
8091
</Value>
92+
<Value name="16">
93+
<Value>BodyString</Value>
94+
</Value>
95+
<Value name="17">
96+
<Value>BodyStream</Value>
97+
</Value>
8198
</Data>
8299
<DataLocation>^RabbitMQ.MessageD</DataLocation>
83100
<DefaultData>MessageDefaultData</DefaultData>

RabbitMQ/Operation.cls

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Method OnMessage(request As Ens.StringRequest, response As Ens.Response) As %Sta
99
{
1010
#Dim sc As %Status = $$$OK
1111
Set response = ##class(Ens.Response).%New()
12-
quit ..Adapter.SendMessage(request.StringValue)
12+
Set sc = ..Adapter.SendMessage(request.StringValue)
13+
Quit sc
1314
}
1415

1516
}

0 commit comments

Comments
 (0)