Search in sources :

Example 1 with CloudTopic

use of com.aliyun.mns.client.CloudTopic in project xnx3 by xnx3.

the class MNSUtil method createTopic.

/**
 * 创建主题
 * @param topicName 要创建的主题的名字
 */
public void createTopic(String topicName) {
    TopicMeta meta = new TopicMeta();
    meta.setTopicName(topicName);
    try {
        CloudTopic topic = getMNSClient().createTopic(meta);
    } catch (Exception e) {
        System.out.println("create topic error, " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : TopicMeta(com.aliyun.mns.model.TopicMeta) CloudTopic(com.aliyun.mns.client.CloudTopic) ServiceException(com.aliyun.mns.common.ServiceException) ClientException(com.aliyun.mns.common.ClientException)

Example 2 with CloudTopic

use of com.aliyun.mns.client.CloudTopic in project xnx3 by xnx3.

the class MNSUtil method createSubScription.

/**
 * 创建订阅
 * @param topicName 要在哪个主题下创建订阅,这个是主题的名字
 * @param subMeta 要创建的订阅的对象
 * @return
 */
public BaseVO createSubScription(String topicName, SubscriptionMeta subMeta) {
    BaseVO vo = new BaseVO();
    CloudTopic topic = getMNSClient().getTopicRef(topicName);
    try {
        // SubscriptionMeta subMeta = new SubscriptionMeta();
        // subMeta.setSubscriptionName("TestSub");
        // subMeta.setEndpoint(HttpEndpoint.GenEndpointLocal());
        // subMeta.setNotifyContentFormat(SubscriptionMeta.NotifyContentFormat.XML);
        // //subMeta.setFilterTag("filterTag"); //设置订阅的filterTag
        String subUrl = topic.subscribe(subMeta);
        vo.setBaseVO(BaseVO.SUCCESS, subUrl);
    } catch (Exception e) {
        vo.setBaseVO(BaseVO.FAILURE, e.getMessage());
        System.out.println("subscribe/unsubribe error");
        e.printStackTrace();
    }
    return vo;
}
Also used : CloudTopic(com.aliyun.mns.client.CloudTopic) BaseVO(com.xnx3.BaseVO) ServiceException(com.aliyun.mns.common.ServiceException) ClientException(com.aliyun.mns.common.ClientException)

Example 3 with CloudTopic

use of com.aliyun.mns.client.CloudTopic in project jeesuite-libs by vakinge.

the class MNSClientInstance method createTopicIfAbsent.

public static CloudTopic createTopicIfAbsent(String topicName, String subForQueue) {
    TopicMeta topicMeta = new TopicMeta();
    topicMeta.setTopicName(topicName);
    CloudTopic topic = getClient().getTopicRef(topicName);
    try {
        topic.getAttribute();
    } catch (ServiceException e) {
        if ("TopicNotExist".equals(e.getErrorCode())) {
            topic.create(topicMeta);
        }
    }
    if (StringUtils.isNotBlank(subForQueue)) {
        String subscriptionName = "sub-for-queue-" + subForQueue;
        PagingListResult<SubscriptionMeta> topicSubscriptions = topic.listSubscriptions(subscriptionName, "", 1);
        if (topicSubscriptions == null || topicSubscriptions.getResult() == null || topicSubscriptions.getResult().isEmpty()) {
            // 创建订阅关系
            SubscriptionMeta subMeta = new SubscriptionMeta();
            subMeta.setSubscriptionName(subscriptionName);
            subMeta.setEndpoint(topic.generateQueueEndpoint(subForQueue));
            subMeta.setNotifyContentFormat(SubscriptionMeta.NotifyContentFormat.SIMPLIFIED);
            subMeta.setNotifyStrategy(SubscriptionMeta.NotifyStrategy.EXPONENTIAL_DECAY_RETRY);
            topic.subscribe(subMeta);
        }
    }
    return topic;
}
Also used : TopicMeta(com.aliyun.mns.model.TopicMeta) CloudTopic(com.aliyun.mns.client.CloudTopic) SubscriptionMeta(com.aliyun.mns.model.SubscriptionMeta) ServiceException(com.aliyun.mns.common.ServiceException)

Example 4 with CloudTopic

use of com.aliyun.mns.client.CloudTopic in project jeesuite-libs by vakinge.

the class MNSProducer method publishMessage.

public String publishMessage(String topicName, Object data) {
    CloudTopic topic = getTopic(topicName);
    TopicMessage tMessage = new RawTopicMessage();
    tMessage.setBaseMessageBody(new MQMessage(topicName, data).toMessageValue(true));
    topic.publishMessage(tMessage);
    return tMessage.getMessageId();
}
Also used : CloudTopic(com.aliyun.mns.client.CloudTopic) RawTopicMessage(com.aliyun.mns.model.RawTopicMessage) TopicMessage(com.aliyun.mns.model.TopicMessage) RawTopicMessage(com.aliyun.mns.model.RawTopicMessage) MQMessage(com.mendmix.amqp.MQMessage)

Example 5 with CloudTopic

use of com.aliyun.mns.client.CloudTopic in project jeesuite-libs by vakinge.

the class MNSClientInstance method createTopicIfAbsent.

public static CloudTopic createTopicIfAbsent(String topicName, String subForQueue) {
    TopicMeta topicMeta = new TopicMeta();
    topicMeta.setTopicName(topicName);
    CloudTopic topic = getClient().getTopicRef(topicName);
    try {
        topic.getAttribute();
    } catch (ServiceException e) {
        if ("TopicNotExist".equals(e.getErrorCode())) {
            topic.create(topicMeta);
        }
    }
    if (StringUtils.isNotBlank(subForQueue)) {
        String subscriptionName = "sub-for-queue-" + subForQueue;
        PagingListResult<SubscriptionMeta> topicSubscriptions = topic.listSubscriptions(subscriptionName, "", 1);
        if (topicSubscriptions == null || topicSubscriptions.getResult() == null || topicSubscriptions.getResult().isEmpty()) {
            // 创建订阅关系
            SubscriptionMeta subMeta = new SubscriptionMeta();
            subMeta.setSubscriptionName(subscriptionName);
            subMeta.setEndpoint(topic.generateQueueEndpoint(subForQueue));
            subMeta.setNotifyContentFormat(SubscriptionMeta.NotifyContentFormat.SIMPLIFIED);
            subMeta.setNotifyStrategy(SubscriptionMeta.NotifyStrategy.EXPONENTIAL_DECAY_RETRY);
            topic.subscribe(subMeta);
        }
    }
    return topic;
}
Also used : TopicMeta(com.aliyun.mns.model.TopicMeta) CloudTopic(com.aliyun.mns.client.CloudTopic) SubscriptionMeta(com.aliyun.mns.model.SubscriptionMeta) ServiceException(com.aliyun.mns.common.ServiceException)

Aggregations

CloudTopic (com.aliyun.mns.client.CloudTopic)8 ServiceException (com.aliyun.mns.common.ServiceException)4 TopicMeta (com.aliyun.mns.model.TopicMeta)3 ClientException (com.aliyun.mns.common.ClientException)2 RawTopicMessage (com.aliyun.mns.model.RawTopicMessage)2 SubscriptionMeta (com.aliyun.mns.model.SubscriptionMeta)2 TopicMessage (com.aliyun.mns.model.TopicMessage)2 MQMessage (com.jeesuite.amqp.MQMessage)1 MQMessage (com.mendmix.amqp.MQMessage)1 BaseVO (com.xnx3.BaseVO)1