24
24
import java .util .List ;
25
25
import java .util .Map ;
26
26
import java .util .Set ;
27
- import java .util .concurrent .atomic .AtomicBoolean ;
28
- import java .util .concurrent .atomic .AtomicInteger ;
29
-
30
27
import org .apache .commons .logging .Log ;
31
28
import org .apache .commons .logging .LogFactory ;
32
29
import org .apache .rocketmq .client .consumer .DefaultMQPushConsumer ;
33
30
import org .apache .rocketmq .client .producer .DefaultMQProducer ;
34
- import org .apache .rocketmq .common .MixAll ;
31
+ import org .apache .rocketmq .common .TopicConfig ;
35
32
import org .apache .rocketmq .common .message .Message ;
36
33
import org .apache .rocketmq .common .message .MessageQueue ;
37
- import org .apache .rocketmq .common .protocol .body .TopicList ;
38
34
import org .apache .rocketmq .streams .common .channel .sink .AbstractSupportShuffleSink ;
39
35
import org .apache .rocketmq .streams .common .channel .split .ISplit ;
40
36
import org .apache .rocketmq .streams .common .configurable .annotation .ENVDependence ;
41
37
import org .apache .rocketmq .streams .common .context .IMessage ;
42
38
import org .apache .rocketmq .streams .common .utils .StringUtil ;
43
39
import org .apache .rocketmq .streams .queue .RocketMQMessageQueue ;
44
40
import org .apache .rocketmq .tools .admin .DefaultMQAdminExt ;
41
+ import org .apache .rocketmq .tools .command .CommandUtil ;
45
42
46
43
public class RocketMQSink extends AbstractSupportShuffleSink {
47
44
@@ -51,6 +48,8 @@ public class RocketMQSink extends AbstractSupportShuffleSink {
51
48
52
49
private String topic ;
53
50
private String groupName ;
51
+ private String clusterName = "DefaultCluster" ;
52
+ private boolean order = false ;
54
53
55
54
private transient List <DefaultMQPushConsumer > consumers = new ArrayList <>();
56
55
private transient DefaultMQProducer producer ;
@@ -67,7 +66,11 @@ public RocketMQSink(String namesrvAddr, String topic, String groupName) {
67
66
this .groupName = groupName ;
68
67
}
69
68
70
-
69
+ public RocketMQSink (String namesrvAddr , String topic , String groupName , String clusterName , boolean order ) {
70
+ this .namesrvAddr = namesrvAddr ;
71
+ this .topic = topic ;
72
+ this .groupName = groupName ;
73
+ }
71
74
@ Override
72
75
protected boolean initConfigurable () {
73
76
super .initConfigurable ();
@@ -132,7 +135,6 @@ protected boolean batchInsert(List<IMessage> messages) {
132
135
return true ;
133
136
}
134
137
135
-
136
138
protected void initProducer () {
137
139
if (producer == null ) {
138
140
synchronized (this ) {
@@ -169,7 +171,6 @@ public void destroyProduce() {
169
171
}
170
172
}
171
173
172
-
173
174
@ Override
174
175
public void destroy () {
175
176
super .destroy ();
@@ -183,30 +184,50 @@ public String getShuffleTopicFieldName() {
183
184
184
185
@ Override
185
186
protected void createTopicIfNotExist (int splitNum ) {
187
+ if (StringUtil .isEmpty (topic )) {
188
+ LOG .error ("Topic should be empty" );
189
+ throw new RuntimeException ("Topic should be empty" );
190
+ }
186
191
DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt ();
187
192
defaultMQAdminExt .setVipChannelEnabled (false );
188
193
defaultMQAdminExt .setNamesrvAddr (this .getNamesrvAddr ());
189
194
defaultMQAdminExt .setInstanceName (Long .toString (System .currentTimeMillis ()));
195
+ TopicConfig topicConfig = new TopicConfig ();
196
+ topicConfig .setReadQueueNums (splitNum );
197
+ topicConfig .setWriteQueueNums (splitNum );
198
+ topicConfig .setTopicName (topic .trim ());
199
+
190
200
try {
191
201
defaultMQAdminExt .start ();
192
- TopicList topicList = defaultMQAdminExt . fetchAllTopicList ();
193
- for ( String topic : topicList . getTopicList ()) {
194
- if ( topic . equals ( this . topic ) ) {
195
- return ;
196
- }
202
+ Set < String > masterSet =
203
+ CommandUtil . fetchMasterAddrByClusterName ( defaultMQAdminExt , clusterName );
204
+ for ( String master : masterSet ) {
205
+ defaultMQAdminExt . createAndUpdateTopicConfig ( master , topicConfig ) ;
206
+ LOG . info ( "Create topic to success: " + master );
197
207
}
198
208
199
-
200
- defaultMQAdminExt .createTopic (MixAll .AUTO_CREATE_TOPIC_KEY_TOPIC , topic , splitNum , 1 );
209
+ if (this .order ) {
210
+ Set <String > brokerNameSet =
211
+ CommandUtil .fetchBrokerNameByClusterName (defaultMQAdminExt , clusterName );
212
+ StringBuilder orderConf = new StringBuilder ();
213
+ String splitor = "" ;
214
+ for (String s : brokerNameSet ) {
215
+ orderConf .append (splitor ).append (s ).append (":" )
216
+ .append (topicConfig .getWriteQueueNums ());
217
+ splitor = ";" ;
218
+ }
219
+ defaultMQAdminExt .createOrUpdateOrderConf (topicConfig .getTopicName (),
220
+ orderConf .toString (), true );
221
+ System .out .printf ("set cluster orderConf. isOrder=%s, orderConf=[%s]" , order , orderConf );
222
+ }
201
223
} catch (Exception e ) {
202
- e . printStackTrace ( );
203
- throw new RuntimeException ("create topic error " + topic , e );
224
+ LOG . error ( "Create topic error" , e );
225
+ throw new RuntimeException ("Create topic error " + topic , e );
204
226
} finally {
205
227
defaultMQAdminExt .shutdown ();
206
228
}
207
229
}
208
230
209
-
210
231
@ Override
211
232
public List <ISplit > getSplitList () {
212
233
initProducer ();
@@ -245,7 +266,6 @@ public int getSplitNum() {
245
266
return splitNames .size ();
246
267
}
247
268
248
-
249
269
public String getTags () {
250
270
return tags ;
251
271
}
@@ -302,4 +322,19 @@ public void setNamesrvAddr(String namesrvAddr) {
302
322
this .namesrvAddr = namesrvAddr ;
303
323
}
304
324
325
+ public String getClusterName () {
326
+ return clusterName ;
327
+ }
328
+
329
+ public void setClusterName (String clusterName ) {
330
+ this .clusterName = clusterName ;
331
+ }
332
+
333
+ public boolean isOrder () {
334
+ return order ;
335
+ }
336
+
337
+ public void setOrder (boolean order ) {
338
+ this .order = order ;
339
+ }
305
340
}
0 commit comments