本文整理汇总了Java中com.amazonaws.services.sns.AmazonSNS.publish方法的典型用法代码示例。如果您正苦于以下问题:Java AmazonSNS.publish方法的具体用法?Java AmazonSNS.publish怎么用?Java AmazonSNS.publish使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.amazonaws.services.sns.AmazonSNS
的用法示例。
在下文中一共展示了AmazonSNS.publish方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: execute
import com.amazonaws.services.sns.AmazonSNS; //导入方法依赖的package包/类
@Override
public void execute(PostDownloadExecution execution) {
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretAccessKey);
AmazonSNS sns = snsClientBuilder.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
LOGGER.debug("SNS: Topic Arn : {}", arn);
LOGGER.debug("SNS: Topic Region : {}", region);
LOGGER.debug("SNS: Topic Access Key : {}", accessKey);
LOGGER.debug("SNS: Topic Secret Access Key : {}", secretAccessKey);
PublishRequest request = new PublishRequest();
request.setTopicArn(arn);
request.setMessageStructure("json");
request.setMessage(formatJsonMessage(execution.fileName));
request.setSubject("A new file has been downloaded");
LOGGER.info("Publishing message to SNS");
PublishResult result = sns.publish(request);
LOGGER.info("Publish successful!");
LOGGER.debug("{}", result.getMessageId());
}
示例2: sendPushMessage
import com.amazonaws.services.sns.AmazonSNS; //导入方法依赖的package包/类
public PublishResult sendPushMessage(AmazonSNS snsClient, PushPlatform platform, String targetArn, String message,
Map<String, MessageAttributeValue> messageAttributes) throws IOException {
Map<String, String> messageMap = new HashMap<String, String>();
messageMap.put(platform.name(), message);
message = ServerUtil.asJson(messageMap);
PublishRequest publishRequest = new PublishRequest();
publishRequest.setMessageStructure("json");
if (messageAttributes != null) {
publishRequest.setMessageAttributes(messageAttributes);
}
publishRequest.setTargetArn(targetArn);
publishRequest.setMessage(message);
PublishResult publishResult = snsClient.publish(publishRequest);
return publishResult;
}
示例3: publishAndReceiveSeparateSQSClients
import com.amazonaws.services.sns.AmazonSNS; //导入方法依赖的package包/类
public void publishAndReceiveSeparateSQSClients() {
final String queueName = someQueueName();
final String queueUrl = someNewQueue(queueName);
final String topicName = "publishAndReceiveSeparateSQSClients";
final String message = "hi from " + topicName;
AmazonSNS amazonSNS = new InMemorySNS(_amazonSQS1,
new Subscription().
withTopicArn(makeTopicArn(topicName)).
withProtocol("sqs").
withSubscriptionArn(makeSomeSubArn(topicName)).
withEndpoint(getQueueArn(queueName)));
amazonSNS.publish(new PublishRequest(makeTopicArn(topicName), message));
ReceiveMessageResult result = _amazonSQS2.receiveMessage(new ReceiveMessageRequest(queueUrl).
withWaitTimeSeconds(15));
Assert.assertEquals(result.getMessages().size(), 1);
Assert.assertEquals(result.getMessages().get(0).getBody(), message);
}
示例4: 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;
}
示例5: publishAndReceive
import com.amazonaws.services.sns.AmazonSNS; //导入方法依赖的package包/类
public void publishAndReceive() {
final String queueName = someQueueName();
final String queueUrl = someNewQueue(queueName);
final String topicName = "publishAndReceive";
final String message = "hi from " + topicName;
AmazonSNS amazonSNS = new InMemorySNS(_amazonSQS1,
new Subscription().
withTopicArn(makeTopicArn(topicName)).
withProtocol("sqs").
withSubscriptionArn(makeSomeSubArn(topicName)).
withEndpoint(getQueueArn(queueName)));
Assert.assertEquals(amazonSNS.listTopics().getTopics().size(), 1);
Assert.assertEquals(amazonSNS.listSubscriptions().getSubscriptions().size(), 1);
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);
}
示例6: 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);
}
示例7: publish
import com.amazonaws.services.sns.AmazonSNS; //导入方法依赖的package包/类
@Override
public PublishResult publish(String topicArn, String messageText, Map<String, MessageAttributeValue> messageAttributes, AmazonSNS amazonSNS)
{
return amazonSNS.publish(new PublishRequest().withTopicArn(topicArn).withMessage(messageText).withMessageAttributes(messageAttributes));
}
示例8: 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());
}