google Analytics APIの読み取りがタイムアウトし、Javaコードをcoldfusionに変換する
簡単な質問:次のJavaコードを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
}};
}
説明:コンポーネントを使用して、Google AnalyticsAPIと通信します。これはうまく機能しますが、読み取りタイムアウトが発生する場合があります。接続を増やしてタイムアウトを読み取りたいのですが。Javaでこれを行う方法を説明するStackOverflowの投稿をここで見つけました(https://stackoverflow.com/a/30721185/2482184)。ただし、JavaコードをColdFusionコードに変換するのに問題があります。現在GoogleAnalyticsAPIとの通信に使用しているColdFusionコンポーネントの下:
<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>
接続を増やしてタイムアウトを読み取る方法を説明するJavaコード:
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();
}
正しく理解している場合は、次のコード行を変更する必要があります。
<cfset variables.analytics = variables.Analytics_Builder
.setApplicationName(variables.analyticsAppName)
.setHttpRequestInitializer(local.credential)
.build() />
次のようなものに:
<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私はjavasetHttpTimeout関数をcoldfusionにカバーすることに成功していません。どんな助けでも大歓迎です。
回答
user12031119
3分のリクエストタイムアウトを作成するには、httpリクエストを行う前にこのコード行を呼び出すことができます。
タグ形式
<cfsetting RequestTimeout = 3 * 60>
CFScript形式
setting requesttimeout = 3 * 60;