作者:俏巴
操作步骤
1、ak,sk参数获取:阿里云常见参数获取位置
2、endpoint参数获取
3、pom.xml
<dependency>
<groupId>com.aliyun.mns</groupId>
<artifactId>aliyun-sdk-mns</artifactId>
<version>1.1.8</version>
<classifier>jar-with-dependencies</classifier>
</dependency>
4、Code Sample
import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.model.PagingListResult;
import com.aliyun.mns.model.QueueMeta;
import com.aliyun.mns.model.TopicMeta;
import java.util.List;
public class DeleteQueueOrTopicDemo {
public static void main(String[] args) {
//初始参数设置 ak,sk信息及对应区域所在的地址
String accessKeyId = "********";
String accessKeySecret = "********";
String endpoint = "http://********.mns.cn-********.aliyuncs.com/";
CloudAccount account = new CloudAccount(accessKeyId, accessKeySecret, endpoint);
MNSClient client = account.getMNSClient();
String prefix = ""; //全部删除设置
String marker = "";
Integer retNumber = 10000;
// deleteQueues(client, prefix, marker, retNumber); // 删除队列
deleteTopics(client, prefix, marker, retNumber); // 删除主题
client.close();
}
/***
* delete queue
* @param client client对象
* @param prefix 队列名称前缀,如果是批量全部删除,直接使用 "" 即可
* @param marker 列举的起始位置,""表示从第一个开始,也可以是前一次列举返回的marker
* @param retNumber 最多返回的个数
*/
public static void deleteQueues(MNSClient client,String prefix,String marker,Integer retNumber)
{
PagingListResult<QueueMeta> listResult = client.listQueue(prefix, marker, retNumber);
System.out.println(listResult.getResult().size());
List<QueueMeta> queues = listResult.getResult();
//循环遍历删除Queue
int i = 0;
System.out.println(queues.size());
if(queues.size() != 0) {
for (QueueMeta queue : queues) {
//删除消息队列
String queueName = queue.getQueueName();
client.getQueueRef(queueName).delete();
i++;
System.out.println("Delete queue " + queueName);
}
}else{
System.out.println("没有满足筛选条件的队列");
}
System.out.println("The number of deleted queues is" + i);
}
/**
* delete topic
* @param client client对象
* @param prefix 主题名称前缀,如果是批量全部删除,直接使用 "" 即可
* @param marker 列举的起始位置,""表示从第一个开始,也可以是前一次列举返回的marker
* @param retNumber 最多返回的个数
*/
public static void deleteTopics(MNSClient client,String prefix,String marker,Integer retNumber)
{
PagingListResult<TopicMeta> listResult = client.listTopic(prefix, marker, retNumber);
List<TopicMeta> topics = listResult.getResult();
//循环遍历删除Queue
int i = 0;
if (topics.size() != 0) {
//循环遍历删除Topic
for (TopicMeta topic : topics) {
//删除消息Topic
String topicName = topic.getTopicName();
client.getTopicRef(topicName).delete();
System.out.println("Delete Topic " + topicName);
i++;
}
}
else{
System.out.println("没有满足筛选条件的主题");
}
System.out.println("The number of deleted topics is" + i);
}
}
参考链接
MNS Java SDK下载