Code-Snippets
Zuletzt geändert von Jannis Klein am 2024/08/13 07:44
Inhalt
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.
Verwendete globale Variablen
1
2
3
4
5
2
3
4
5
$global:targetVR = "" # Target Virtual Router
$global:targetRK = "" # Target Routkingkey
$global:interval = 0 # Time in seconds between Messages
$global:backgroundJob = $null # Instance for the background job
$global:targetRK = "" # Target Routkingkey
$global:interval = 0 # Time in seconds between Messages
$global:backgroundJob = $null # Instance for the background job
Start Background Job
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#
# @brief: This functions defines the actual background job.
#
function global:StartJob {
Write-LogMessage -Message "Starting Background Job"
$global:backgroundJob = Start-CustomBackgroundJob -Arguments $global:targetVR,$global:targetRK,$global:interval -ScriptBlock {
$vr = $args[0]
$rk = $args[1]
$interval = $args[2]
while ($true)
{
Start-Sleep $interval
Write-LogMessage -Message "Sending Background job Message"
WriteBusinessLog-Message -Code 0 -Message "Sending Background job Message" -LogType Info
Publish-Message -VirtualRouter $vr -RoutingKey $rk -Message "Hello World from Background Job" -Tags "ICQL"
}
}
}
# @brief: This functions defines the actual background job.
#
function global:StartJob {
Write-LogMessage -Message "Starting Background Job"
$global:backgroundJob = Start-CustomBackgroundJob -Arguments $global:targetVR,$global:targetRK,$global:interval -ScriptBlock {
$vr = $args[0]
$rk = $args[1]
$interval = $args[2]
while ($true)
{
Start-Sleep $interval
Write-LogMessage -Message "Sending Background job Message"
WriteBusinessLog-Message -Code 0 -Message "Sending Background job Message" -LogType Info
Publish-Message -VirtualRouter $vr -RoutingKey $rk -Message "Hello World from Background Job" -Tags "ICQL"
}
}
}
Stop Background Job
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
#
# @brief: This functions is used to stop the background job.
#
function global:StopJob {
if ($null -ne $global:backgroundJob)
{
Stop-CustomBackgroundJob -Job $global:backgroundJob
$global:backgroundJob = $null;
}
}
# @brief: This functions is used to stop the background job.
#
function global:StopJob {
if ($null -ne $global:backgroundJob)
{
Stop-CustomBackgroundJob -Job $global:backgroundJob
$global:backgroundJob = $null;
}
}
Restart Background Job
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
#
# @brief: This functions restarts the background job.
#
function global:RestartJobIfRunning {
if ($null -ne $global:backgroundJob)
{
Stop-CustomBackgroundJob -Job $global:backgroundJob
global:StartJob
}
}
# @brief: This functions restarts the background job.
#
function global:RestartJobIfRunning {
if ($null -ne $global:backgroundJob)
{
Stop-CustomBackgroundJob -Job $global:backgroundJob
global:StartJob
}
}
OnStartUp
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
#
# @mandatory: Yes
# @brief: This event gets fired on StartUp.
#
Register-EngineEvent -SourceIdentifier 'MicroService.OnStart' -Action {
Write-LogMessage -Message "Starting.. HelloWorld"
Write-LogMessage -Message "# $global:targetVR ## $global:targetRK ## $global:interval #"
WriteBusinessLog-Message -Code 0 -Message "Starting.. HelloWorld" -LogType Info
global:StartJob
}
# @mandatory: Yes
# @brief: This event gets fired on StartUp.
#
Register-EngineEvent -SourceIdentifier 'MicroService.OnStart' -Action {
Write-LogMessage -Message "Starting.. HelloWorld"
Write-LogMessage -Message "# $global:targetVR ## $global:targetRK ## $global:interval #"
WriteBusinessLog-Message -Code 0 -Message "Starting.. HelloWorld" -LogType Info
global:StartJob
}
OnShutDown
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
#
# @mandatory: Yes
# @brief: This event gets fired on Shut down
#
Register-EngineEvent -SourceIdentifier 'MicroService.OnStop' -Action {
Write-LogMessage -Message "Stopping.. HelloWorld"
WriteBusinessLog-Message -Code 0 -Message "Stopping.. HelloWorld" -LogType Info
global:StopJob
}
# @mandatory: Yes
# @brief: This event gets fired on Shut down
#
Register-EngineEvent -SourceIdentifier 'MicroService.OnStop' -Action {
Write-LogMessage -Message "Stopping.. HelloWorld"
WriteBusinessLog-Message -Code 0 -Message "Stopping.. HelloWorld" -LogType Info
global:StopJob
}
OnMessage
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
#
# @mandatory: No
# @brief: This event is optional and gets fired when the script needs to be able to listen for messages.
#
Register-EngineEvent -SourceIdentifier 'MicroService.OnMessage' -Action {
[string]$messageBodyString = [System.Text.Encoding]::UTF8.GetString($args[0].Body)
Write-LogMessage -Message "Received Message: $messageBodyString"
WriteBusinessLog-Message -Code 0 -Message "Received Message: $messageBodyString" -LogType Info
}
# @mandatory: No
# @brief: This event is optional and gets fired when the script needs to be able to listen for messages.
#
Register-EngineEvent -SourceIdentifier 'MicroService.OnMessage' -Action {
[string]$messageBodyString = [System.Text.Encoding]::UTF8.GetString($args[0].Body)
Write-LogMessage -Message "Received Message: $messageBodyString"
WriteBusinessLog-Message -Code 0 -Message "Received Message: $messageBodyString" -LogType Info
}
OnConfigChanged
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#
# @mandatory: No
# @brief: This event is used when the script needs parameter defined in a config.
#
Register-EngineEvent -SourceIdentifier 'MicroService.OnConfigChanged' -Action {
$config = $args[0]
Write-LogMessage -Message "Received config: $config"
$global:targetVR = (Select-Xml -Content $config -XPath "/HelloWorldConfig/TargetVR").Node.InnerText
$global:targetRK = (Select-Xml -Content $config -XPath "/HelloWorldConfig/TargetRK").Node.InnerText
$global:interval = [int](Select-Xml -Content $config -XPath "/HelloWorldConfig/Interval").Node.InnerText
Write-LogMessage -Message "Parsed values: # $global:targetVR ## $global:targetRK ## $global:interval #"
global:RestartJobIfRunning
}
# @mandatory: No
# @brief: This event is used when the script needs parameter defined in a config.
#
Register-EngineEvent -SourceIdentifier 'MicroService.OnConfigChanged' -Action {
$config = $args[0]
Write-LogMessage -Message "Received config: $config"
$global:targetVR = (Select-Xml -Content $config -XPath "/HelloWorldConfig/TargetVR").Node.InnerText
$global:targetRK = (Select-Xml -Content $config -XPath "/HelloWorldConfig/TargetRK").Node.InnerText
$global:interval = [int](Select-Xml -Content $config -XPath "/HelloWorldConfig/Interval").Node.InnerText
Write-LogMessage -Message "Parsed values: # $global:targetVR ## $global:targetRK ## $global:interval #"
global:RestartJobIfRunning
}