admin
2020-10-26 89e370bfdda29ac8a8f7080a18dc09a6ddc75c09
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
package com.qcloud.cmq;
 
import com.qcloud.cmq.Json.JSONArray;
import com.qcloud.cmq.Json.JSONObject;
 
import java.util.List;
import java.util.TreeMap;
import java.util.Vector;
 
/**
 * TODO topic class.
 *
 * @author York.
 *         Created 2016年9月26日.
 */
public class Topic {
       // topic name 
       protected String topicName;
       // cmq client 
       protected CMQClient client;
        
        /**
         * construct .
         *
         * @param topicName String 
         * @param client  CMQClient
         */
        Topic(String topicName, CMQClient client) {
        this.topicName = topicName;
        this.client = client;
        }
        
        
        /**
         * TODO set topic attributes
         *
         * @param maxMsgSize  int 
         * @throws Exception  
         */
        public void setTopicAttributes(final int maxMsgSize)throws Exception
        {
            if (maxMsgSize < 0  || maxMsgSize >65536)
                throw new CMQClientException("Invalid parameter maxMsgSize < 0 or maxMsgSize > 65536");
            
            TreeMap<String, String> param = new TreeMap<String, String>();
 
            param.put("topicName",this.topicName);
            
            if(maxMsgSize > 0)
                param.put("maxMsgSize",Integer.toString(maxMsgSize));
            
            String result = this.client.call("SetTopicAttributes", 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 topic attributes.
         *
         * @return  return topic meta object
         * @throws Exception
         */
        public TopicMeta getTopicAttributes() throws Exception
        {
            TreeMap<String, String> param = new TreeMap<String, String>();
 
            param.put("topicName",this.topicName);                    
            
            String result = this.client.call("GetTopicAttributes", param);
            JSONObject jsonObj = new JSONObject(result);
            int code = jsonObj.getInt("code");
            if(code != 0)
                throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
            
            TopicMeta meta = new TopicMeta();
            meta.msgCount = jsonObj.getInt("msgCount");
            meta.maxMsgSize = jsonObj.getInt("maxMsgSize");
            meta.msgRetentionSeconds = jsonObj.getInt("msgRetentionSeconds");
            meta.createTime =jsonObj.getInt("createTime");
            meta.lastModifyTime = jsonObj.getInt("lastModifyTime");    
            meta.filterType = jsonObj.getInt("filterType");
            return meta;
            
        }
        
        /**
         * publish message without  tags.
         *
         * @param message  String
         * @return msgId, String
         * @throws Exception
         */
        public String publishMessage(String message) throws Exception 
        {
            return publishMessage(message,null,null);
        }
        public String publishMessage(String message,String routingKey) throws Exception
        {
            return publishMessage(message, null,routingKey);
        }
        
        /**
         * publish message .
         *
         * @param msg  String message body
         * @param vTagList Vector<String>  message tag
          * @return  msgId String  
         * @throws Exception
         */
        public String publishMessage(String msg, List<String> vTagList,String routingKey) throws Exception
        {
            TreeMap<String, String> param = new TreeMap<String, String>();
 
            param.put("topicName",this.topicName);
            param.put("msgBody",msg);
            if(routingKey != null)
                param.put("routingKey",routingKey);
            
            if( vTagList != null )
            {
                for(int i = 0 ; i < vTagList.size() ; ++i)
                {
                    param.put("msgTag."+Integer.toString(i+1), vTagList.get(i));
                }
            }
            
            String result = this.client.call("PublishMessage", param);
            JSONObject jsonObj = new JSONObject(result);
            int code = jsonObj.getInt("code");
            if(code != 0)
                throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));            
            return jsonObj.getString("msgId");
        }
        
        /**
         * TODO batch publish message without tags.
         *
         * @param vMsgList Vector<String> message array
         * @return msgId Vector<String> message id array
         * @throws Exception 
         */
        public Vector<String> batchPublishMessage(List<String> vMsgList) throws Exception
        {
            return batchPublishMessage(vMsgList,null, null);    
        }
        public Vector<String> batchPublishMessage(List<String> vMsgList,String routingKey) throws Exception
        {
            return batchPublishMessage(vMsgList,null,routingKey);
        }
        
        /**
         * batch publish message 
         *
         * @param vMsgList   message array
         * @param vTagList   message tag array
         * @return message handles array
         * @throws Exception
         */
        public Vector<String> batchPublishMessage(List<String> vMsgList,List<String> vTagList,String routingKey ) throws Exception
        {
            
            TreeMap<String, String> param = new TreeMap<String, String>();
 
            param.put("topicName",this.topicName);
            if(routingKey != null)
                param.put("routingKey",routingKey);
            if(vMsgList != null )
            {
                for(int i = 0 ; i< vMsgList.size() ; ++i)
                {
                    param.put("msgBody."+Integer.toString(i+1), vMsgList.get(i));
                }
            }            
            if(vTagList != null)
            {
                for(int i = 0 ; i< vTagList.size() ; ++i)
                {
                    param.put("msgTag."+Integer.toString(i+1), vTagList.get(i));
                }
            }
            
            String result = this.client.call("BatchPublishMessage", param);
            JSONObject jsonObj = new JSONObject(result);
            int code = jsonObj.getInt("code");
            if(code != 0)
                throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId"));
            
            JSONArray jsonArray = jsonObj.getJSONArray("msgList");
            
            Vector<String> vmsgId = new Vector<String>();
            for(int i=0;i<jsonArray.length();i++)
            {    
                JSONObject obj = (JSONObject)jsonArray.get(i);
                vmsgId.add(obj.getString("msgId"));
            }
            
            return vmsgId;
 
        }
    /**
     * TODO list subscription by topic.
     *
     * @param totalCount           int
     * @param offset               int
     * @param limit                int
     * @param searchWord           String
     * @param vSubscriptionList    List<String>
     * @return totalCount          int
     * @throws Exception
     */
    public int ListSubscription(final  int offset , int limit ,
            final String searchWord, List< String>vSubscriptionList
            ) throws Exception
    {
        TreeMap<String, String> param = new TreeMap<String, String>();
        param.put("topicName", this.topicName);
        if( searchWord != null  && !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("ListSubscriptionByTopic", 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("subscriptionList");
        for(int i=0;i<jsonArray.length();i++)
        {
             JSONObject obj = (JSONObject)jsonArray.get(i);
             vSubscriptionList.add(obj.getString("subscriptionName"));
        }
        return totalCount;
    }
 
}