generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
77 lines (66 loc) · 1.62 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
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright (c) 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 use a dequeue or double ended queue.
*/
package main
import (
"context"
"fmt"
"github.com/oracle/coherence-go-client/v2/coherence"
"log"
)
func main() {
var (
value *string
ctx = context.Background()
)
const iterations = 10
// create a new Session to the default gRPC port of 1408 using plain text
session, err := coherence.NewSession(ctx, coherence.WithPlainText())
if err != nil {
panic(err)
}
defer session.Close()
namedQueue, err := coherence.GetNamedDeQueue[string](ctx, session, "double-ended-queue")
if err != nil {
panic(err)
}
// add 10 entries to the end (tail) of the queue
for i := 1; i <= iterations; i++ {
v := fmt.Sprintf("value-%v", i)
log.Printf("OfferTail() %s to the queue\n", v)
err = namedQueue.OfferTail(ctx, v)
if err != nil {
panic(err)
}
}
// Offer a value to the head
err = namedQueue.OfferHead(ctx, "value-head")
if err != nil {
panic(err)
}
// peek the tail of the queue
value, err = namedQueue.PeekTail(ctx)
if err != nil {
panic(err)
}
log.Printf("PeekTail() returned: %s\n", *value)
// poll for iterations +1 because we added another entry to the head
for i := 1; i <= iterations+1; i++ {
value, err = namedQueue.PollHead(ctx)
if err != nil {
panic(err)
}
log.Printf("PollHead() returned: %s\n", *value)
}
// try to read again should get nil
value, err = namedQueue.PollHead(ctx)
if err != nil {
panic(err)
}
log.Println("last value is", value)
}