All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nofdev.topic.TopicConsumerContainer.groovy Maven / Gradle / Ivy

There is a newer version: 1.7.6
Show newest version
package org.nofdev.topic

import com.aliyun.mns.client.CloudAccount
import com.aliyun.mns.client.CloudQueue
import com.aliyun.mns.model.Message
import com.aliyun.mns.model.QueueMeta
import com.aliyun.mns.model.SubscriptionMeta
import com.aliyun.mns.model.TopicMeta

/**
 * 主题消费者
 * Created by aci on 12/04/2017.
 */
class TopicConsumerContainer {

    private CloudQueue queue;

    public static TopicConsumerContainer initMNSConsumer(String name, TopicConsumerConfig topicConsumerConfig) {
        def topicConsumer = new TopicConsumerContainer()


        def mnsAccount = new CloudAccount(topicConsumerConfig.getAccessId(), topicConsumerConfig.getAccessKey(), topicConsumerConfig.getEndpoint())
        def client = mnsAccount.getMNSClient()

        def subName = name + "-" + topicConsumerConfig.getGroupId()

        def meta = new QueueMeta()
        meta.setQueueName(subName)
        meta.setPollingWaitSeconds(30)
        meta.setVisibilityTimeout(300)//取出消息后隐藏5分钟
        meta.setMaxMessageSize(65536)
        meta.setMessageRetentionPeriod(1296000)
        meta.setDelayMessages(0)
        meta.setLoggingEnabled(true)
        topicConsumer.queue = client.createQueue(meta)

//        def topic = client.getTopicRef(name)

        def metaTopic = new TopicMeta()
        metaTopic.setTopicName(name)
        metaTopic.setLoggingEnabled(true)
        def topic = client.createTopic(metaTopic)

        def subMeta = new SubscriptionMeta()
        subMeta.setSubscriptionName(subName)
        subMeta.setNotifyContentFormat(SubscriptionMeta.NotifyContentFormat.SIMPLIFIED)
        subMeta.setEndpoint(topic.generateQueueEndpoint(subName))
        topic.subscribe(subMeta)

        return topicConsumer
    }

    public Message receive() {
        return this.queue.popMessage()
    }

    public void delete(String receiptHandle) {
        this.queue.deleteMessage(receiptHandle)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy