我一直在尝试在 Java 中实现 GetAttributeRequest,它具有与以下 C# 代码类似的功能。
try
{
long epochWindowTime = ToEpochTimeInMilliseconds(DateTime.UtcNow.Subtract(this.SQSWindow));
int numberOfMessages = 0;
// 1st query to determine the # of Messages available in the queue
using (AmazonSQSClient client = new AmazonSQSClient(
this.AWSAccessKey, this.AWSSecretAccessKey,
ew AmazonSQSConfig() { ServiceURL = this.AWSSQSServiceUrl }))
{
// get the NumberOfMessages to optimize how to Receive all of the messages from the queue
GetQueueAttributesRequest attributesRequest = new GetQueueAttributesRequest();
attributesRequest.QueueUrl = this.QueueUrl;
attributesRequest.AttributeName.Add("ApproximateNumberOfMessages");
numberOfMessages = client.GetQueueAttributes(attributesRequest).GetQueueAttributesResult.ApproximateNumberOfMessages;
}
我对 Java 实现的尝试如下所示,
try
{
long epochWindowTime;
int numberOfMessages = 0;
Map<String, String> attributes;
// Setup the SQS client
AmazonSQS client = new AmazonSQSClient(new
ClasspathPropertiesFileCredentialsProvider());
client.setEndpoint(this.AWSSQSServiceUrl);
// get the NumberOfMessages to optimize how to
// Receive all of the messages from the queue
GetQueueAttributesRequest attributesRequest =
new GetQueueAttributesRequest();
attributesRequest.setQueueUrl(this.QueueUrl);
//attributesRequest.setAttributeNames();
attributes = client.getQueueAttributes(attributesRequest).
getAttributes();
numberOfMessages = Integer.valueOf(attributes.get(
"ApproximateNumberOfMessages")).intValue();
}catch (AmazonServiceException ase){
ase.printStackTrace();
}catch (AmazonClientException ace) {
ace.printStackTrace();
}
因为添加属性名称的 Java AmazonSQS 实现需要作为集合,所以我不明白如何正确添加“ApproximateNumberOfMessages”。
我也很好奇是否有更好的选择
new AmazonSQSClient(new ClasspathPropertiesFileCredentialsProvider());
更接近 C# 实现?原因是此方法旨在用作 SDK 的一部分,而 AWSAccessKey 和 AWSSecretAccessKey 将成为单独配置文件的一部分。创建自己的 AWSCredentialsProvider 是我唯一的选择吗?