我有一个在AppEngine Standard Java8上运行的webapp。该应用程序需要将消息发布到Google PubSub。每当执行此行时:

ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);


它将抛出错误“ java.lang.NoSuchFieldError:CONTEXT_SPAN_KEY”。 io.grpc.internal.CensusTracingModule $ TracingClientInterceptor.interceptCall(CensusTracingModule.java:380)。看起来我使用的是没有CONTEXT_SPAN_KEY的“库”的较新版本或较旧版本。有人知道如何针对这个图书馆吗?

我是Google App Engine(Java8)和PubSub的新手,这是我的第一篇stackoverflow帖子。

在过去的两天里,我一直在努力寻找导致此错误的根本原因,但到目前为止还没有运气。如果有人可以帮助,将不胜感激!

这是代码:

PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(payload)).build();

  ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);

  // Add an asynchronous callback to handle success / failure
  ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback<String>() {

    @Override
    public void onFailure(Throwable throwable) {
      throwable.printStackTrace();

      if (throwable instanceof ApiException) {
        ApiException apiException = ((ApiException) throwable);
        // details on the API exception
        System.out.println(apiException.getStatusCode().getCode());
        System.out.println(apiException.isRetryable());
      }
      System.out.println("Error publishing message");
    }


这是错误消息:

[INFO] GCLOUD: INFO: Dev App Server is now running
[INFO] GCLOUD:  published:
[INFO] GCLOUD: java.lang.NoSuchFieldError: CONTEXT_SPAN_KEY
[INFO] GCLOUD:  at io.grpc.internal.CensusTracingModule$TracingClientInterceptor.interceptCall(CensusTracingModule.java:380)
[INFO] GCLOUD: Error publishing message
[INFO] GCLOUD:  at io.grpc.ClientInterceptors$InterceptorChannel.newCall(ClientInterceptors.java:104)
[INFO] GCLOUD:  at io.grpc.internal.CensusStatsModule$StatsClientInterceptor.interceptCall(CensusStatsModule.java:675)
[INFO] GCLOUD:  at io.grpc.ClientInterceptors$InterceptorChannel.newCall(ClientInterceptors.java:104)
[INFO] GCLOUD:  at com.google.api.gax.grpc.GrpcHeaderInterceptor.interceptCall(GrpcHeaderInterceptor.java:81)
[INFO] GCLOUD:  at io.grpc.ClientInterceptors$InterceptorChannel.newCall(ClientInterceptors.java:104)
[INFO] GCLOUD:  at com.google.api.gax.grpc.GrpcMetadataHandlerInterceptor.interceptCall(GrpcMetadataHandlerInterceptor.java:55)
[INFO] GCLOUD:  at io.grpc.ClientInterceptors$InterceptorChannel.newCall(ClientInterceptors.java:104)
[INFO] GCLOUD:  at io.grpc.internal.ManagedChannelImpl.newCall(ManagedChannelImpl.java:636)
[INFO] GCLOUD:  at com.google.api.gax.grpc.ChannelPool.newCall(ChannelPool.java:77)
[INFO] GCLOUD:  at com.google.api.gax.grpc.GrpcClientCalls.newCall(GrpcClientCalls.java:75)
[INFO] GCLOUD:  at com.google.api.gax.grpc.GrpcDirectCallable.futureCall(GrpcDirectCallable.java:58)
[INFO] GCLOUD:  at com.google.api.gax.grpc.GrpcExceptionCallable.futureCall(GrpcExceptionCallable.java:62)
[INFO] GCLOUD:  at com.google.api.gax.rpc.AttemptCallable.call(AttemptCallable.java:75)
[INFO] GCLOUD:  at com.google.api.gax.rpc.RetryingCallable.futureCall(RetryingCallable.java:63)
[INFO] GCLOUD:  at com.google.api.gax.rpc.RetryingCallable.futureCall(RetryingCallable.java:41)
[INFO] GCLOUD:  at com.google.api.gax.rpc.BatchingCallable.futureCall(BatchingCallable.java:79)
[INFO] GCLOUD:  at com.google.api.gax.rpc.UnaryCallable$1.futureCall(UnaryCallable.java:126)
[INFO] GCLOUD:  at com.google.api.gax.rpc.UnaryCallable.futureCall(UnaryCallable.java:87)
[INFO] GCLOUD:  at com.google.cloud.pubsub.v1.Publisher.publishOutstandingBatch(Publisher.java:322)
[INFO] GCLOUD:  at com.google.cloud.pubsub.v1.Publisher.publishAllOutstanding(Publisher.java:311)
[INFO] GCLOUD:  at com.google.cloud.pubsub.v1.Publisher$3.run(Publisher.java:285)
[INFO] GCLOUD:  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[INFO] GCLOUD:  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[INFO] GCLOUD:  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
[INFO] GCLOUD:  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
[INFO] GCLOUD:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[INFO] GCLOUD:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[INFO] GCLOUD:  at java.lang.Thread.run(Thread.java:745)
[INFO] GCLOUD: Oct 12, 2018 7:25:22 AM com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler handle
[INFO] GCLOUD: WARNING: exception occurred while invoking backend method
[INFO] GCLOUD: java.io.IOException: com.fasterxml.jackson.databind.JsonMappingException: ProjectTopicName.getType() not supported (through reference chain: com.google.cloud.pubsub.v1.Publisher["topicName"]->com.google.pubsub.v1.ProjectTopicName["type"])
[INFO] GCLOUD:  at com.google.api.server.spi.response.ServletResponseResultWriter.writeValueAsString(ServletResponseResultWriter.java:212)
[INFO] GCLOUD:  at com.google.api.server.spi.response.ServletResponseResultWriter.write(ServletResponseResultWriter.java:101)
[INFO] GCLOUD:  at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:352)
[INFO] GCLOUD:  at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:119)
[INFO] GCLOUD:  at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:102)
[INFO] GCLOUD:  at com.google.api.server.spi.dispatcher.PathDispatcher.dispatch(PathDispatcher.java:50)
[INFO] GCLOUD:  at com.google.api.server.spi.EndpointsServlet.service(EndpointsServlet.java:72)
[INFO] GCLOUD:  at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:865)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1655)
[INFO] GCLOUD:  at com.google.api.control.ControlFilter.doFilter(ControlFilter.java:331)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
[INFO] GCLOUD:  at com.google.api.control.ConfigFilter.doFilter(ConfigFilter.java:125)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
[INFO] GCLOUD:  at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:134)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
[INFO] GCLOUD:  at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
[INFO] GCLOUD:  at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
[INFO] GCLOUD:  at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
[INFO] GCLOUD:  at com.google.appengine.tools.development.jetty9.StaticFileFilter.doFilter(StaticFileFilter.java:123)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
[INFO] GCLOUD:  at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
[INFO] GCLOUD:  at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
[INFO] GCLOUD:  at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
[INFO] GCLOUD:  at com.google.appengine.tools.development.DevAppServerRequestLogFilter.doFilter(DevAppServerRequestLogFilter.java:44)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1634)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
[INFO] GCLOUD:  at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146)
[INFO] GCLOUD:  at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524)
[INFO] GCLOUD:  at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
[INFO] GCLOUD:  at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257)
[INFO] GCLOUD:  at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
[INFO] GCLOUD:  at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
[INFO] GCLOUD:  at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1317)
[INFO] GCLOUD:  at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
[INFO] GCLOUD:  at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
[INFO] GCLOUD:  at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
[INFO] GCLOUD:  at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
[INFO] GCLOUD:  at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1219)
[INFO] GCLOUD:  at com.google.appengine.tools.development.jetty9.DevAppEngineWebAppContext.doScope(DevAppEngineWebAppContext.java:94)
[INFO] GCLOUD:  at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
[INFO] GCLOUD:  at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
[INFO] GCLOUD:  at com.google.appengine.tools.development.jetty9.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:595)
[INFO] GCLOUD:  at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
[INFO] GCLOUD:  at org.eclipse.jetty.server.Server.handle(Server.java:531)
[INFO] GCLOUD:  at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:352)
[INFO] GCLOUD:  at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
[INFO] GCLOUD:  at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:281)
[INFO] GCLOUD:  at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
[INFO] GCLOUD:  at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
[INFO] GCLOUD:  at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:762)
[INFO] GCLOUD:  at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:680)
[INFO] GCLOUD:  at java.lang.Thread.run(Thread.java:745)
[INFO] GCLOUD: Caused by: com.fasterxml.jackson.databind.JsonMappingException: ProjectTopicName.getType() not supported (through reference chain: com.google.cloud.pubsub.v1.Publisher["topicName"]->com.google.pubsub.v1.ProjectTopicName["type"])
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:391)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:351)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:316)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:727)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1396)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1120)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:993)
[INFO] GCLOUD:  at com.google.api.server.spi.response.ServletResponseResultWriter.writeValueAsString(ServletResponseResultWriter.java:210)
[INFO] GCLOUD:  ... 56 more
[INFO] GCLOUD: Caused by: java.lang.UnsupportedOperationException: ProjectTopicName.getType() not supported
[INFO] GCLOUD:  at com.google.pubsub.v1.ProjectTopicName.getType(ProjectTopicName.java:130)
[INFO] GCLOUD:  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[INFO] GCLOUD:  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[INFO] GCLOUD:  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[INFO] GCLOUD:  at java.lang.reflect.Method.invoke(Method.java:498)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:687)
[INFO] GCLOUD:  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
[INFO] GCLOUD:  ... 66 more
[INFO] GCLOUD:


