Skip to content

Instantly share code, notes, and snippets.

@ilopmar
Last active December 15, 2015 22:30
Show Gist options
  • Save ilopmar/a55af3346aaa788c632d to your computer and use it in GitHub Desktop.
Save ilopmar/a55af3346aaa788c632d to your computer and use it in GitHub Desktop.
package com.example
import ratpack.guice.Guice
import ratpack.server.RatpackServer
public class MyApp {
public static void main(String[] args) throws Exception {
RatpackServer.start { spec ->
spec.registry {
Guice.registry { r ->
r.bind(MyService.class)
}
}
.handlers { chain ->
chain.get { ctx ->
MyService myService = ctx.get(MyService.class)
myService.myMethod()
ctx.render("Finished!")
}
}
}
}
}
package example;
import ratpack.guice.Guice;
import ratpack.server.RatpackServer;
public class MyApp {
public static void main(String[] args) throws Exception {
RatpackServer.start(spec -> spec
.registry(Guice.registry(r -> r
.bind(MyService.class)
))
.handlers(chain -> chain
.get(ctx -> {
MyService myService = ctx.get(MyService.class);
myService.myMethod();
ctx.render("Finished");
})
));
}
}
package com.example
class MyService {
void myMethod() {
System.out.println("calling myMethod");
}
}
@ilopmar
Copy link
Author

ilopmar commented Dec 15, 2015

If I run the MyApp.java application everything works fine. I can see the message calling myMethod in the console and the Finished message rendered in the browser.

If I run the MyApp.groovy application I get this error:

java.lang.ClassCastException: ratpack.guice.Guice$$Lambda$57/1865859824 cannot be cast to ratpack.registry.Registry
  at ratpack.server.internal.DefaultRatpackServer$DefinitionBuild.lambda$new$242(DefaultRatpackServer.java:169)
  at ratpack.server.internal.ServerRegistry.buildUserRegistry(ServerRegistry.java:81)
  at ratpack.server.internal.ServerRegistry.serverRegistry(ServerRegistry.java:70)
  at ratpack.server.internal.DefaultRatpackServer.buildServerRegistry(DefaultRatpackServer.java:292)
  at ratpack.server.internal.DefaultRatpackServer.buildAdapter(DefaultRatpackServer.java:265)
  at ratpack.server.internal.DefaultRatpackServer$ReloadHandler.lambda$null$257(DefaultRatpackServer.java:494)
  at ratpack.exec.Blocking$1.lambda$get$122(Blocking.java:70)
  at ratpack.exec.Blocking.intercept(Blocking.java:241)
  at ratpack.exec.Blocking.access$000(Blocking.java:35)
  at ratpack.exec.Blocking$1.get(Blocking.java:68)
  at ratpack.exec.Blocking$1.get(Blocking.java:61)
  at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at ratpack.exec.internal.DefaultExecController$ExecControllerBindingThreadFactory.lambda$newThread$135(DefaultExecController.java:113)
  at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
  at java.lang.Thread.run(Thread.java:745)

Any idea why this happens?

@danhyun
Copy link

danhyun commented Dec 15, 2015

Yes, just change to

public class MyApp {
  public static void main(String[] args) throws Exception {
    RatpackServer.start { spec ->
      spec.registry(
        Guice.registry { r ->
          r.bind(MyService.class)
        }
      )
      .handlers { chain ->
        chain.get { ctx ->
          MyService myService = ctx.get(MyService.class)
          myService.myMethod()
          ctx.render("Finished!")
        }
      }
    }
  }
}

@ilopmar
Copy link
Author

ilopmar commented Dec 15, 2015

Excellent! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment