本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
将 Java Message Service 客户端与其他 Amazon SQS 客户端配合使用
配合使用 Amazon SQS Java Message Service (JMS) 客户端与 Amazon SDK,会将 Amazon SQS 消息大小限制为 256 KB。不过,您可以使用任何 Amazon SQS 客户端创建 JMS 提供程序。例如,可以使用 JMS 客户端与适用于 Java 的 Amazon SQS 扩展型客户端库来发送包含对 Amazon S3 中的消息负载(最大为 2 GB)的引用的 Amazon SQS 消息。有关更多信息,请参阅 使用 Java 和 Amazon S3 管理大型 Amazon 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();