generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
66 lines (53 loc) · 1.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
/*
Package main shows how to listen for all session events on a NamedMap or NamedCache.
*/
package main
import (
"context"
"fmt"
"github.com/oracle/coherence-go-client/v2/coherence"
"time"
)
type Person struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}
func (p Person) String() string {
return fmt.Sprintf("Person{id=%d, name=%s, age=%d}", p.ID, p.Name, p.Age)
}
func main() {
ctx := context.Background()
// create a new Session
session, err := coherence.NewSession(ctx, coherence.WithPlainText())
if err != nil {
panic(err)
}
// create a new NamedMap of Person with key int
namedMap, err := coherence.GetNamedMap[int, Person](session, "people")
if err != nil {
panic(err)
}
// clear the Map
if err = namedMap.Clear(ctx); err != nil {
panic(err)
}
// Create a listener to listen for session events
listener := coherence.NewSessionLifecycleListener().
OnAny(func(e coherence.SessionLifecycleEvent) {
fmt.Printf("**EVENT=%s: source=%v\n", e.Type(), e.Source())
})
session.AddSessionLifecycleListener(listener)
defer session.RemoveSessionLifecycleListener(listener)
session.Close()
sleep("Sleeping to ensure we see events")
}
func sleep(msg string) {
fmt.Println(msg)
time.Sleep(time.Duration(5) * time.Second)
}