Wiki source code of Code-Snippets

Last modified by Jannis Klein on 2024/08/13 07:44

Show last authors
1 {{aagon.floatingbox/}}
2
3 (% class="wikigeneratedid" %)
4 Bei den nachfolgenden Essential Code Snippets handelt es sich um vorgefertige Code-Einheiten, die Sie für die angegebenen, konkrete Anwendungsfälle im Skript-Editor verwenden können.
5
6 = Verwendete globale Variablen =
7
8 {{code language="PowerShell" layout="LINENUMBERS"}}
9 $global:targetVR = "" # Target Virtual Router
10 $global:targetRK = "" # Target Routkingkey
11 $global:interval = 0 # Time in seconds between Messages
12
13 $global:backgroundJob = $null # Instance for the background job
14 {{/code}}
15
16 = Start Background Job =
17
18
19 {{code language="PowerShell" layout="LINENUMBERS"}}
20 #
21 # @brief: This functions defines the actual background job.
22 #
23 function global:StartJob {
24
25 Write-LogMessage -Message "Starting Background Job"
26 $global:backgroundJob = Start-CustomBackgroundJob -Arguments $global:targetVR,$global:targetRK,$global:interval -ScriptBlock {
27
28 $vr = $args[0]
29 $rk = $args[1]
30 $interval = $args[2]
31
32 while ($true)
33 {
34 Start-Sleep $interval
35 Write-LogMessage -Message "Sending Background job Message"
36 WriteBusinessLog-Message -Code 0 -Message "Sending Background job Message" -LogType Info
37 Publish-Message -VirtualRouter $vr -RoutingKey $rk -Message "Hello World from Background Job" -Tags "ICQL"
38 }
39 }
40 }
41 {{/code}}
42
43 = Stop Background Job =
44
45 {{code language="PowerShell" layout="LINENUMBERS"}}
46 #
47 # @brief: This functions is used to stop the background job.
48 #
49 function global:StopJob {
50
51 if ($null -ne $global:backgroundJob)
52 {
53 Stop-CustomBackgroundJob -Job $global:backgroundJob
54 $global:backgroundJob = $null;
55 }
56 }
57 {{/code}}
58
59 = Restart Background Job =
60
61 {{code language="PowerShell" layout="LINENUMBERS"}}
62 #
63 # @brief: This functions restarts the background job.
64 #
65 function global:RestartJobIfRunning {
66
67 if ($null -ne $global:backgroundJob)
68 {
69 Stop-CustomBackgroundJob -Job $global:backgroundJob
70 global:StartJob
71 }
72 }
73 {{/code}}
74
75 = OnStartUp =
76
77 {{code language="PowerShell" layout="LINENUMBERS"}}
78 #
79 # @mandatory: Yes
80 # @brief: This event gets fired on StartUp.
81 #
82 Register-EngineEvent -SourceIdentifier 'MicroService.OnStart' -Action {
83
84 Write-LogMessage -Message "Starting.. HelloWorld"
85 Write-LogMessage -Message "# $global:targetVR ## $global:targetRK ## $global:interval #"
86
87 WriteBusinessLog-Message -Code 0 -Message "Starting.. HelloWorld" -LogType Info
88
89 global:StartJob
90 }
91 {{/code}}
92
93 = OnShutDown =
94
95 {{code language="PowerShell" layout="LINENUMBERS"}}
96 #
97 # @mandatory: Yes
98 # @brief: This event gets fired on Shut down
99 #
100 Register-EngineEvent -SourceIdentifier 'MicroService.OnStop' -Action {
101
102 Write-LogMessage -Message "Stopping.. HelloWorld"
103
104 WriteBusinessLog-Message -Code 0 -Message "Stopping.. HelloWorld" -LogType Info
105
106 global:StopJob
107 }
108 {{/code}}
109
110 = OnMessage =
111
112 {{code language="PowerShell" layout="LINENUMBERS"}}
113 #
114 # @mandatory: No
115 # @brief: This event is optional and gets fired when the script needs to be able to listen for messages.
116 #
117 Register-EngineEvent -SourceIdentifier 'MicroService.OnMessage' -Action {
118
119 [string]$messageBodyString = [System.Text.Encoding]::UTF8.GetString($args[0].Body)
120
121 Write-LogMessage -Message "Received Message: $messageBodyString"
122
123 WriteBusinessLog-Message -Code 0 -Message "Received Message: $messageBodyString" -LogType Info
124 }
125 {{/code}}
126
127 = OnConfigChanged =
128
129 {{code language="PowerShell" layout="LINENUMBERS"}}
130 #
131 # @mandatory: No
132 # @brief: This event is used when the script needs parameter defined in a config.
133 #
134 Register-EngineEvent -SourceIdentifier 'MicroService.OnConfigChanged' -Action {
135
136 $config = $args[0]
137
138 Write-LogMessage -Message "Received config: $config"
139
140 $global:targetVR = (Select-Xml -Content $config -XPath "/HelloWorldConfig/TargetVR").Node.InnerText
141 $global:targetRK = (Select-Xml -Content $config -XPath "/HelloWorldConfig/TargetRK").Node.InnerText
142 $global:interval = [int](Select-Xml -Content $config -XPath "/HelloWorldConfig/Interval").Node.InnerText
143
144 Write-LogMessage -Message "Parsed values: # $global:targetVR ## $global:targetRK ## $global:interval #"
145
146 global:RestartJobIfRunning
147 }
148 {{/code}}
© Aagon GmbH 2025
Besuchen Sie unsere neue Aagon-Community