Search in sources :

Example 1 with Message

use of com.aliyun.mns.model.Message in project summer-mis by cn-cerc.

the class SyncServerQueue method pop.

@Override
public int pop(ISession session, IPopProcesser popProcesser, int maxRecords) {
    if (popFrom == null)
        throw new RuntimeException("popFrom is null");
    if (popTo == null)
        throw new RuntimeException("popTo is null");
    // 取出数据队列
    String queueCode = popFrom.name().toLowerCase() + "-to-" + popTo.name().toLowerCase();
    QueueServer mns = (QueueServer) session.getProperty(QueueServer.SessionId);
    CloudQueue queue = mns.openQueue(queueCode);
    for (int i = 0; i < maxRecords; i++) {
        Message msg = queue.popMessage();
        if (msg == null) {
            return i;
        }
        String receiptHandle = msg.getReceiptHandle();
        String body = msg.getMessageBody();
        if (body == null) {
            queue.deleteMessage(receiptHandle);
            continue;
        }
        DataRow record = new DataRow();
        record.setJson(body);
        try {
            if (!popProcesser.popRecord(session, record, true)) {
                log.error("{} 处理失败,请检查数据源和帐套信息 {}", receiptHandle, body);
            }
            queue.deleteMessage(receiptHandle);
        } catch (Exception e) {
            log.error(record.toString());
            e.printStackTrace();
        }
    }
    return maxRecords;
}
Also used : Message(com.aliyun.mns.model.Message) QueueServer(cn.cerc.db.queue.QueueServer) DataRow(cn.cerc.db.core.DataRow) CloudQueue(com.aliyun.mns.client.CloudQueue)

Example 2 with Message

use of com.aliyun.mns.model.Message in project summer-mis by cn-cerc.

the class SyncServerQueue method push.

@Override
public void push(ISession session, DataRow record) {
    if (pushFrom == null)
        throw new RuntimeException("pushFrom is null");
    if (pushTo == null)
        throw new RuntimeException("pushTo is null");
    // 初始化客户端
    QueueServer mns = (QueueServer) session.getProperty(QueueServer.SessionId);
    // 数据写入队列
    String queueCode = pushFrom.name().toLowerCase() + "-to-" + pushTo.name().toLowerCase();
    CloudQueue queue = mns.openQueue(queueCode);
    Message message = new Message();
    message.setMessageBody(record.toString());
    queue.putMessage(message);
}
Also used : Message(com.aliyun.mns.model.Message) QueueServer(cn.cerc.db.queue.QueueServer) CloudQueue(com.aliyun.mns.client.CloudQueue)

Example 3 with Message

use of com.aliyun.mns.model.Message in project xnx3 by xnx3.

the class MNSUtil method putMessage.

/**
 * 发送消息
 * @param queueName 要发送消息的队列名字
 * @param messageBody 待发送消息正文,控制台消息发送接受的缺省编码为base64, 和Message Service 的官方SDK一致。
 * @return {@link Message}
 * 			<ul>
 * 				<li>null : 失败</li>
 * 				<li>不为null,返回 {@link Message}对象 : 成功</li>
 * 			</ul>
 */
public Message putMessage(String queueName, String messageBody) {
    Message message = new Message();
    message.setMessageBody(messageBody);
    return putMessage(queueName, message);
}
Also used : Message(com.aliyun.mns.model.Message)

Example 4 with Message

use of com.aliyun.mns.model.Message 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 5 with Message

use of com.aliyun.mns.model.Message 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)

Aggregations

Message (com.aliyun.mns.model.Message)6 CloudQueue (com.aliyun.mns.client.CloudQueue)5 QueueServer (cn.cerc.db.queue.QueueServer)2 DataRow (cn.cerc.db.core.DataRow)1 ClientException (com.aliyun.mns.common.ClientException)1 ServiceException (com.aliyun.mns.common.ServiceException)1 HttpEndpoint (com.aliyun.mns.sample.HttpEndpoint)1