AWS SDK for Javaを使用して、AWS Pinpointのエンドポイント取得処理を実装したため内容を記載する。

環境構築

以下を参照する。

Amazon Pinpoint エンドポイント一覧取得処理

参考: https://docs.aws.amazon.com/ja_jp/pinpoint/latest/developerguide/audience-data-endpoints.html

AWS SDK for Javaの依存関係を追加

プロジェクトのpom.xmlファイルを開き、AWS SDK for Javaの依存関係を追加する。

Mavenプロジェクトで外部依存関係を含む実行可能なJARファイルを作成するには、maven-assembly-pluginまたはmaven-shade-pluginのいずれかを使用する必要がある。これにより、すべての依存関係が含まれた単一のJARファイルが作成される。

以下は、pom.xmlmaven-assembly-pluginを追加する例

<project ...>
  <dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>software.amazon.awssdk</groupId>
              <artifactId>bom</artifactId>
              <version>2.20.45</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- AWS SDK for Java -->
    <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>bom</artifactId>
      <version>2.20.45</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>pinpointsmsvoice</artifactId>
    </dependency>
    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>pinpoint</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.30</version>
    </dependency>
  </dependencies>
  <build>
  <plugins>
      <!-- maven-assembly-plugin -->
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>3.3.0</version>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.example.App</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
</project>

App.javaに処理を記述する

package com.example;

import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.pinpoint.PinpointClient;
import software.amazon.awssdk.services.pinpoint.model.EndpointResponse;
import software.amazon.awssdk.services.pinpoint.model.GetUserEndpointsRequest;
import software.amazon.awssdk.services.pinpoint.model.GetUserEndpointsResponse;
import software.amazon.awssdk.services.pinpoint.model.PinpointException;
import java.util.List;

public class App 
{
    public static void main(String[] args) {

        final String usage = "\n" +
            "Usage: " +
            "   <applicationId> <userId>\n\n" +
            "Where:\n" +
            "   applicationId - The ID of the Amazon Pinpoint application that has the endpoint.\n" +
            "   userId - The user id applicable to the endpoints";

        if (args.length != 2) {
            System.out.println(usage);
           System.exit(1);
        }

        String applicationId = args[0];
        String userId = args[1];
        PinpointClient pinpoint = PinpointClient.builder()
            .region(Region.AP_NORTHEAST_1)
            .credentialsProvider(ProfileCredentialsProvider.create())
            .build();

        listAllEndpoints(pinpoint, applicationId, userId );
        pinpoint.close();
    }

    //snippet-start:[pinpoint.java2.list_endpoints.main]
    public static void listAllEndpoints(PinpointClient pinpoint, String applicationId, String userId) {

        try {
            GetUserEndpointsRequest endpointsRequest = GetUserEndpointsRequest.builder()
                .userId(userId)
                .applicationId(applicationId)
                .build();

            GetUserEndpointsResponse response = pinpoint.getUserEndpoints(endpointsRequest);
            List<EndpointResponse> endpoints = response.endpointsResponse().item();

            // Display the results.
            for (EndpointResponse endpoint: endpoints) {
                System.out.println("The channel type is: "+endpoint.channelType());
                System.out.println("The address is  "+endpoint.address());
            }

        } catch ( PinpointException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
}

mvn clean packageを実行する。

これによりtargetディレクトリにexample-java-1.0-SNAPSHOT-jar-with-dependencies.jarという名前のJARファイルが作成される。その後、以下のコマンドでアプリケーションを実行する。

java -jar target/example-java-1.0-SNAPSHOT-jar-with-dependencies.jar ${ApplicationId} ${userId}

出力結果: Amazon Pinpoint のエンドポイント一覧を取得する事ができた。

The channel type is: APNS_SANDBOX
The address is  xxxxx
The channel type is: GCM
The address is  xxxxx
The channel type is: APNS_SANDBOX
The address is  xxxxx
The channel type is: APNS_SANDBOX
The address is  xxxxx
The channel type is: GCM
The address is  xxxxx
The channel type is: GCM
The address is  xxxxx
The channel type is: GCM
The address is  xxxxx
The channel type is: GCM
The address is  xxxxx
カテゴリー: Java