29
29
import org .apache .pulsar .client .api .PulsarClientException ;
30
30
import org .apache .pulsar .client .api .Schema ;
31
31
import org .apache .pulsar .client .api .TableView ;
32
+ import org .apache .pulsar .common .util .FutureUtil ;
32
33
33
34
/**
34
35
* The load data store, base on {@link TableView <T>}.
@@ -58,54 +59,60 @@ public TableViewLoadDataStoreImpl(PulsarClient client, String topic, Class<T> cl
58
59
}
59
60
60
61
@ Override
61
- public CompletableFuture <Void > pushAsync (String key , T loadData ) {
62
+ public synchronized CompletableFuture <Void > pushAsync (String key , T loadData ) {
63
+ if (producer == null ) {
64
+ return FutureUtil .failedFuture (new IllegalStateException ("producer has not been started" ));
65
+ }
62
66
return producer .newMessage ().key (key ).value (loadData ).sendAsync ().thenAccept (__ -> {});
63
67
}
64
68
65
69
@ Override
66
- public CompletableFuture <Void > removeAsync (String key ) {
70
+ public synchronized CompletableFuture <Void > removeAsync (String key ) {
71
+ if (producer == null ) {
72
+ return FutureUtil .failedFuture (new IllegalStateException ("producer has not been started" ));
73
+ }
67
74
return producer .newMessage ().key (key ).value (null ).sendAsync ().thenAccept (__ -> {});
68
75
}
69
76
70
77
@ Override
71
- public Optional <T > get (String key ) {
78
+ public synchronized Optional <T > get (String key ) {
72
79
validateTableViewStart ();
73
80
return Optional .ofNullable (tableView .get (key ));
74
81
}
75
82
76
83
@ Override
77
- public void forEach (BiConsumer <String , T > action ) {
84
+ public synchronized void forEach (BiConsumer <String , T > action ) {
78
85
validateTableViewStart ();
79
86
tableView .forEach (action );
80
87
}
81
88
82
- public Set <Map .Entry <String , T >> entrySet () {
89
+ public synchronized Set <Map .Entry <String , T >> entrySet () {
83
90
validateTableViewStart ();
84
91
return tableView .entrySet ();
85
92
}
86
93
87
94
@ Override
88
- public int size () {
95
+ public synchronized int size () {
89
96
validateTableViewStart ();
90
97
return tableView .size ();
91
98
}
92
99
93
100
@ Override
94
- public void closeTableView () throws IOException {
101
+ public synchronized void closeTableView () throws IOException {
95
102
if (tableView != null ) {
96
103
tableView .close ();
97
104
tableView = null ;
98
105
}
99
106
}
100
107
101
108
@ Override
102
- public void start () throws LoadDataStoreException {
109
+ public synchronized void start () throws LoadDataStoreException {
103
110
startProducer ();
104
111
startTableView ();
105
112
}
106
113
107
114
@ Override
108
- public void startTableView () throws LoadDataStoreException {
115
+ public synchronized void startTableView () throws LoadDataStoreException {
109
116
if (tableView == null ) {
110
117
try {
111
118
tableView = client .newTableViewBuilder (Schema .JSON (clazz )).topic (topic ).create ();
@@ -117,7 +124,7 @@ public void startTableView() throws LoadDataStoreException {
117
124
}
118
125
119
126
@ Override
120
- public void startProducer () throws LoadDataStoreException {
127
+ public synchronized void startProducer () throws LoadDataStoreException {
121
128
if (producer == null ) {
122
129
try {
123
130
producer = client .newProducer (Schema .JSON (clazz )).topic (topic ).create ();
@@ -129,7 +136,7 @@ public void startProducer() throws LoadDataStoreException {
129
136
}
130
137
131
138
@ Override
132
- public void close () throws IOException {
139
+ public synchronized void close () throws IOException {
133
140
if (producer != null ) {
134
141
producer .close ();
135
142
producer = null ;
@@ -138,15 +145,14 @@ public void close() throws IOException {
138
145
}
139
146
140
147
@ Override
141
- public void init () throws IOException {
148
+ public synchronized void init () throws IOException {
142
149
close ();
143
150
start ();
144
151
}
145
152
146
- private void validateTableViewStart () {
153
+ private synchronized void validateTableViewStart () {
147
154
if (tableView == null ) {
148
155
throw new IllegalStateException ("table view has not been started" );
149
156
}
150
157
}
151
-
152
158
}
0 commit comments