当前位置: 首页>>代码示例>>Java>>正文


Java AmazonSNS.createTopic方法代码示例

本文整理汇总了Java中com.amazonaws.services.sns.AmazonSNS.createTopic方法的典型用法代码示例。如果您正苦于以下问题:Java AmazonSNS.createTopic方法的具体用法?Java AmazonSNS.createTopic怎么用?Java AmazonSNS.createTopic使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.amazonaws.services.sns.AmazonSNS的用法示例。


在下文中一共展示了AmazonSNS.createTopic方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: postMessage

import com.amazonaws.services.sns.AmazonSNS; //导入方法依赖的package包/类
/**
 * Sends using the sns publisher
 */
@Override
public boolean postMessage(NotificationMessage nMsg,
                           BasicSubscriber subscriber) {

    SNSSubscriber sub;
    if (subscriber instanceof SNSSubscriber) {
        sub = (SNSSubscriber) subscriber;
    } else {
        throw new ClassCastException("invalid subscriber " + subscriber.getClass().getName());
    }

    SNSMessage msg = buildSNSMessage(nMsg);
    AmazonSNS sns = new AmazonSNSClient(new BasicAWSCredentials(sub.getAwsAccessKey(), sub.getAwsSecretKey()));
    if (sub.getSnsEndpoint() != null) {
        sns.setEndpoint(sub.getSnsEndpoint());
    }

    CreateTopicRequest tRequest = new CreateTopicRequest();
    tRequest.setName(msg.getTopicName());
    CreateTopicResult result = sns.createTopic(tRequest);

    PublishRequest pr = new PublishRequest(result.getTopicArn(), msg.getTxtMessage()).withSubject(msg.getSubject());

    try {
        PublishResult pubresult = sns.publish(pr);
        logger.info("Published msg with id - " + pubresult.getMessageId());
    } catch (AmazonClientException ace) {
        logger.error(ace.getMessage());
        return false;
    }
    return true;
}
 
开发者ID:oneops,项目名称:oneops,代码行数:36,代码来源:SNSService.java

示例2: manualSubscriptionSetup

import com.amazonaws.services.sns.AmazonSNS; //导入方法依赖的package包/类
public void manualSubscriptionSetup() {
    final String queueName = someQueueName();
    final String queueUrl = someNewQueue(queueName);
    final String topicName = "manualSubscriptionSetup";
    final String message = "hi from " + topicName;

    AmazonSNS amazonSNS = new InMemorySNS(_amazonSQS1);

    amazonSNS.createTopic(new CreateTopicRequest(topicName));
    Assert.assertEquals(amazonSNS.listTopics().getTopics().size(), 1);
    Assert.assertEquals(amazonSNS.listTopics().getTopics().get(0).getTopicArn(), makeTopicArn(topicName));

    //make sure create is idempotent
    amazonSNS.createTopic(new CreateTopicRequest(topicName));
    Assert.assertEquals(amazonSNS.listTopics().getTopics().size(), 1, "existing topic duplicated?");
    Assert.assertEquals(amazonSNS.listTopics().getTopics().get(0).getTopicArn(), makeTopicArn(topicName));

    //subscribe.
    amazonSNS.subscribe(new SubscribeRequest().
            withEndpoint(getQueueArn(queueName)).
            withProtocol("sqs").
            withTopicArn(makeTopicArn(topicName)));

    Assert.assertEquals(amazonSNS.listSubscriptions().getSubscriptions().size(), 1);
    Assert.assertEquals(amazonSNS.listSubscriptions().getSubscriptions().get(0).getTopicArn(), makeTopicArn(topicName));
    Assert.assertEquals(amazonSNS.
            listSubscriptionsByTopic(new ListSubscriptionsByTopicRequest(makeTopicArn(topicName)))
            .getSubscriptions()
            .size(),
            1);

    amazonSNS.publish(new PublishRequest(makeTopicArn(topicName), message));

    ReceiveMessageResult result = _amazonSQS1.receiveMessage(new ReceiveMessageRequest(queueUrl));
    Assert.assertEquals(result.getMessages().size(), 1);
    Assert.assertEquals(result.getMessages().get(0).getBody(), message);
}
 
开发者ID:bazaarvoice,项目名称:awslocal,代码行数:38,代码来源:TestSNSClient.java

示例3: process

import com.amazonaws.services.sns.AmazonSNS; //导入方法依赖的package包/类
public void process(Exchange exchange) throws Exception {

		// TODO cache arns and don't create if not necessary
		final String topic = determineTopic(exchange);

		// creates a new topic, or returns the URL of an existing one
		CreateTopicRequest request = new CreateTopicRequest(topic);

		LOG.trace("Creating topic [{}] with request [{}]...", topic, request);

		final AmazonSNS snsClient = getEndpoint().getSNSClient();
		
		CreateTopicResult result = snsClient.createTopic(request);

		final String topicArn = result.getTopicArn();
		LOG.trace("Topic created with Amazon resource name: {}", topicArn);

		final SnsConfiguration configuration = getEndpoint().getConfiguration();
		
		if (ObjectHelper.isNotEmpty(configuration.getPolicy())) {
			LOG.trace("Updating topic [{}] with policy [{}]", topicArn, configuration.getPolicy());

			snsClient.setTopicAttributes(
					new SetTopicAttributesRequest(topicArn, "Policy", configuration.getPolicy()));

			LOG.trace("Topic policy updated");
		}

		PublishRequest publishRequest = new PublishRequest();

		publishRequest.setTopicArn(topicArn);
		publishRequest.setSubject(determineSubject(exchange));
		publishRequest.setMessageStructure(determineMessageStructure(exchange));
		publishRequest.setMessage(exchange.getIn().getBody(String.class));

		LOG.trace("Sending request [{}] from exchange [{}]...", publishRequest, exchange);

		PublishResult publishResult = snsClient.publish(publishRequest);

		LOG.trace("Received result [{}]", publishResult);

		Message message = getMessageForResponse(exchange);
		message.setHeader(SnsConstants.MESSAGE_ID, publishResult.getMessageId());
	}
 
开发者ID:highsource,项目名称:aufzugswaechter,代码行数:45,代码来源:DynamicSnsProducer.java


注:本文中的com.amazonaws.services.sns.AmazonSNS.createTopic方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。