serverless trigger

master
yixian 5 years ago
parent 082a9d2d31
commit 9f389e322e

@ -14,8 +14,10 @@ import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.server.ResponseStatusException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
/**
* Create by davep at 2019-08-12 9:31
@ -35,6 +37,8 @@ public class QCloudServerlessTrigger implements ServerlessFunctionTrigger {
this.secretKey = secretKey;
this.region = region;
this.restTemplate = new RestTemplateBuilder()
.setConnectTimeout(Duration.ofMinutes(5))
.setReadTimeout(Duration.ofMinutes(5))
.messageConverters(new StringHttpMessageConverter(StandardCharsets.UTF_8))
.build();
}
@ -44,18 +48,25 @@ public class QCloudServerlessTrigger implements ServerlessFunctionTrigger {
public JSONObject triggerFunction(String function, JSONObject params) {
ServerLessFunctionInvokeRequest request = new ServerLessFunctionInvokeRequest(secretId, secretKey, region, function, params);
ResponseEntity<String> resp = restTemplate.exchange(request.request(), String.class);
JSONObject data = JSON.parseObject(resp.getBody());
JSONObject response = data.getJSONObject("Response");
String requestId = response.getString("RequestId");
JSONObject result = response.getJSONObject("Result");
int invokeResult = result.getIntValue("InvokeResult");
if (invokeResult != 0) {
String errMsg = result.getString("ErrMsg");
logger.error("Invoking function [{}] failed,request id={},invoke result={},errMsg={}", function, requestId, invokeResult, errMsg);
throw new ServerErrorException("Invoke function " + function + " failed:[" + errMsg + "]");
if (resp.getStatusCode().is2xxSuccessful()) {
JSONObject data = JSON.parseObject(resp.getBody());
if (data == null) {
throw new ServerErrorException("Invoke function " + function + " failed:[no response]");
}
JSONObject response = data.getJSONObject("Response");
String requestId = response.getString("RequestId");
JSONObject result = response.getJSONObject("Result");
int invokeResult = result.getIntValue("InvokeResult");
if (invokeResult != 0) {
String errMsg = result.getString("ErrMsg");
logger.error("Invoking function [{}] failed,request id={},invoke result={},errMsg={}", function, requestId, invokeResult, errMsg);
throw new ServerErrorException("Invoke function " + function + " failed:[" + errMsg + "]");
} else {
String retMsg = result.getString("RetMsg");
return StringUtils.isNotEmpty(retMsg) ? JSON.parseObject(retMsg) : null;
}
} else {
String retMsg = result.getString("RetMsg");
return StringUtils.isNotEmpty(retMsg) ? JSON.parseObject(retMsg) : null;
throw new ResponseStatusException(resp.getStatusCode(), "Invoke function " + function + " failed:[no response]");
}
}

Loading…
Cancel
Save