亚马逊Notification sqs-跟卖监控示例

官方文档说明如下:在这里插入图片描述

STEP 1 创建SQS

1.1 创建队列
在这里插入图片描述
1.2 设置基本属性
根据需求配置队列属性在这里插入图片描述
1.3 设置默认访问策略
访问策略选择默认,创建结束后再修改在这里插入图片描述

STEP 2 配置SQS (允许amazon 发送消息)

2.1 编辑队列
在队列创建完成后, 记录arn,并点击编辑
在这里插入图片描述
2.2 修改策略
记录当前队列的root Principal,点击策略生成器(此时,我们已经拿到了自身的ARN, 和自身的 Principal)在这里插入图片描述
2.3 自身拥有队列的所有权限在这里插入图片描述
2.4 Amazon拥有 GetQueueAttributes 和 SendMessage 权限
Amazon的为Principal (固定的): arn:aws:iam::437568002678:root在这里插入图片描述
2.5 生成配置在这里插入图片描述
2.6 复制配置JSON在这里插入图片描述
2.7 替换配置JSON在这里插入图片描述
2.8 点击保存
至此,我们拥有了一个队列,并且,该队列允许亚马逊发送消息,开发者账号拥有该队列所有权限

STEP 3 监听队列

3.1 pom依赖

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-sqs</artifactId>
            <version>1.11.106</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>amazon-sqs-java-messaging-lib</artifactId>
            <version>1.0.4</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>sqs</artifactId>
            <version>2.17.46</version>
        </dependency>

3.2 监听代码

@Component
@Slf4j
public class SqsConsumer {
    @JmsListener(destination = "SQS名称")
    public void consumer(SQSTextMessage sqsTestMessage) throws Exception {
        String text = sqsTestMessage.getText();
        System.out.print("接收到SQS通知:" + text);
    }
}

3.3 SqsConfig

@Configuration
@EnableJms
public class SqsConfig {
    @Value("${accessKeyId}")
    private String accessKeyId;
    @Value("${secretKey}")
    private String secretKey;

    SQSConnectionFactory connectionFactory = null;

    AmazonSQSClientBuilder amazonSQSClientBuilder = null;

    @Bean
    public SqsClient getSqsClient() {
        SqsClientBuilder sqsClientBuilder = SqsClient.builder().
//us-east-2为SQS所做区域
                region(Region.of("us-east-2")).credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("" +
                accessKeyId, secretKey)));
        return sqsClientBuilder.build();
    }

    public AmazonSQSClientBuilder getAmazonSQSClientBuilder() {
        if (amazonSQSClientBuilder != null) {
            return amazonSQSClientBuilder;
        }
        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKeyId, secretKey);
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeout(3000);
        clientConfiguration.setProtocol(Protocol.HTTP);
        clientConfiguration.useGzip();
        clientConfiguration.useTcpKeepAlive();
        AmazonSQSClientBuilder amazonSQSClientBuilder = AmazonSQSClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .withClientConfiguration(clientConfiguration)
//us-east-2为SQS所做区域
                .withRegion("us-east-2");
        return amazonSQSClientBuilder;
    }

    public SQSConnectionFactory getConnectionFactory() {
        if (connectionFactory != null) {
            return connectionFactory;
        }
        connectionFactory = new SQSConnectionFactory(new ProviderConfiguration(), getAmazonSQSClientBuilder());
        return connectionFactory;
    }

    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(this.getConnectionFactory());
        factory.setDestinationResolver(new DynamicDestinationResolver());
        factory.setConcurrency("3-10");
        /**
         *  SESSION_TRANSACTED
         *  CLIENT_ACKNOWLEDGE : After the client confirms, the client must call the acknowledge method of javax.jms.message after receiving the message. After the confirmation, the JMS server will delete the message
         *  AUTO_ACKNOWLEDGE : Automatic acknowledgment, no extra work required for client to send and receive messages
         *  DUPS_OK_ACKNOWLEDGE : Allow the confirmation mode of the replica. Once a method call from the receiving application returns from the processing message, the session object acknowledges the receipt of the message; and duplicate acknowledgments are allowed. This pattern is very effective when resource usage needs to be considered.
         */
        factory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
        factory.setSessionTransacted(false);
        return factory;
    }
}

STEP 4 跟卖订阅消息

调用NotificationsApi中的createDestination和createSubscription接口,创建订阅。

