admin
2018-12-19 b91b38942de4a865f105c4ca1e7d5ffce1b60b66
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package com.qcloud.cmq;
 
import java.util.List;
import java.util.TreeMap;
import java.util.Vector;
import java.lang.Integer;
 
import com.qcloud.cmq.*;
import com.qcloud.cmq.Json.*;
 
/**
 *  CMQ_jAVA_SDK_V1.0.2
 *  Update: Add topic and subscription function
 *  Tips:
 *  1 Account class object is not safty in multy thread.
 *  2 You should init different account for topic or queue
 *  Account class
 * @author York.
 *         Created 2016年9月27日.
 */
public class Account{
 
 
    protected CMQClient client;
 
    /**
     * @brief
     * @param secretId    String 
     * @param secretKey   String
     * @param endpoint    String  https://cmq-queue-gz.api.qcloud.com
     * @param method      String "POST" or "GET"  default: "POST"
     */
    public Account(String endpoint, String secretId, String secretKey){
        this.client = new CMQClient(endpoint, "/v2/index.php", secretId, secretKey, "POST");
    }
 
    /**
     * @brief
     * @param secretId    String
     * @param secretKey   String
     * @param endpoint    https://cmq-queue-gz.api.qcloud.com
     * @param path          String
     * @param method      String "POST" or "GET" default :"POST"
     */
    public Account(String secretId, String secretKey,String endpoint, String path, String method){
        this.client = new CMQClient(endpoint, path, secretId, secretKey, method);
    }
    
    public void setSignMethod(String Method)
    {
        this.client.setSignMethod(Method);
    }
    /**
     * create queue
     * @param queueName   String queue name
     * @param meta        QueueMeta class object
     * @throws Exception
     * @throws CMQClientException
     * @throws CMQServerException
     */
    public void createQueue(String queueName,QueueMeta meta) throws Exception {
        TreeMap<String, String> param = new TreeMap<String, String>();
        if(queueName.equals(""))
            throw new CMQClientException("Invalid parameter:queueName is empty");
        else
            param.put("queueName",queueName);
 
        if(meta.maxMsgHeapNum > 0)
            param.put("maxMsgHeapNum",Integer.toString(meta.maxMsgHeapNum));
        if(meta.pollingWaitSeconds > 0)
            param.put("pollingWaitSeconds",Integer.toString(meta.pollingWaitSeconds));
        if(meta.visibilityTimeout > 0)
            param.put("visibilityTimeout",Integer.toString(meta.visibilityTimeout));
        if(meta.maxMsgSize > 0)
            param.put("maxMsgSize",Integer.toString(meta.maxMsgSize));
        if(meta.msgRetentionSeconds > 0)
            param.put("msgRetentionSeconds",Integer.toString(meta.msgRetentionSeconds));
 
        String result = this.client.call("CreateQueue", param);
        JSONObject jsonObj = new JSONObject(result);
        int code = jsonObj.getInt("code");
        if(code != 0)
            throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
    }
 
    /**
     * delete queue
     *
     * @param queueName   String queue name
     * @throws CMQClientException
     * @throws CMQServerException
     */
    public void deleteQueue(String queueName) throws Exception {
        TreeMap<String, String> param = new TreeMap<String, String>();
        if(queueName.equals(""))
            throw new CMQClientException("Invalid parameter:queueName is empty");
        else
            param.put("queueName",queueName);
 
        String result = this.client.call("DeleteQueue", param);
        JSONObject jsonObj = new JSONObject(result);
        int code = jsonObj.getInt("code");
        if(code != 0)
            throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
    }
 
