org.nofdev.topic.TopicConsumerContainer.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of service-topic-consumer Show documentation
Show all versions of service-topic-consumer Show documentation
The basic componet of Nofdev Topic framework
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)
}
}