Search in sources :

Example 1 with ServiceException

use of com.aliyun.mns.common.ServiceException in project xnx3 by xnx3.

the class MNSUtil method deleteQueue.

/**
 * 删除队列
 * @param queueName
 */
public void deleteQueue(String queueName) {
    try {
        // Delete Queue
        CloudQueue queue = getMNSClient().getQueueRef(queueName);
        queue.delete();
    } catch (ClientException ce) {
        clientException(ce);
    } catch (ServiceException se) {
        serviceException(se);
    } catch (Exception e) {
        exception(e);
    }
}
Also used : 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 2 with ServiceException

use of com.aliyun.mns.common.ServiceException 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 3 with ServiceException

use of com.aliyun.mns.common.ServiceException 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 ServiceException

use of com.aliyun.mns.common.ServiceException 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)

Aggregations

ServiceException (com.aliyun.mns.common.ServiceException)4 CloudQueue (com.aliyun.mns.client.CloudQueue)2 CloudTopic (com.aliyun.mns.client.CloudTopic)2 ClientException (com.aliyun.mns.common.ClientException)2 SubscriptionMeta (com.aliyun.mns.model.SubscriptionMeta)2 TopicMeta (com.aliyun.mns.model.TopicMeta)2 Message (com.aliyun.mns.model.Message)1