use of com.aliyun.mns.client.CloudQueue in project xnx3 by xnx3.
the class MNSUtil method listMessage.
/**
* 列出这个队列内的消息(最多每次列出15条)
* <br/>消费后可使用 {@link #deleteMessage(String, Message)} 将其删除
* @param queueName 队列名字
* @return List 可根据 list.size() 进行判断当前是否有消息
*/
public List<Message> listMessage(String queueName) {
List<Message> batchPopMessage = null;
// replace with your queue name
CloudQueue queue = getMNSClient().getQueueRef(queueName);
batchPopMessage = queue.batchPopMessage(15);
if (batchPopMessage == null) {
// 避免空指针异常
batchPopMessage = new ArrayList<Message>();
}
return batchPopMessage;
}
use of com.aliyun.mns.client.CloudQueue in project xnx3 by xnx3.
the class MNSUtil method main.
/**
* 不是实时的,测试应该有个几秒的误差延迟
*/
public static void main(String[] args) {
// 方式一,在当前用户目录建立 .aliyun-mns.properties 配置文件,将三个参数加入其中
MNSUtil mns = new MNSUtil();
// 方式二,创建MNSUtil时,直接传入
// MNSUtil mns = new MNSUtil(accessKeyId, accessKeySecret, endpoint);
// 队列名字
String queueName = "testaaaaa";
// 创建队列
CloudQueue cq = mns.createQueue(queueName);
// 向队列中加入消息
mns.putMessage(queueName, "哈哈1");
mns.putMessage(queueName, "哈哈3");
// 获取队列
List<Message> list = mns.listMessage(queueName);
System.out.println("消息条数:" + list.size());
for (int i = 0; i < list.size(); i++) {
Message message = list.get(i);
System.out.println(message.getMessageId() + ":" + message.getMessageBody());
// 用完消息后删除掉
mns.deleteMessage(queueName, message);
}
// 删除队列
// mns.deleteQueue(queueName);
mns.close();
}
use of com.aliyun.mns.client.CloudQueue in project xnx3 by xnx3.
the class MNSUtil method putMessage.
/**
* 发送消息
* @param queueName 要发送消息的队列名字
* @param message 发送的消息对象
* @return {@link Message}
* <ul>
* <li>null : 失败</li>
* <li>不为null,返回 {@link Message}对象 : 成功</li>
* </ul>
*/
public Message putMessage(String queueName, Message message) {
try {
CloudQueue queue = getMNSClient().getQueueRef(queueName);
Message putMsg = queue.putMessage(message);
return putMsg;
} catch (ClientException ce) {
clientException(ce);
} catch (ServiceException se) {
serviceException(se);
} catch (Exception e) {
exception(e);
}
return null;
}
use of com.aliyun.mns.client.CloudQueue in project xnx3 by xnx3.
the class MNSUtil method deleteMessage.
/**
* 删除队列内的指定某条消息
* @param queueName
*/
public void deleteMessage(String queueName, Message message) {
// replace with your queue name
CloudQueue queue = getMNSClient().getQueueRef(queueName);
queue.deleteMessage(message.getReceiptHandle());
}
Aggregations