看一下Amazon官方的定义:
ANY_OFFER_CHANGED
Sellers can subscribe to this notification.
The ANY_OFFER_CHANGED notification is sent whenever there is a change to any of the top 20 offers, by condition (new or used), or if the external price (the price from other retailers) changes for an item that you sell, or if there is a change to which offer wins the BuyBox, or a change to the BuyBox price. The top 20 offers are determined by the landed price, which is the price plus shipping minus Amazon Points. If multiple sellers are charging the same landed price, the results will be returned in random order.
You will only receive ANY_OFFER_CHANGED notifications for items for which you have active offers. You cannot subscribe to notifications for items for which you do not have active offers.
在这里插入图片描述
4.1 创建 createDestination
注意:
在请求参数中: resourceSpecification下的 sqs 和 eventBridge 只能二选一,并且必须选一个,在SQS中,我们填写SQS就行
访问createDestination时,由于Destination是开发者的内容,与卖家没有关系,因此,在ApiClient对象的创建上,需要使用scope模式,scope = “sellingpartnerapi::notifications”

LWAClientScopes lwaClientScopes = new LWAClientScopes(new HashSet<>());
            lwaClientScopes.getScopes().add("sellingpartnerapi::notifications");
            LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder()
                    .clientId(clientId)
                    .clientSecret(clientSecret)
                    .scopes(lwaClientScopes)
                    .endpoint(AmazonAdvertUrlConstants.AUTH_TOKEN_NA_URL)
                    .build();

            NotificationsApi notificationsApi = new NotificationsApi.Builder()
                    .awsAuthenticationCredentialsProvider(commonBiz.awsAuthenticationCredentialsProviderBuilder().build())
                    .awsAuthenticationCredentials(commonBiz.awsAuthenticationCredentials().build())
                    .lwaAuthorizationCredentials(lwaAuthorizationCredentials)
                    .endpoint(AmazonSalesUrlConstants.AMAZON_SELLINGPARTNERAPI_NA_URL)
                    .build();
            DestinationResourceSpecification destinationResourceSpecification = new DestinationResourceSpecification();
            SqsResource sqsResource = new SqsResource();
            sqsResource.setArn("SQS的ARN");
            destinationResourceSpecification.setSqs(sqsResource);

            CreateDestinationRequest body = new CreateDestinationRequest();
            body.setResourceSpecification(destinationResourceSpecification);
            body.setName("SQS的名称");
            CreateDestinationResponse createDestinationResponse = notificationsApi.createDestination(body);
            return success(createDestinationResponse);

请求参数示例:

body
{
  "resourceSpecification": {
    "sqs": {
      "arn": "arn:aws:sqs:us-west-1:手动马赛克:TEST"
    }
  },
  "name": "testDestination"
}

返回参数:

{
  "payload": {
    "name": "testDestination",
    "destinationId": "2d6eee67-23e9-40fe-destinationId",
    "resource": {
      "sqs": {
        "arn": "arn:aws:sqs:us-west-1:手动马赛克:TEST"
      }
    }
  }
}

需要记录下返回结果中的: destinationId

4.2 根据Destination创建订阅