    /**
     *  list queue
     *
     * @param searchWord String
     * @param offset     int
     * @param limit      int
     * @param queueList  List<String>
     * @return totalCount int
     * @throws Exception
     * @throws CMQClientException
     * @throws CMQServerException
     */
    public int listQueue(String searchWord, int offset, int limit, List<String> queueList) throws Exception {
        TreeMap<String, String> param = new TreeMap<String, String>();
        if(!searchWord.equals(""))
            param.put("searchWord",searchWord);
        if(offset >=0 )
            param.put("offset",Integer.toString(offset));
        if(limit > 0 )
            param.put("limit",Integer.toString(limit));
 
        String result = this.client.call("ListQueue", param);
        JSONObject jsonObj = new JSONObject(result);
        int code = jsonObj.getInt("code");
        if(code != 0)
            throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
 
        int totalCount = jsonObj.getInt("totalCount");
        JSONArray jsonArray = jsonObj.getJSONArray("queueList");
        for(int i=0;i<jsonArray.length();i++)
        {
            JSONObject obj = (JSONObject)jsonArray.get(i);
            queueList.add(obj.getString("queueName"));
        }
 
        return totalCount;
    }
 
    /**
     * get Queue
     * @param queueName  String
     * @return Queue object
     *
     */
    public Queue getQueue(String queueName){
        return new Queue(queueName,this.client);
    }
 
    /**
     * get topic object
     * @param topicName String
     * @return Topic object
     */
    public Topic getTopic(String topicName)
    {
        return new Topic(topicName, this.client);
    }
 
 
    /**
     * TODO create topic
     *
     * @param topicName   String
     * @param maxMsgSize  int
     * @param filterType  
     * @throws Exception
     */
    public void createTopic(final String topicName, final int maxMsgSize) throws Exception
    {
        createTopic(topicName,maxMsgSize,1);
    }
    public void createTopic(final String  topicName, final int maxMsgSize, int filterType) throws Exception
    {
        TreeMap<String, String> param = new TreeMap<String, String>();
        if(topicName.equals(""))
            throw new CMQClientException("Invalid parameter:topicName is empty");
        else
            param.put("topicName",topicName);
 
        param.put("filterType",Integer.toString(filterType));
        if(maxMsgSize < 1  || maxMsgSize > 65536)
            throw new CMQClientException("Invalid parameter: maxMsgSize > 65536 or maxMsgSize < 1");
 
        param.put("maxMsgSize",Integer.toString(maxMsgSize));
        String result = this.client.call("CreateTopic", param);
        JSONObject jsonObj = new JSONObject(result);
        int code = jsonObj.getInt("code");
        if(code != 0)
            throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
    }
 
 
    /**
     * TODO delete topic
     *
     * @param topicName  String
     * @throws Exception int
     */
    public void deleteTopic(final String topicName) throws Exception
    {
        TreeMap<String, String> param = new TreeMap<String, String>();
        if(topicName.equals(""))
            throw new CMQClientException("Invalid parameter:topicName is empty");
        else
            param.put("topicName",topicName);
 
        String result = this.client.call("DeleteTopic", param);
        JSONObject jsonObj = new JSONObject(result);
        int code = jsonObj.getInt("code");
        if(code != 0)
            throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
    }
 
 
    /**
     * TODO list topic
     *
     * @param searchWord  String
     * @param vTopicList  List<String>
     * @param offset      int
     * @param limit       int
     * @return totalCount int
     * @throws Exception
     */
    public int listTopic( final String searchWord, List<String> vTopicList , final int offset ,final int limit) throws Exception
    {
        TreeMap<String, String> param = new TreeMap<String, String>();
        if(!searchWord.equals(""))
            param.put("searchWord",searchWord);
        if(offset >=0 )
            param.put("offset",Integer.toString(offset));
        if(limit > 0 )
            param.put("limit",Integer.toString(limit));
 
        String result = this.client.call("ListTopic", param);
        JSONObject jsonObj = new JSONObject(result);
        int code = jsonObj.getInt("code");
        if(code != 0)
            throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
 
 
        int totalCount = jsonObj.getInt("totalCount");
        JSONArray jsonArray = jsonObj.getJSONArray("topicList");
 
        vTopicList.clear();
        for(int i=0;i<jsonArray.length();i++)
        {
            JSONObject obj = (JSONObject)jsonArray.get(i);
            vTopicList.add(obj.getString("topicName"));
        }
        return totalCount;
    }
 
