|
| 1 | +/// Various test methods |
| 2 | +Class RabbitMQ.Utils |
| 3 | +{ |
| 4 | + |
| 5 | +Parameter CLASS = "isc.rabbitmq.API"; |
| 6 | + |
| 7 | +Parameter CLASSPATH = "C:\InterSystems\RabbitMQjava.jar"; |
| 8 | + |
| 9 | +Parameter BUILDCLASSPATH = "D:\Cache\RabbitMQ\java\out\artifacts\RabbitMQjava_jar\RabbitMQjava.jar"; |
| 10 | + |
| 11 | +Parameter GATEWAY = "RabbitMQ"; |
| 12 | + |
| 13 | +Parameter QUEUE = "Test"; |
| 14 | + |
| 15 | +/// Load Jar from path. |
| 16 | +/// Write $System.Status.GetErrorText(##class(RabbitMQ.Utils).UpdateJar()) |
| 17 | +ClassMethod UpdateJar(gatewayName = {..#GATEWAY}, path As %String = {..#CLASSPATH}) |
| 18 | +{ |
| 19 | + #Dim sc As %Status = $$$OK |
| 20 | + Set sc = ##class(Ens.Director).StopProduction(, 1) |
| 21 | + Quit:$$$ISERR(sc) sc |
| 22 | + |
| 23 | + Set sc = ##class(%Net.Remote.Service).StopGateway(gatewayName) |
| 24 | + |
| 25 | + Set:$system["ed-pc" sc = ##class(%File).CopyFile(..#BUILDCLASSPATH,..#CLASSPATH, $$$YES, .result) |
| 26 | + Quit:sc'=1 $$$ERROR($$$GeneralError, $$$FormatText("File copy failed with error: %1", result)) |
| 27 | + |
| 28 | + Set gateway = ..Connect(gatewayName, path, .sc) |
| 29 | + Quit:$$$ISERR(sc) sc |
| 30 | + |
| 31 | + Set sc = gateway.%Import(..#CLASS) |
| 32 | + Quit:$$$ISERR(sc) sc |
| 33 | + Set:'##class(%Dictionary.CompiledClass).%ExistsId(..#CLASS) sc = $$$ERROR($$$GeneralError, $$$FormatText("Class '%1' does not exist",..#CLASS)) |
| 34 | + Quit:$$$ISERR(sc) sc |
| 35 | + |
| 36 | + Set sc = ##class(%Net.Remote.Service).StopGateway(gatewayName) |
| 37 | + |
| 38 | + Set sc = ##class(Ens.Director).StartProduction() |
| 39 | + Quit sc |
| 40 | +} |
| 41 | + |
| 42 | +/// Read one message. |
| 43 | +/// Write $System.Status.GetErrorText(##class(RabbitMQ.Utils).ReadMsg()) |
| 44 | +ClassMethod ReadMsg(pMsgLen = 32000) As %Status |
| 45 | +{ |
| 46 | + #Dim gateway as %Net.Remote.Gateway |
| 47 | + #Dim exception as %Exception.AbstractException |
| 48 | + |
| 49 | + Set sc = $$$OK |
| 50 | + Try { |
| 51 | + |
| 52 | + Set gateway = ..Connect() |
| 53 | + #Dim api As isc.rabbitmq.API |
| 54 | + Set api = ..GetAPI(gateway) |
| 55 | + |
| 56 | + Set list = ##class(%ListOfDataTypes).%New() |
| 57 | + For i=1:1:15 Do list.Insert("") |
| 58 | + |
| 59 | + #Dim stream As %Stream.Object |
| 60 | + Set stream = api.readMessageStream(.list) |
| 61 | + Write !,"Body: " |
| 62 | + Do stream.OutputToDevice() |
| 63 | + Write ! |
| 64 | + |
| 65 | + Zw list |
| 66 | + |
| 67 | + Set sc= gateway.%Disconnect() |
| 68 | + } Catch ex { |
| 69 | + Set sc = $$$ADDSC(ex.AsStatus(), $g(%objlasterror)) |
| 70 | + } |
| 71 | + |
| 72 | + Quit sc |
| 73 | +} |
| 74 | + |
| 75 | +/// Send one message. |
| 76 | +/// Write $System.Status.GetErrorText(##class(RabbitMQ.Utils).SendMsg()) |
| 77 | +ClassMethod SendMsg(msg = "356") As %Status |
| 78 | +{ |
| 79 | + #dim gateway as %Net.Remote.Gateway |
| 80 | + #dim exception as %Exception.AbstractException |
| 81 | + |
| 82 | + Set sc = $$$OK |
| 83 | + Try { |
| 84 | + |
| 85 | + Set gateway = ..Connect() |
| 86 | + #Dim api As isc.rabbitmq.API |
| 87 | + Set api = ..GetAPI(gateway) |
| 88 | + |
| 89 | + Set stream = ##class(%GlobalBinaryStream).%New() |
| 90 | + Do stream.Write(msg) |
| 91 | + |
| 92 | + Do api.sendMessage(stream, "correlationId", "message " _ $zdt($zts,3,1,3)) |
| 93 | + |
| 94 | + |
| 95 | + Set sc= gateway.%Disconnect() |
| 96 | + |
| 97 | + } Catch ex { |
| 98 | + Set sc = $$$ADDSC(ex.AsStatus(), $g(%objlasterror)) |
| 99 | + } |
| 100 | + |
| 101 | + Quit sc |
| 102 | +} |
| 103 | + |
| 104 | +/// Get JGW object |
| 105 | +ClassMethod Connect(gatewayName As %String = {..#GATEWAY}, path As %String = {..#CLASSPATH}, Output sc As %Status) As %Net.Remote.Gateway |
| 106 | +{ |
| 107 | + Set gateway = "" |
| 108 | + Set sc = ##class(%Net.Remote.Service).OpenGateway(gatewayName, .gatewayConfig) |
| 109 | + Quit:$$$ISERR(sc) gateway |
| 110 | + Set sc = ##class(%Net.Remote.Service).ConnectGateway(gatewayConfig, .gateway, path, $$$YES) |
| 111 | + Quit gateway |
| 112 | +} |
| 113 | + |
| 114 | +/// Construct RabbitMQ API object. |
| 115 | +ClassMethod GetAPI(gateway As %Net.Remote.Gateway) As isc.rabbitmq.API |
| 116 | +{ |
| 117 | + Set host = "localhost" |
| 118 | + Set port = -1 |
| 119 | + Set user = "guest" |
| 120 | + Set pass = "guest" |
| 121 | + Set virtualHost = "/" |
| 122 | + Set queue = ..#QUEUE |
| 123 | + Set durable = $$$YES |
| 124 | + |
| 125 | + Set api = ##class(isc.rabbitmq.API).%New(gateway, host, port, user, pass, virtualHost, queue, durable) |
| 126 | + Quit api |
| 127 | +} |
| 128 | + |
| 129 | +} |
| 130 | + |
0 commit comments