1

我正在尝试关注这篇博文,该博文演示了ratpack 中的阻塞处理程序,但我无法让它工作。我缺少背景的方法异常。

我的 build.gradle 文件如下来自这个 GitHub 目录

buildscript {
    repositories {
    maven { url "http://oss.jfrog.org/artifactory/repo" }
    jcenter()
  }
  dependencies {
    classpath 'io.ratpack:ratpack-gradle:0.9.4'
  }
}

apply plugin: "ratpack-groovy"
apply plugin: "idea"
apply plugin: "eclipse"

repositories {
  maven { url "http://oss.jfrog.org/artifactory/repo" }
  jcenter()
  maven { url "http://repo.springsource.org/repo" }
  maven { url "https://nexus.codehaus.org/content/repositories/snapshots/" }
}

dependencies {
  springloaded "org.springsource.loaded:springloaded:1.1.5.RELEASE"

  testCompile "org.spockframework:spock-core:0.7-groovy-2.0", {
      exclude module: "groovy-all"
  }
}

// The Groovy that rest-assured drags in via ratpack-groovy-test is toxic, prevent it from coming in
// This should be fixed upstream in Ratpack
configurations.testCompile.dependencies.find { it.name == "ratpack-groovy-test" }.exclude(group: "org.codehaus.groovy")

处理程序代码如下。

import static ratpack.groovy.Groovy.ratpack

    ratpack {
        handlers {
            get("non-blocking") {
                background {
                    Thread.sleep(10000)
                }.then{
                    response.send()
                }
            }
        }
    }

我缺少背景的方法异常。

groovy.lang.MissingMethodException: No signature of method: nonBlocking.background() is applicable for argument types: (nonBlocking$_run_closure1$_closure2$_closure3$_closure4) values: [nonBlocking$_run_closure1$_closure2$_closure3$_closure4@497352d0]
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:81)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at 
4

2 回答 2

2

background此后已被替换为Blocking.get

https://ratpack.io/manual/current/api/ratpack/exec/Blocking.html#get-ratpack.func.Factory-

于 2017-09-03T21:48:26.480 回答
1

您使用的 Gradle 版本是什么?请记住,这个示例已经有将近 4 年的历史了,并且它使用了 Gradle 等旧版本的工具。如果我使用例如 Gradle3.5-rc-2版本并执行

./gradlew test

它会因以下错误而失败:

FAILURE: Build failed with an exception.

* Where:
Build file '/home/wololock/workspace/idea/stackoverflow-answers/46009278/build.gradle' line: 11

* What went wrong:
A problem occurred evaluating root project '46009278'.
> Failed to apply plugin [id 'ratpack-groovy']
   > Could not find method groovy() for arguments [DefaultExternalModuleDependency{group='io.ratpack', name='ratpack-groovy', version='0.9.4', configuration='default'}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

现在如果我们看一下这个例子开发时使用的 Gradle 版本,我们会发现它1.10根据项目gradle-wrapper.properties文件使用了 Gradle:

#Sun Jul 21 19:00:37 CDT 2013
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.10-bin.zip

如果您定义相同的 Gradle 包装器版本并执行以下操作:

./gradlew test

或者

./gradlew runFatJar

要运行应用程序,它应该可以工作:

10:39:44: Executing external task 'runFatJar'...
The groovy configuration has been deprecated and is scheduled to be removed in Gradle 2.0. Typically, usages of 'groovy' can simply be replaced with 'compile'. In some cases, it may be necessary to additionally configure the 'groovyClasspath' property of GroovyCompile and Groovydoc tasks.
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:fatJar
wrz 02, 2017 10:39:48 AM ratpack.server.internal.NettyRatpackServer start
INFO: Ratpack started for http://localhost:5050
:runFatJar
Sat Sep 02 10:40:40 CEST 2017 - received blocking request
Sat Sep 02 10:40:50 CEST 2017 - replying blocking request
Sat Sep 02 10:40:50 CEST 2017 - received non-blocking request
Sat Sep 02 10:41:00 CEST 2017 - replying non-blocking request
于 2017-09-02T08:42:22.520 回答