    /**
     * TODO create subscribe
     *
     * @param topicName        String
     * @param subscriptionName String
     * @param Endpoint         String
     * @param Protocal           String
     * @throws Exception
     */
    public void createSubscribe(final String topicName, final String subscriptionName, final String Endpoint,final String Protocal) throws Exception
    {
        createSubscribe(topicName,subscriptionName, Endpoint,Protocal,null,null,"BACKOFF_RETRY","JSON");
 
    }
    /**
     * TODO create subscribe
     *
     * @param topicName           String
     * @param subscriptionName      String
     * @param Endpoint            String
     * @param Protocal            String
     * @param FilterTag           List<String>
     * @param NotifyStrategy      String
     * @param NotifyContentFormat String
     * @throws Exception
     */
    public void createSubscribe(final String topicName, final String subscriptionName, final String Endpoint,final String Protocal,
            final List<String> FilterTag , final List<String>bindingKey,final String NotifyStrategy, final  String NotifyContentFormat) throws Exception
    {
 
 
        if ( FilterTag != null && FilterTag.size() > 5)
            throw new CMQClientException("Invalid parameter: Tag number > 5");
 
 
        TreeMap<String, String> param = new TreeMap<String, String>();
        if(topicName.equals(""))
            throw new CMQClientException("Invalid parameter:topicName is empty");
 
        param.put("topicName",topicName);
 
        if(subscriptionName.equals(""))
            throw new CMQClientException("Invalid parameter:subscriptionName is empty");
 
        param.put("subscriptionName", subscriptionName);
 
        if(Endpoint.equals(""))
            throw new CMQClientException("Invalid parameter:Endpoint is empty");
 
        param.put("endpoint", Endpoint);
 
        if(Protocal.equals(""))
            throw new CMQClientException("Invalid parameter:Protocal is empty");
 
        param.put("protocol", Protocal);
 
        if(NotifyStrategy.equals(""))
            throw new CMQClientException("Invalid parameter:NotifyStrategy is empty");
 
        param.put("notifyStrategy", NotifyStrategy);
 
        if(NotifyContentFormat.equals("") )
            throw new CMQClientException("Invalid parameter:NotifyContentFormat is empty");
        param.put("notifyContentFormat", NotifyContentFormat);
 
        if(FilterTag != null )
        {
                      for(int i = 0 ; i < FilterTag.size() ; ++i)
                            param.put("filterTag."+ Integer.toString(i+1),FilterTag.get(i));
        }
        if(bindingKey != null )
        {
                      for(int i = 0 ; i < bindingKey.size() ; ++i)
                            param.put("bindingKey."+ Integer.toString(i+1),bindingKey.get(i));
        }
 
        String result = this.client.call("Subscribe", param);
        JSONObject jsonObj = new JSONObject(result);
        int code = jsonObj.getInt("code");
        if(code != 0)
            throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
    }
 
 
    /**
     *  delete subscription .
     *
     * @param topicName          String
     * @param subscriptionName   String
     * @throws Exception
     */
    public void deleteSubscribe(final String topicName, final String subscriptionName) throws Exception
    {
        TreeMap<String, String> param = new TreeMap<String, String>();
        if(topicName.equals(""))
            throw new CMQClientException("Invalid parameter:topicName is empty");
        else
            param.put("topicName",topicName);
 
        if(subscriptionName.equals(""))
            throw new CMQClientException("Invalid parameter:subscriptionName is empty");
        else
            param.put("subscriptionName",subscriptionName);
 
        String result = this.client.call("Unsubscribe", param);
        JSONObject jsonObj = new JSONObject(result);
        int code = jsonObj.getInt("code");
        if(code != 0)
            throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
    }
 
 
    /**
     * TODO get a subscription object.
     *
     * @param topicName          String
     * @param subscriptionName     String
     * @return
     */
    public Subscription getSubscription(final String topicName,final String subscriptionName)
    {
        return new Subscription(topicName, subscriptionName, this.client);
    }
}