将 Java 消息服务与其他 Amazon SQS 客户端一起使用 - Amazon Simple Queue Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

将 Java 消息服务与其他 Amazon SQS 客户端一起使用

使用带软件开发工具包的亚马逊 SQS Java 消息服务 (JMS) 客户端将亚马逊 Amazon SQS 消息大小限制为 256 KB。不过,您可以使用任何 Amazon SQS 客户端创建 JMS 提供程序。例如,可以使用 JMS 客户端与适用于 Java 的 Amazon SQS 扩展型客户端库来发送包含对 Amazon S3 中的消息负载(最大为 2 GB)的引用的 Amazon SQS 消息。有关更多信息,请参阅 使用 Java 和 Amazon S3 管理大型亚马逊 SQS 消息

以下 Java 代码示例为扩展客户端库创建 JMS 提供程序。

在测试此示例使用 JMS 和 Amazon SQS 的先决条件之前,请参阅中的先决条件。

AmazonS3 s3 = new AmazonS3Client(credentials); Region s3Region = Region.getRegion(Regions.US_WEST_2); s3.setRegion(s3Region); // Set the Amazon S3 bucket name, and set a lifecycle rule on the bucket to // permanently delete objects a certain number of days after each object's creation date. // Next, create the bucket, and enable message objects to be stored in the bucket. BucketLifecycleConfiguration.Rule expirationRule = new BucketLifecycleConfiguration.Rule(); expirationRule.withExpirationInDays(14).withStatus("Enabled"); BucketLifecycleConfiguration lifecycleConfig = new BucketLifecycleConfiguration().withRules(expirationRule); s3.createBucket(s3BucketName); s3.setBucketLifecycleConfiguration(s3BucketName, lifecycleConfig); System.out.println("Bucket created and configured."); // Set the SQS extended client configuration with large payload support enabled. ExtendedClientConfiguration extendedClientConfig = new ExtendedClientConfiguration() .withLargePayloadSupportEnabled(s3, s3BucketName); AmazonSQS sqsExtended = new AmazonSQSExtendedClient(new AmazonSQSClient(credentials), extendedClientConfig); Region sqsRegion = Region.getRegion(Regions.US_WEST_2); sqsExtended.setRegion(sqsRegion);

以下 Java 代码示例将创建连接工厂:

// Create the connection factory using the environment variable credential provider. // Pass the configured Amazon SQS Extended Client to the JMS connection factory. SQSConnectionFactory connectionFactory = new SQSConnectionFactory( new ProviderConfiguration(), sqsExtended ); // Create the connection. SQSConnection connection = connectionFactory.createConnection();