Skip to content

Instantly share code, notes, and snippets.

@Narigo
Last active December 12, 2015 04:28
Show Gist options
  • Save Narigo/4714791 to your computer and use it in GitHub Desktop.
Save Narigo/4714791 to your computer and use it in GitHub Desktop.
package tests;
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.http.RouteMatcher;
import org.vertx.java.deploy.Verticle;
public class HttpClientExample extends Verticle {
public void start() {
RouteMatcher rm = new RouteMatcher();
rm.get("/", new Handler<HttpServerRequest>() {
public void handle(HttpServerRequest request) {
request.response.statusCode = 200;
request.response.end("something");
}
});
vertx.createHttpServer().requestHandler(rm).listen(8080);
}
}
package tests;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.vertx.java.core.Handler;
import org.vertx.java.core.buffer.Buffer;
import org.vertx.java.core.http.HttpClient;
import org.vertx.java.core.http.HttpClientResponse;
import org.vertx.java.test.TestVerticle;
import org.vertx.java.test.VertxConfiguration;
import org.vertx.java.test.VertxTestBase;
import org.vertx.java.test.junit.VertxJUnit4ClassRunner;
@RunWith(VertxJUnit4ClassRunner.class)
@VertxConfiguration
@TestVerticle(main = "tests.HttpClientExample")
public class HttpClientTest extends VertxTestBase {
private <T> void offer(T content, LinkedBlockingQueue<T> queue) {
try {
queue.offer(content, 10L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void httpTest() {
HttpClient client = getVertx().createHttpClient().setHost("localhost").setPort(8080);
final LinkedBlockingQueue<HttpClientResponse> queueResponse = new LinkedBlockingQueue<>();
final LinkedBlockingQueue<Buffer> queueBuffer = new LinkedBlockingQueue<>();
client.getNow("/", new Handler<HttpClientResponse>() {
@Override
public void handle(HttpClientResponse event) {
offer(event, queueResponse);
event.bodyHandler(new Handler<Buffer>() {
@Override
public void handle(Buffer event) {
offer(event, queueBuffer);
}
});
}
});
try {
HttpClientResponse answer1 = queueResponse.poll(10L, TimeUnit.SECONDS);
if (answer1 == null) {
fail("Did not receive answer1 in time.");
}
assertTrue("should get status code 200.", answer1.statusCode == 200);
Buffer answer2 = queueBuffer.poll(10L, TimeUnit.SECONDS);
if (answer2 == null) {
fail("Did not receive answer2 in time.");
}
assertTrue("should receive 'something' but received: " + answer2.toString(), "something".equals(answer2.toString()));
} catch (InterruptedException e) {
//
}
}
}
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tests;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.vertx.java.core.Handler;
/**
* @author swilliams
*
* @param <T>
*/
public class QueueReplyHandler<T> implements Handler<T> {
private final LinkedBlockingQueue<T> queue;
private final long timeout;
private final TimeUnit timeUnit;
public QueueReplyHandler(LinkedBlockingQueue<T> queue) {
this(queue, 5000L);
}
public QueueReplyHandler(LinkedBlockingQueue<T> queue, long timeout) {
this(queue, timeout, TimeUnit.MILLISECONDS);
}
public QueueReplyHandler(LinkedBlockingQueue<T> queue, long timeout, TimeUnit timeUnit) {
this.queue = queue;
this.timeout = timeout;
this.timeUnit = timeUnit;
}
@Override
public void handle(T event) {
try {
if (event != null) {
queue.offer(event, timeout, timeUnit);
}
} catch (InterruptedException e) {
Assert.fail(e.getMessage());
}
}
}
@purplefox
Copy link

What you're doing here is illegal in Vert.x - vert.x objects e.g. HttpClientRequest HttpClientResponse etc must only be accessed on the context on which they're being created.

In this case you are getting a HttpResponse then transferring it back to the main thread where you are trying to do something with it.

@purplefox
Copy link

Is this being run in a verticle?

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