pom.xml

<project>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.test</groupId>
<artifactId>appengine-pubsub</artifactId>
<prerequisites>
    <maven>3.0</maven>
</prerequisites>

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <endpoints.framework.version>2.1.2</endpoints.framework.version>
    <endpoints.management.version>1.0.10</endpoints.management.version>

    <google.datastore.version>1.38.0</google.datastore.version>

    <io.netty.all>5.0.0.Alpha2</io.netty.all>

    <endpoints.project.id>my-very-first-project-id</endpoints.project.id>
</properties>

<dependencies>
    <!-- Google -->
    <dependency>
        <groupId>com.google.endpoints</groupId>
        <artifactId>endpoints-framework</artifactId>
        <version>${endpoints.framework.version}</version>
    </dependency>
    <dependency>
        <groupId>com.google.endpoints</groupId>
        <artifactId>endpoints-management-control-appengine-all</artifactId>
        <version>${endpoints.management.version}</version>
    </dependency>
    <dependency>
        <groupId>com.google.endpoints</groupId>
        <artifactId>endpoints-management-control-appengine</artifactId>
        <version>${endpoints.management.version}</version>
    </dependency>
    <dependency>
        <groupId>com.google.endpoints</groupId>
        <artifactId>endpoints-framework-auth</artifactId>
        <version>${endpoints.management.version}</version>
    </dependency>
    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-api-1.0-sdk</artifactId>
        <version>1.9.64</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-pubsub -->
    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-pubsub</artifactId>
        <version>0.48.0-beta</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>
    <!-- javax -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <type>jar</type>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180813</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>3.6.1</version>
    </dependency>

</dependencies>

<build>
    <!-- for hot reload of the web application-->
    <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes
    </outputDirectory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <webResources>
                    <resources>
                        <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                        <filtering>true</filtering>
                        <targetPath>WEB-INF</targetPath>
                    </resources>
                </webResources>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>1.3.2</version>
            <configuration>
                <!-- deploy configuration -->
                <hostname>${endpoints.project.id}.appspot.com</hostname>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>endpoints-framework-maven-plugin</artifactId>
            <version>1.0.2</version>
            <configuration>
                <!-- plugin configuration -->
                <hostname>${endpoints.project.id}.appspot.com</hostname>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>display-dependency-updates</goal>
                        <goal>display-plugin-updates</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

最佳答案

在逐个筛选出lib之后,我能够找出原因。我从pom.xml中删除了endpoints-management-control-appengine-all,PubSub开始工作(从本地)。

<dependency>
    <groupId>com.google.endpoints</groupId>
    <artifactId>endpoints-management-control-appengine-all</artifactId>
    <version>${endpoints.management.version}</version>
</dependency>


谢谢大家!

关于java - AppEngine标准Java8中的Google PubSub错误:java.lang.NoSuchFieldError:CONTEXT_SPAN_KEY,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52775142/

10-11 05:00