Trying out the library for the first time - encountering problems

207 views
Skip to first unread message

Florin-Daniel Cioloboc

unread,
Jun 24, 2016, 11:15:52 AM6/24/16
to asynchttpclient
Hi everyone,

First of all, this is the first time I started using this library and so far I'm doing something wrong and I can't figure out how to fix it.
My mentor for the Google Summer of Code recommended me to try to use this library instead of the Java core HTTP functionality, however, I'm having some issues here I don't know where to ask.

This is what I use in the main:

AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient();
Future<Response> f = asyncHttpClient.prepareGet("http://istsos.org/").execute();
Response r = f.get();
System.out.println(r.getStatusCode());

This is what I get:

Exception in thread "main" java.lang.NoClassDefFoundError: io/netty/util/Timer
    at istsos.Database.main(Database.java:135)
Caused by: java.lang.ClassNotFoundException: io.netty.util.Timer


Hopefully someone can help me out a bit, thanks in advance.

Best,
Florin

Florin-Daniel Cioloboc

unread,
Jun 24, 2016, 11:18:56 AM6/24/16
to asynchttpclient
By the way, I'm using the 2.0.6 version of it. I downloaded the .jar file from Maven repository as I'm not using Maven.
Could that be an issue?

Best,
Florin

Marshall Pierce

unread,
Jun 24, 2016, 11:24:28 AM6/24/16
to asyncht...@googlegroups.com
This is because you’re missing dependencies (netty, in this case). You should use a build tool like gradle or maven to handle dependencies for you.

I have a quickstart project to help people get started with gradle; you’re welcome to crib from it. https://bitbucket.org/marshallpierce/java-quickstart
> --
> You received this message because you are subscribed to the Google Groups "asynchttpclient" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to asynchttpclie...@googlegroups.com.
> To post to this group, send email to asyncht...@googlegroups.com.
> Visit this group at https://groups.google.com/group/asynchttpclient.
> For more options, visit https://groups.google.com/d/optout.

Stéphane LANDELLE

unread,
Jun 24, 2016, 11:25:15 AM6/24/16
to asyncht...@googlegroups.com
"java.lang.ClassNotFoundException" typically means a required library is missing from your classpath.

If you really don't want to use maven or another compatible build and dependancy management tool (which you really should if you want to go past the quick-and-dirty student level), and want to build your classpath manually, you have to add those deps too. Here, you're missing Netty, but some others too. Have a look at the pom.xml and the parent's one too.

Stéphane Landelle
GatlingCorp CEO


--

Florin-Daniel Cioloboc

unread,
Jun 24, 2016, 11:36:38 AM6/24/16
to asynchttpclient
Hey guys,

Wow. Thank you for the lightning replies.

It's not that I don't want to use Maven/Gradle, I actually just configured my project to Gradle as it would be more helpful.
Yes, I really want to go past that student level as it's most likely not recommended to keep on going manually like this.

Basically, if I got this right, all I have to do now is add a line that compiles netty in the build.gradle file?

Best,
Florin

Stéphane LANDELLE

unread,
Jun 24, 2016, 11:39:45 AM6/24/16
to asyncht...@googlegroups.com
If you use a proper build tool, you just have to depend on asynchttpclient and the build tool will fetch all the transitive dependencies for you.
And no, Netty is not the only dep you'll need.

Stéphane Landelle
GatlingCorp CEO


Marshall Pierce

unread,
Jun 24, 2016, 11:42:44 AM6/24/16
to asyncht...@googlegroups.com
See https://bitbucket.org/marshallpierce/java-quickstart/src/2e51d8d58f3756f143c99de62fd59bf2e411a6cb/build.gradle?at=master for an example.

You’ll want a line like:

compile 'org.asynchttpclient:async-http-client:2.0.7’

in your dependencies block. This will get AHC and all its dependencies.

Run `./gradlew dependencies` to see the full dependency tree for all the various configurations.

> On Jun 24, 2016, at 8:36 AM, Florin-Daniel Cioloboc <ciolobo...@gmail.com> wrote:
>
> Hey guys,
>
> Wow. Thank you for the lightning replies.
>
> It's not that I don't want to use Maven/Gradle, I actually just configured my project to Gradle as it would be more helpful.
> Yes, I really want to go past that student level as it's most likely not recommended to keep on going manually like this.
>
> Basically, if I got this right, all I have to do now is add a line that compiles netty in the build.gradle file?
>
> Best,
> Florin
>
>
> On Friday, June 24, 2016 at 6:25:15 PM UTC+3, Stéphane LANDELLE wrote:
> "java.lang.ClassNotFoundException" typically means a required library is missing from your classpath.
>
> If you really don't want to use maven or another compatible build and dependancy management tool (which you really should if you want to go past the quick-and-dirty student level), and want to build your classpath manually, you have to add those deps too. Here, you're missing Netty, but some others too. Have a look at the pom.xml and the parent's one too.
>
> Stéphane Landelle
> GatlingCorp CEO
> slan...@gatling.io
>
>

