|
| 1 | +Class RabbitMQ.InboundAdapter Extends (Ens.InboundAdapter, RabbitMQ.Common) |
| 2 | +{ |
| 3 | + |
| 4 | +/// Stream class to store message body. Leave empty to use strings. |
| 5 | +Property BodyClass As %Dictionary.CacheClassname; |
| 6 | + |
| 7 | +Parameter SETTINGS = "BodyClass:Basic"; |
| 8 | + |
| 9 | +/// Establish gateway connectionand init java API |
| 10 | +Method OnInit() As %Status |
| 11 | +{ |
| 12 | + Set sc = $$$OK |
| 13 | + Quit:..JGService="" $$$ERROR($$$GeneralError,"Specify JGService setting") |
| 14 | + Quit:'##class(Ens.Director).IsItemEnabled(..JGService) $$$ERROR($$$GeneralError, $$$FormatText("Java Gateway Service: '%1' is down",..JGService)) |
| 15 | + Set sc = ..Connect() |
| 16 | + Quit:$$$ISERR(sc) |
| 17 | + Set sc = ..ConnectToRabbitMQ() |
| 18 | + Quit sc |
| 19 | +} |
| 20 | + |
| 21 | +/// Close connection |
| 22 | +Method OnTearDown() As %Status |
| 23 | +{ |
| 24 | + Do ..API.close() |
| 25 | + Quit $$$OK |
| 26 | +} |
| 27 | + |
| 28 | +/// default InboundAdapter behavior: always call ProcessInput on CallInterval |
| 29 | +Method OnTask() As %Status |
| 30 | +{ |
| 31 | + Set sc = $$$OK |
| 32 | + |
| 33 | + Set messageCount = 1 |
| 34 | + |
| 35 | + While messageCount > 0 { |
| 36 | + #Dim messageList As %ListOfDataTypes |
| 37 | + |
| 38 | + If ..BodyClass = "" { |
| 39 | + Set messageList = ..API.readMessageString() |
| 40 | + } Else { |
| 41 | + Set tempStream = ..GetTempStream() |
| 42 | + Set messageList = ..API.readMessageStream(.tempStream) |
| 43 | + } |
| 44 | + |
| 45 | + Set messageLength = messageList.GetAt(1) |
| 46 | + Set messageCount = messageList.GetAt(2) |
| 47 | + |
| 48 | + If messageLength>0 { |
| 49 | + #Dim message As RabbitMQ.Message |
| 50 | + Set message = ..ListToMessage(messageList) |
| 51 | + If ..BodyClass = "" { |
| 52 | + Set message.Body = ..DecodeMessageBody(messageList.GetAt(16)) |
| 53 | + } Else { |
| 54 | + Set message.Body = $classmethod(..BodyClass, "%New") |
| 55 | + Do message.Body.Write(..DecodeMessageBody(tempStream.Read(messageLength))) |
| 56 | + Do message.Body.Rewind() |
| 57 | + } |
| 58 | + Set sc = ..BusinessHost.ProcessInput(message) |
| 59 | + } Else { |
| 60 | + CONTINUE |
| 61 | + } |
| 62 | + Quit:$$$ISERR(sc) |
| 63 | + } |
| 64 | + Set ..BusinessHost.%WaitForNextCallInterval=1 |
| 65 | + Quit sc |
| 66 | +} |
| 67 | + |
| 68 | +ClassMethod ListToMessage(list As %ListOfDataTypes) As RabbitMQ.Message |
| 69 | +{ |
| 70 | + Set message = ##class(RabbitMQ.Message).%New() |
| 71 | + |
| 72 | + Set message.ContentType = list.GetAt(3) |
| 73 | + Set message.ContentEncoding = list.GetAt(4) |
| 74 | + Set message.CorrelationId = list.GetAt(5) |
| 75 | + Set message.ReplyTo = list.GetAt(6) |
| 76 | + Set message.Expiration = list.GetAt(7) |
| 77 | + Set message.MessageId = list.GetAt(8) |
| 78 | + Set message.Type = list.GetAt(9) |
| 79 | + Set message.UserId = list.GetAt(10) |
| 80 | + Set message.AppId = list.GetAt(11) |
| 81 | + Set message.ClusterId = list.GetAt(12) |
| 82 | + Set message.DeliveryMode = list.GetAt(13) |
| 83 | + Set message.Priority = list.GetAt(14) |
| 84 | + Set message.Timestamp = list.GetAt(15) |
| 85 | + |
| 86 | + Quit message |
| 87 | +} |
| 88 | + |
| 89 | +Method DecodeMessageBody(body As %String) As %String |
| 90 | +{ |
| 91 | + If ..Encoding '= "" { |
| 92 | + If $isObject(body) { |
| 93 | + // TODO streams |
| 94 | + } Else { |
| 95 | + Set body = $zcvt(body, "O", ..Encoding) |
| 96 | + } |
| 97 | + } |
| 98 | + Quit body |
| 99 | +} |
| 100 | + |
| 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 | + |
| 111 | +} |
| 112 | + |
0 commit comments