【发布时间】:2018-03-25 18:06:48
【问题描述】:
我在 Amazon 的 Lambda 服务上上传了以下 Java 类:
public class DevicePutHandler implements RequestHandler<DeviceRequest, Device> {
private static final Logger log = Logger.getLogger(DevicePutHandler.class);
public Device handleRequest(DeviceRequest request, Context context) {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
DynamoDBMapper mapper = new DynamoDBMapper(client);
if (request == null) {
log.info("The request had a value of null.");
return null;
}
log.info("Retrieving device");
Device deviceRetrieved = mapper.load(Device.class, request.getDeviceId());
log.info("Updating device properties");
deviceRetrieved.setBuilding(request.getBuilding());
deviceRetrieved.setMotionPresent(request.getMotionPresent());
mapper.save(deviceRetrieved);
log.info("Updated device has been saved");
return deviceRetrieved;
}
}
我还有一个执行角色集,可以让我完全控制 DynamoDB。我的权限应该完全没问题,因为我在其他以这种方式使用 Lambda 和 DynamoDB 的项目中使用了完全相同的权限(唯一的区别是不同的请求类型)。
这个类的目的是让它被 API Gateway (API Gateway -> Lambda -> DynamoDB) 调用,但现在我只是想在 Lambda (Lambda -> DynamoDB) 上测试它。
作为参考,以防万一,这里是 DeviceRequest 类:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "deviceId", "building", "motionPresent" })
public class DeviceRequest {
@JsonProperty("deviceId")
private String deviceId;
@JsonProperty("building")
private String building;
@JsonProperty("motionPresent")
private Boolean motionPresent;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("deviceId")
public String getDeviceId() {
return deviceId;
}
@JsonProperty("deviceId")
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
@JsonProperty("building")
public String getBuilding() {
return building;
}
@JsonProperty("building")
public void setBuilding(String building) {
this.building = building;
}
@JsonProperty("motionPresent")
public Boolean getMotionPresent() {
return motionPresent;
}
@JsonProperty("motionPresent")
public void setMotionPresent(Boolean motionPresent) {
this.motionPresent = motionPresent;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
这里是设备类:
@DynamoDBTable(tableName="DeviceTable")
public class Device {
private String deviceID;
private String building;
private String queue;
private boolean motionPresent;
@DynamoDBHashKey(attributeName="Device ID")
public String getDeviceID() {
return deviceID;
}
public void setDeviceID(String deviceID) {
this.deviceID = deviceID;
}
@DynamoDBAttribute(attributeName="Motion Present")
public boolean getMotionPresent() {
return motionPresent;
}
public void setMotionPresent(boolean motionPresent) {
this.motionPresent = motionPresent;
}
@DynamoDBAttribute(attributeName="Building")
public String getBuilding() {
return this.building;
}
public void setBuilding(String building) {
this.building = building;
}
@DynamoDBAttribute(attributeName="Queue")
public String getQueue() {
return this.queue;
}
public void setQueue(String queue) {
this.queue = queue;
}
}
这是我尝试用来测试 Lambda 的 JSON 输入:
{
"deviceId": "test_device_name",
"building": "building1",
"motionPresent": false
}
不会引发任何异常(我尝试将其包装在 try/catch 块周围),并且 lambda 超时是唯一发生的事情。在初始化 DynamoDB 客户端之前,我一开始就尝试使用 log/print 语句来查看是否可以正确读取请求,并且它似乎可以正确解析 JSON 字段。我也把clientbuilder分离出来,发现builder对象是可以初始化的,但是超时来自builder调用build()做client的时候。
如果有人知道为什么会发生这种超时,请告诉我!
【问题讨论】:
-
超过了 Lambda 超时?还是在没有对 DynamoDB 的出站 Internet 访问(或 VPC 终端节点)的 VPC 中运行?
-
错误只是说“任务在 3.00 秒后超时”。如果我更改超时时间,它将一直运行,直到超过我设置的值。我没有在 VPC 中配置任何东西。
标签: java amazon-web-services amazon-dynamodb aws-lambda