【发布时间】:2018-07-28 17:03:29
【问题描述】:
我正在尝试编写一个 lambda 函数,该函数从 lex 输入中获取信息,并调用一个 rest api,将该信息作为参数传递,返回一个字符串,然后我想将其作为响应发送给 lex。它在 Eclipse 中运行时按预期工作,但是当我将 jar 上传到 amazon lambda 时,它没有给出错误,但输出字符串为 null。
public class LambdaFunctionHandler implements RequestHandler<Map<String,Object>, Object> {
@Override
public Object handleRequest(Map<String,Object> input, Context context) {
ValidationHook.LexRequest lexRequest= LexRequestFactory.createLexRequest(input);
String orgbotcommand = lexRequest.getCommand()+"%20"+lexRequest.getType()+"%20"+lexRequest.getNew_variable();
lexRequest.setOrgbotcommand(orgbotcommand);
try {
URL url = new URL("http://localhost:8080/mindtuit/execute?command="+orgbotcommand);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
lexRequest.setResponse(output);
}
System.out.println(lexRequest.getResponse());
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String content = String.format("command recieved by %s is %s,response is %s ",lexRequest.getBotName(),lexRequest.getOrgbotcommand(),lexRequest.getResponse());
Message message = new Message("PlainText",content);
DialogAction dialogAction = new DialogAction("Close", "Fulfilled", message );
System.out.println(dialogAction);
return new LexRespond(dialogAction);
}
}
【问题讨论】:
-
您是否在将 URL 上传到 Lambda 之前更改了它?因为它在您的示例中指向 localhost。如果您正在运行本地 API,则必须将其公开(例如,再次使用 Lambda 或其他服务,例如 EC2)。
-
@s.hesse 你是对的,但这里可能不是这样,因为它已经被处理过了。如果您可以分享,@Kanika 日志可能会有所帮助。
-
@s.hesse 我在 EC2 上托管了我的 api,它与 lex 一起使用。非常感谢 :) 。还有一件事,单次调用的计算时间非常长(58 秒)。有没有其他方法(除了增加计算资源)来提高效率
-
@kanika 你找到了吗,为什么要花这么多时间,而你的解决方法只是更改主机名是吗?
-
@KanikaAgarwal 也许包括预热时间?
标签: java rest aws-lambda amazon-lex