NotificationsApi notificationsApi = new NotificationsApi.Builder()
                    .awsAuthenticationCredentialsProvider(commonBiz.awsAuthenticationCredentialsProviderBuilder().build())
                    .awsAuthenticationCredentials(commonBiz.awsAuthenticationCredentials().build())
                    .lwaAuthorizationCredentials(commonBiz.lwaAuthorizationCredentials(refreshToken.toString()))
                    .endpoint(AmazonSalesUrlConstants.AMAZON_SELLINGPARTNERAPI_NA_URL)
                    .build();
            CreateSubscriptionRequest request = new CreateSubscriptionRequest();
            request.setDestinationId("4.1中的destinationId");
            request.setPayloadVersion("1.0");
            CreateSubscriptionResponse createDestinationResponse = notificationsApi.createSubscription(request, "ANY_OFFER_CHANGED
");

返回参数:

{
  "payload": {
    "subscriptionId":"7ca3572b-2130-41eb-subscriptionId",
    "payloadVersion":"1.0",
    "destinationId": "2d6eee67-23e9-40fe-destinationId",
  }
}

已经订阅成功,等待接收消息。

STEP 5 跟卖监控消息解读

{
    "AnyOfferChangedNotification" : {
      "SellerId" : "卖家id",
      "OfferChangeTrigger" : {
        "MarketplaceId" : "ATVPDKIKX0DER",
        "ASIN" : "asin",
        "ItemCondition" : "new",
        "TimeOfOfferChange" : "2022-03-03T09:59:38.071Z",
        "OfferChangeType" : "Internal"
      },
      "Summary" : {
        "NumberOfOffers" : [ {
          "Condition" : "new",
          "FulfillmentChannel" : "Amazon",
          "OfferCount" : 2
        }, {
          "Condition" : "new",
          "FulfillmentChannel" : "Merchant",
          "OfferCount" : 1
        } ],
        "LowestPrices" : [ {
          "Condition" : "new",
          "FulfillmentChannel" : "Amazon",
          "LandedPrice" : {
            "Amount" : 10.9,
            "CurrencyCode" : "USD"
          },
          "ListingPrice" : {
            "Amount" : 10.9,
            "CurrencyCode" : "USD"
          },
          "Shipping" : {
            "Amount" : 0.0,
            "CurrencyCode" : "USD"
          }
        }, {
          "Condition" : "new",
          "FulfillmentChannel" : "Merchant",
          "LandedPrice" : {
            "Amount" : 9.99,
            "CurrencyCode" : "USD"
          },
          "ListingPrice" : {
            "Amount" : 9.99,
            "CurrencyCode" : "USD"
          },
          "Shipping" : {
            "Amount" : 0.0,
            "CurrencyCode" : "USD"
          }
        } ],
        "BuyBoxPrices" : [ {
          "Condition" : "New",
          "LandedPrice" : {
            "Amount" : 10.9,
            "CurrencyCode" : "USD"
          },
          "ListingPrice" : {
            "Amount" : 10.9,
            "CurrencyCode" : "USD"
          },
          "Shipping" : {
            "Amount" : 0.0,
            "CurrencyCode" : "USD"
          }
        } ],
        "SalesRankings" : [ {
          "ProductCategoryId" : "xxx",
          "Rank" : 75687
        }, {
          "ProductCategoryId" : "xxx",
          "Rank" : 104
        } ],
        "NumberOfBuyBoxEligibleOffers" : [ {
          "Condition" : "new",
          "FulfillmentChannel" : "Amazon",
          "OfferCount" : 2
        }, {
          "Condition" : "new",
          "FulfillmentChannel" : "Merchant",
          "OfferCount" : 1
        } ]
      },
      "Offers" : [ {
        "SellerId" : "SellerId",
        "SubCondition" : "new",
        "SellerFeedbackRating" : {
          "FeedbackCount" : 87,
          "SellerPositiveFeedbackRating" : 89
        },
        "ShippingTime" : {
          "MinimumHours" : 24,
          "MaximumHours" : 48,
          "AvailabilityType" : "NOW",
          "AvailableDate" : ""
        },
        "ListingPrice" : {
          "Amount" : 9.99,
          "CurrencyCode" : "USD"
        },
        "Shipping" : {
          "Amount" : 0.0,
          "CurrencyCode" : "USD"
        },
        "ShipsFrom" : {
          "Country" : "CN",
          "State" : ""
        },
        "IsFulfilledByAmazon" : false,
        "IsBuyBoxWinner" : false,
        "PrimeInformation" : {
          "IsOfferPrime" : false,
          "IsOfferNationalPrime" : false
        },
        "IsFeaturedMerchant" : true,
        "ShipsDomestically" : true
      }, {
        "SellerId" : "SellerId",
        "SubCondition" : "new",
        "SellerFeedbackRating" : {
          "FeedbackCount" : 34,
          "SellerPositiveFeedbackRating" : 90
        },
        "ShippingTime" : {
          "MinimumHours" : 0,
          "MaximumHours" : 0,
          "AvailabilityType" : "NOW",
          "AvailableDate" : ""
        },
        "ListingPrice" : {
          "Amount" : 10.9,
          "CurrencyCode" : "USD"
        },
        "Shipping" : {
          "Amount" : 0.0,
          "CurrencyCode" : "USD"
        },
        "IsFulfilledByAmazon" : true,
        "IsBuyBoxWinner" : false,
        "PrimeInformation" : {
          "IsOfferPrime" : true,
          "IsOfferNationalPrime" : true
        },
        "IsFeaturedMerchant" : true,
        "ShipsDomestically" : true
      }]
    }
  }
}

Offers数组中,SellerId不是自己的SellerId就是别人对你商品的跟卖,通过https://www.amazon.com/sp?ie=UTF8&seller=SellerId可以对跟卖的店铺进行查看。
费用:所有客户每月都可免费发出 100 万个 Amazon SQS 请求,超过100w 每100w 收费 0.40 USD
几乎可以视为不花钱。。

配合亚马逊刊登api-跟卖,创建跟卖产品,然后进行监控。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老牛十八岁啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值