Search in sources :

Example 11 with CloudQueue

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;
}
Also used : Message(com.aliyun.mns.model.Message) CloudQueue(com.aliyun.mns.client.CloudQueue)

Example 12 with CloudQueue

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();
}
Also used : Message(com.aliyun.mns.model.Message) HttpEndpoint(com.aliyun.mns.sample.HttpEndpoint) CloudQueue(com.aliyun.mns.client.CloudQueue)

Example 13 with CloudQueue

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;
}
Also used : Message(com.aliyun.mns.model.Message) ServiceException(com.aliyun.mns.common.ServiceException) ClientException(com.aliyun.mns.common.ClientException) ServiceException(com.aliyun.mns.common.ServiceException) ClientException(com.aliyun.mns.common.ClientException) CloudQueue(com.aliyun.mns.client.CloudQueue)

Example 14 with CloudQueue

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());
}
Also used : CloudQueue(com.aliyun.mns.client.CloudQueue)

Aggregations

CloudQueue (com.aliyun.mns.client.CloudQueue)14 Message (com.aliyun.mns.model.Message)5 QueueServer (cn.cerc.db.queue.QueueServer)2 CloudAccount (com.aliyun.mns.client.CloudAccount)2 MNSClient (com.aliyun.mns.client.MNSClient)2 ClientException (com.aliyun.mns.common.ClientException)2 ServiceException (com.aliyun.mns.common.ServiceException)2 QueueMeta (com.aliyun.mns.model.QueueMeta)2 Semaphore (java.util.concurrent.Semaphore)2 MqttException (org.eclipse.paho.mqttv5.common.MqttException)2 DataRow (cn.cerc.db.core.DataRow)1 QueueSession (cn.cerc.jdb.queue.QueueSession)1 HttpEndpoint (com.aliyun.mns.sample.HttpEndpoint)1 StandardThreadExecutor (com.jeesuite.common.async.StandardThreadExecutor)1 StandardThreadFactory (com.jeesuite.common.async.StandardThreadExecutor.StandardThreadFactory)1 StandardThreadExecutor (com.mendmix.common.async.StandardThreadExecutor)1 StandardThreadFactory (com.mendmix.common.async.StandardThreadExecutor.StandardThreadFactory)1