Florin-Daniel Cioloboc

unread,
Jun 24, 2016, 12:02:26 PM6/24/16
to asynchttpclient

Yes, found out the hard way that netty wasn't the only thing I needed so I tried to add a couple of things which obviously didn't work.
Then I tried Marshall Pierce's solution but that did not work either and got the following errors when I tried to refresh it. Attached a png picture.

Best,
Florin
gradle.png

Florin-Daniel Cioloboc

unread,
Jun 24, 2016, 12:03:35 PM6/24/16
to asynchttpclient
Forgot to mention that I'm using Eclipse as an IDE and not the classic CLI git/terminal commands.


Best,
Florin

On Friday, June 24, 2016 at 6:15:52 PM UTC+3, Florin-Daniel Cioloboc wrote:

Florin-Daniel Cioloboc

unread,
Jun 24, 2016, 12:20:30 PM6/24/16
to asynchttpclient
OK, I made some modification and changed the code in build.gradle to this.

apply plugin: 'java'

repositories
{
  jcenter
()
}

dependencies
{
  compile
'com.google.code.findbugs:jsr305:3.0.0'
  compile
group: 'org.asynchttpclient', name: 'async-http-client', version: '2.0.7'
  testCompile
'junit:junit:4.12'
}

sourceCompatibility
= JavaVersion.VERSION_1_8


But, I get the following:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J
: Defaulting to no-operation (NOP) logger implementation
SLF4J
: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

I assume, I'm still missing something such as the org.slf4j but I don't understand why...

Tatu Saloranta

unread,
Jun 24, 2016, 1:36:23 PM6/24/16
to asyncht...@googlegroups.com
This is due to slf4j properly separating API to use for logging, and implementation deployment uses for handling such logging: libraries can (and should) not specify implementation, but they need to know how to log things.

So dependency to AHC can not give you concrete logger backend; you need to choose one for yourself, unless you are using a platform that forces the decision (like if running on DropWizard or Spring Boot, I think). You can add Logback as dependency, for example, to get rid of this warning.

-+ Tatu +-



--

Florin-Daniel Cioloboc

unread,
Jun 24, 2016, 1:49:07 PM6/24/16
to asynchttpclient
Hey Tatu,

I see, that makes sense what you telling me. I tried adding the following in the dependency in the hopes of getting rid of the warning.

  compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'

It still didn't go away, so if I may so bold to ask, how does one add a Logback as dependency to Gradle build file?

I tried using this for a change after some Googling
compile "ch.qos.logback:logback-classic:1.1.3"

configurations
.all {
    exclude
group: "org.slf4j", module: "slf4j-log4j12"
    exclude
group: "log4j", module: "log4j"
}



As a result I got a whole lot of this:
Request DefaultFullHttpRequest(decodeResult: success, version: HTTP/1.1, content: UnpooledUnsafeHeapByteBuf(freed))
GET
/ HTTP/1.1
Host: istsos.org
Accept: */*
User-Agent: AHC/2.0

Response DefaultHttpResponse(decodeResult: success, version: HTTP/1.1)
HTTP/1.1 200 OK
Date: Fri, 24 Jun 2016 17:45:30 GMT
Server: Apache/2.2.14 (Ubuntu)
Accept-Ranges: bytes
X-Mod-Pagespeed: 1.8.31.4-4056
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Cache-Control: max-age=0, no-cache
Content-Length: 16182
Content-Type: text/html

20:45:30.032 [AsyncHttpClient-2-1] DEBUG o.a.netty.channel.ChannelManager - Adding key: http://istsos.org:80 for channel [id: 0xb8d1d88e, L:/5.15.59.222:49824 - R:istsos.org/195.176.186.139:80]

Essentially, I got a lot of things which I don't think I really need at the moment? Is there another solution.



Best,
Florin

Stéphane LANDELLE

unread,
Jun 24, 2016, 2:03:48 PM6/24/16
to asyncht...@googlegroups.com
Read the logback documentation and figure out how to configure it.

Stéphane Landelle
GatlingCorp CEO

Tatu Saloranta

unread,
Jun 24, 2016, 6:28:38 PM6/24/16
to asyncht...@googlegroups.com
Um, you got logging to work, and there's stuff in the logs? Yes, that's how that works. :-D

As Stéphane suggested, this would be good time to configure your logger: as with logger implementation, library like AHC does not (and should not) try to configure it since you (via application / framework) know settings that should be applied. All library does is call various `logger.info(...)` etc calls; how that behaves is up to logging configuration; and documentation for that exist.

Good luck!

-+ Tatu +-

Florin-Daniel Cioloboc

unread,
Jul 10, 2016, 2:15:53 PM7/10/16
to asynchttpclient
Hello,

Yes, I figured it out, in any case I reimplemented the AsyncHttpClient in another way and pretty much restructured my coding to make
good use of this, had plenty of time to do it :)

In any case, my apologies for not replying, however I would like to thank you for helping me out with this.

Best,
Florin
Reply all
Reply to author
Forward
0 new messages