Working at the Ads API Workshop in NYC I came across this error using the Java client library.
This error is intermittent. In the code example below, the error occurs on the 2nd search call:
List adGroupRows = googleSearchService.search( adGroupRequest );
Once the error is thrown it seems to not get thrown again. ie. I can call this code once, get the error, and then call it 2 -> n times without getting the error again.
Gradle dependency: implementation "com.google.api-ads:google-ads:0.7.0"
Stacktrace:
2019-03-06 14:52:00,799 INFO summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v0.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 7402014503, RequestID: WvvumPapGMwF2YJSE_oJBw, ResponseCode: OK, Fault: null.
2019-03-06 14:52:05,179 ERROR ManagedChannelOrphanWrapper - ~* Channel ManagedChannelImpl{logId=1, target=googleads.googleapis.com:443} was not shutdown properly!!! *~
Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.
java.lang.RuntimeException: ManagedChannel allocation site
at io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference.(ManagedChannelOrphanWrapper.java:103)
at io.grpc.internal.ManagedChannelOrphanWrapper.(ManagedChannelOrphanWrapper.java:53)
at io.grpc.internal.ManagedChannelOrphanWrapper.(ManagedChannelOrphanWrapper.java:44)
at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:411)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:246)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:160)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:152)
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:149)
at com.google.ads.googleads.v0.services.stub.GrpcGoogleAdsServiceStub.create(GrpcGoogleAdsServiceStub.java:78)
at com.google.ads.googleads.v0.services.stub.GoogleAdsServiceStubSettings.createStub(GoogleAdsServiceStubSettings.java:110)
at com.google.ads.googleads.v0.services.GoogleAdsServiceClient.(GoogleAdsServiceClient.java:141)
at com.google.ads.googleads.v0.services.GoogleAdsServiceClient.create(GoogleAdsServiceClient.java:122)
at com.google.ads.googleads.lib.GrpcServiceDescriptor$GoogleAdsServiceDescriptor.newServiceClient(GrpcServiceDescriptor.java:988)
at com.google.ads.googleads.lib.GrpcServiceDescriptor$GoogleAdsServiceDescriptor.newServiceClient(GrpcServiceDescriptor.java:981)
at com.google.ads.googleads.lib.GoogleAdsClient.getGoogleAdsServiceClient(GoogleAdsClient.java:420)
at inc.fluency.google.service.google.newapi.GoogleSearchService.search(GoogleSearchService.java:23)
at inc.fluency.google.service.google.newapi.TestNewAPI.testGetAllCampaigns(TestNewAPI.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor115.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javax.management.modelmbean.RequiredModelMBean$4.run(RequiredModelMBean.java:1252)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at javax.management.modelmbean.RequiredModelMBean.invokeMethod(RequiredModelMBean.java:1246)
at javax.management.modelmbean.RequiredModelMBean.invoke(RequiredModelMBean.java:1085)
at org.springframework.jmx.export.SpringModelMBean.invoke(SpringModelMBean.java:90)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at mx4j.tools.adaptor.http.InvokeOperationCommandProcessor.executeRequest(InvokeOperationCommandProcessor.java:164)
at mx4j.tools.adaptor.http.HttpAdaptor$HttpClient.run(HttpAdaptor.java:980)
2019-03-06 14:52:07,206 INFO summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v0.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 7402014503, RequestID: aKj_a-9aFIYfnr4QdMalng, ResponseCode: OK, Fault: null.
Simplified code:
final String campaignQuery = "select campaign.id, campaign.name from campaign";
final String adGroupQuery = "select campaign.id, campaign.name, ad_group.id, ad_group.name, ad_group.campaign from ad_group";
final SearchGoogleAdsRequest campaignRequest = SearchGoogleAdsRequest.newBuilder()
.setCustomerId( fluentAccount.getReferenceId() )
.setPageSize( PAGE_SIZE )
.setQuery( campaignQuery )
.build();
final SearchGoogleAdsRequest adGroupRequest = SearchGoogleAdsRequest.newBuilder()
.setCustomerId( fluentAccount.getReferenceId() )
.setPageSize( PAGE_SIZE )
.setQuery( adGroupQuery )
.build();
List campaignRows = googleSearchService.search( campaignRequest );
List adGroupRows = googleSearchService.search( adGroupRequest );
public List search( SearchGoogleAdsRequest request ) throws Exception {
return Lists.newArrayList(
googleService
.getGoogleAdsClient()
.getGoogleAdsServiceClient()
.search( request )
.iterateAll() );
}
public GoogleAdsClient getGoogleAdsClient() throws Exception {
if( googleAdsClient == null ) {
googleAdsClient = GoogleAdsClient.newBuilder()
.setCredentials( getUserCredentials() )
.setDeveloperToken( developerToken )
.setLoginCustomerId( Long.valueOf( clientCustomerId ) )
.build();
}
return googleAdsClient;
}
private Credentials getUserCredentials() throws Exception {
/* Prime the clientId / clientSecret / refreshToken / developerToken */
getAdwordsCredentials();
return UserCredentials.newBuilder()
.setClientId( clientId )
.setClientSecret( clientSecret )
.setRefreshToken( refreshToken )
.build();
}
private Credential getAdwordsCredentials() throws Exception {
if ( adWordsCredentials == null ) {
adWordsCredentials = new OfflineCredentials.Builder()
.forApi(OfflineCredentials.Api.ADWORDS)
.withClientSecrets( credentials.getClientId(), credentials.getClientSecret() )
.withRefreshToken( credentials.getRefreshToken() )
.build()
.generateCredential();
}
return adWordsCredentials;
}