Tăng Google Analytics API Đọc hết thời gian, chuyển đổi mã java thành coldfusion
Câu hỏi ngắn: Làm cách nào để chuyển đổi mã java sau sang mã ColdFusion?
private static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) {
return new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
requestInitializer.initialize(httpRequest);
httpRequest.setConnectTimeout(3 * 60000); // 3 minutes connect timeout
httpRequest.setReadTimeout(3 * 60000); // 3 minutes read timeout
}};
}
Giải thích: Tôi sử dụng một thành phần để giao tiếp với API google analytics. Điều này hoạt động tốt tuy nhiên đôi khi xảy ra thời gian chờ đọc. Tôi muốn tăng cường kết nối và hết thời gian đọc. Tôi đã tìm thấy một bài đăng ở đây trên StackOverflow giải thích cách thực hiện việc này trong Java (https://stackoverflow.com/a/30721185/2482184). Tuy nhiên, tôi đang gặp sự cố khi chuyển đổi mã java sang mã ColdFusion. Bên dưới thành phần ColdFusion mà tôi hiện đang sử dụng để giao tiếp với API Google Analytics:
<cfcomponent displayname="cfanalyticsConnector" output="false">
<cffunction name="init" access="public" output="false" returntype="cfanalyticsConnector">
<cfargument name="serviceAccountId" type="string" required="true" />
<cfargument name="pathToKeyFile" type="string" required="true" />
<cfargument name="analyticsAppName" type="string" required="true" />
<cfscript>
variables.serviceAccountId = arguments.serviceAccountId;
variables.pathToKeyFile = arguments.pathToKeyFile;
variables.analyticsAppName = arguments.analyticsAppName;
variables.HTTP_Transport = createObject("java", "com.google.api.client.http.javanet.NetHttpTransport").init();
variables.JSON_Factory = createObject("java", "com.google.api.client.json.jackson2.JacksonFactory").init();
variables.HTTP_Request_Initializer = createObject("java", "com.google.api.client.http.HttpRequestInitializer");
variables.Credential_Builder = createObject("java", "com.google.api.client.googleapis.auth.oauth2.GoogleCredential$Builder"); variables.Analytics_Scopes = createObject("java", "com.google.api.services.analytics.AnalyticsScopes"); variables.Analytics_Builder = createObject("java", "com.google.api.services.analytics.Analytics$Builder").init(
variables.HTTP_Transport,
variables.JSON_Factory,
javaCast("null", ""));
variables.Collections = createObject("java", "java.util.Collections");
variables.File_Obj = createObject("java", "java.io.File");
variables.credential = "";
variables.analytics = "";
</cfscript>
<cfreturn this />
</cffunction>
<cffunction name="buildAnalytics" access="public" output="true" returntype="struct" hint="creates analytics object">
<cfset local = {} />
<cfset local.credential = "" />
<cfset local.returnStruct = {} />
<cfset local.returnStruct.success = true />
<cfset local.returnStruct.error = "" />
<!--- Access tokens issued by the Google OAuth 2.0 Authorization Server expire in one hour.
When an access token obtained using the assertion flow expires, then the application should
generate another JWT, sign it, and request another access token.
https://developers.google.com/accounts/docs/OAuth2ServiceAccount --->
<cftry>
<cfset local.credential = Credential_Builder
.setTransport(variables.HTTP_Transport)
.setJsonFactory(variables.JSON_Factory)
.setServiceAccountId(variables.serviceAccountId)
.setServiceAccountScopes(Collections.singleton(variables.Analytics_Scopes.ANALYTICS_READONLY))
.setServiceAccountPrivateKeyFromP12File(variables.File_Obj.Init(variables.pathToKeyFile))
.build() />
<cfcatch type="any">
<cfset local.returnStruct.error = "Credential Object Error: " & cfcatch.message & " - " & cfcatch.detail />
<cfset local.returnStruct.success = false />
</cfcatch>
</cftry>
<cfif local.returnStruct.success>
<cftry>
<cfset variables.analytics = variables.Analytics_Builder
.setApplicationName(variables.analyticsAppName)
.setHttpRequestInitializer(local.credential)
.build() />
<cfcatch type="any">
<cfset local.returnStruct.error = "Analytics Object Error: " & cfcatch.message & " - " & cfcatch.detail />
<cfset local.returnStruct.success = false />
</cfcatch>
</cftry>
</cfif>
<cfreturn local.returnStruct />
</cffunction>
</cfcomponent>
Mã java giải thích cách tăng kết nối và hết thời gian đọc:
private static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) {
return new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
requestInitializer.initialize(httpRequest);
httpRequest.setConnectTimeout(3 * 60000); // 3 minutes connect timeout
httpRequest.setReadTimeout(3 * 60000); // 3 minutes read timeout
}};
}
public static Analytics initializeAnalytics() throws Exception {
// Initializes an authorized analytics service object.
// Construct a GoogleCredential object with the service account email
// and p12 file downloaded from the developer console.
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountPrivateKeyFromP12File(new File(KEY_FILE_LOCATION))
.setServiceAccountScopes(AnalyticsScopes.all())
.build();
// Construct the Analytics service object.
return new Analytics.Builder(httpTransport, JSON_FACTORY,setHttpTimeout(credential))
.setApplicationName(APPLICATION_NAME).build();
}
Nếu tôi hiểu đúng, tôi cần thay đổi dòng mã này:
<cfset variables.analytics = variables.Analytics_Builder
.setApplicationName(variables.analyticsAppName)
.setHttpRequestInitializer(local.credential)
.build() />
Đối với một cái gì đó như:
<cfset variables.analytics = createObject("java",
"com.google.api.services.analytics.Analytics$Builder").init(
variables.HTTP_Transport,
variables.JSON_Factory,
setHttpTimeout(local.credential)
)
.setApplicationName(variables.analyticsAppName)
.setHttpRequestInitializer(local.credential)
.build() />
>
Howerer Tôi đã không thành công trong việc che đậy hàm java setHttpTimeout thành coldfusion. Mọi sự trợ giúp sẽ rất được trân trọng.
Trả lời
Để tạo thời gian chờ yêu cầu 3 phút, bạn có thể gọi dòng mã này trước khi thực hiện yêu cầu http của mình.
Định dạng thẻ
<cfsetting RequestTimeout = 3 * 60>
Định dạng CFScript
setting requesttimeout = 3 * 60;