

AWS SDK for JavaScript v2가 지원 종료에 도달했습니다. [AWS SDK for JavaScript v3](https://docs.aws.amazon.com//sdk-for-javascript/v3/developer-guide/)로 마이그레이션하실 것을 권장합니다. 마이그레이션 방법에 대한 자세한 내용은 해당 [공지 사항](https://aws.amazon.com/blogs//developer/announcing-end-of-support-for-aws-sdk-for-javascript-v2/)을 참조하세요.

# Amazon SQS에서 긴 폴링 활성화
<a name="sqs-examples-enable-long-polling"></a>

![\[JavaScript code example that applies to Node.js execution\]](http://docs.aws.amazon.com/ko_kr/sdk-for-javascript/v2/developer-guide/images/nodeicon.png)

**이 Node.js 코드 예제는 다음을 보여 줍니다.**
+ 새로 생성된 대기열에 대해 긴 폴링을 활성화하는 방법
+ 기존 대기열에 대해 긴 폴링을 활성화하는 방법
+ 메시지를 수신할 때 긴 폴링을 활성화하는 방법.

## 시나리오
<a name="sqs-examples-enable-long-polling-scenario"></a>

긴 폴링은 응답을 전송하기 전에 대기열에서 메시지를 사용할 수 있을 때까지 Amazon SQS를 지정된 시간 동안 대기시켜 빈 응답의 개수를 줄입니다. 또한, 긴 폴링은 서버의 샘플링 대신에 모든 서버를 쿼리하여 False인 빈 응답을 제거합니다. 긴 폴링을 활성화하려면 수신된 메시지에 0이 아닌 대기 시간을 지정해야 합니다. 이렇게 하려면 대기열의 `ReceiveMessageWaitTimeSeconds` 파라미터를 설정하거나 메시지가 수신되었을 때 메시지에서 `WaitTimeSeconds` 파라미터를 설정하면 됩니다.

이 예제에서는 일련의 Node.js 모듈을 사용하여 긴 폴링을 활성화합니다. Node.js 모듈은 SDK for JavaScript로 `AWS.SQS` 클라이언트 클래스의 다음 메서드를 사용하여 긴 폴링을 활성화합니다.
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#setQueueAttributes-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#setQueueAttributes-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#receiveMessage-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#receiveMessage-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#createQueue-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#createQueue-property)

Amazon SQS 긴 폴링에 대한 자세한 내용은 *Amazon Simple Queue Service 개발자 안내서*의 [긴 폴링](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html)을 참조하세요.

## 사전 필수 작업
<a name="sqs-examples-enable-long-polling-prerequisites"></a>

이 예제를 설정하고 실행하려면 먼저 이러한 작업들을 완료해야 합니다.
+ Node.js를 설치합니다. Node.js 설치에 대한 자세한 내용은 [Node.js 웹 사이트](https://nodejs.org)를 참조하세요.
+ 사용자 자격 증명을 사용하여 공유 구성 파일을 생성합니다. 공유 자격 증명 파일 제공에 대한 자세한 내용은 [공유 인증 자격 증명 파일에서 Node.js에 인증 자격 증명 로드](loading-node-credentials-shared.md) 섹션을 참조하세요.

## 대기열 생성 시 긴 폴링 활성화
<a name="sqs-examples-enable-long-polling-on-queue-creation"></a>

파일 이름이 `sqs_longpolling_createqueue.js`인 Node.js 모듈을 생성합니다. 위와 같이 SDK를 구성해야 합니다. Amazon SQS에 액세스하려면 `AWS.SQS` 서비스 객체를 생성합니다. `ReceiveMessageWaitTimeSeconds` 파라미터의 0이 아닌 값을 포함하여 대기열을 생성하는 데 필요한 파라미터를 포함하는 JSON 객체를 생성합니다. `createQueue` 메서드를 호출합니다. 그러면 대기열에 대해 긴 폴링이 활성화됩니다.

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create the SQS service object
var sqs = new AWS.SQS({ apiVersion: "2012-11-05" });

var params = {
  QueueName: "SQS_QUEUE_NAME",
  Attributes: {
    ReceiveMessageWaitTimeSeconds: "20",
  },
};

sqs.createQueue(params, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data.QueueUrl);
  }
});
```

예제를 실행하려면 명령줄에서 다음을 입력합니다.

```
node sqs_longpolling_createqueue.js
```

이 샘플 코드는 [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/sqs/sqs_longpolling_createqueue.js)에서 찾을 수 있습니다.

## 기존 대기열에 대해 긴 폴링 활성화
<a name="sqs-examples-enable-long-polling-existing-queue"></a>

파일 이름이 `sqs_longpolling_existingqueue.js`인 Node.js 모듈을 생성합니다. 위와 같이 SDK를 구성해야 합니다. Amazon Simple Queue Service에 액세스하려면 `AWS.SQS` 서비스 객체를 생성합니다. `ReceiveMessageWaitTimeSeconds` 파라미터의 0이 아닌 값 및 대기열의 URL을 포함하여 대기열의 속성을 설정하는 데 필요한 파라미터를 포함하는 JSON 객체를 생성합니다. `setQueueAttributes` 메서드를 호출합니다. 그러면 대기열에 대해 긴 폴링이 활성화됩니다.

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create the SQS service object
var sqs = new AWS.SQS({ apiVersion: "2012-11-05" });

var params = {
  Attributes: {
    ReceiveMessageWaitTimeSeconds: "20",
  },
  QueueUrl: "SQS_QUEUE_URL",
};

sqs.setQueueAttributes(params, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});
```

예제를 실행하려면 명령줄에서 다음을 입력합니다.

```
node sqs_longpolling_existingqueue.js
```

이 샘플 코드는 [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/sqs/sqs_longpolling_existingqueue.js)에서 찾을 수 있습니다.

## 메시지 수신 시 긴 폴링 활성화
<a name="sqs-examples-enable-long-polling-on-receive-message"></a>

파일 이름이 `sqs_longpolling_receivemessage.js`인 Node.js 모듈을 생성합니다. 위와 같이 SDK를 구성해야 합니다. Amazon Simple Queue Service에 액세스하려면 `AWS.SQS` 서비스 객체를 생성합니다. `WaitTimeSeconds` 파라미터의 0이 아닌 값 및 대기열의 URL을 포함하여 메시지를 수신하는 데 필요한 파라미터를 포함하는 JSON 객체를 생성합니다. `receiveMessage` 메서드를 호출합니다.

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create the SQS service object
var sqs = new AWS.SQS({ apiVersion: "2012-11-05" });

var queueURL = "SQS_QUEUE_URL";

var params = {
  AttributeNames: ["SentTimestamp"],
  MaxNumberOfMessages: 1,
  MessageAttributeNames: ["All"],
  QueueUrl: queueURL,
  WaitTimeSeconds: 20,
};

sqs.receiveMessage(params, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});
```

예제를 실행하려면 명령줄에서 다음을 입력합니다.

```
node sqs_longpolling_receivemessage.js
```

이 샘플 코드는 [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/sqs/sqs_longpolling_receivemessage.js)에서 찾을 수 있습니다.