Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced micro-services utils (based on HTTP client).
- Loading branch information
1 parent
3cc2ed6
commit 95e7288
Showing
8 changed files
with
197 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,66 @@ | |||
package org.rapidoid.http; | |||
|
|||
/* | |||
* #%L | |||
* rapidoid-http | |||
* %% | |||
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors | |||
* %% | |||
* 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. | |||
* #L% | |||
*/ | |||
|
|||
import org.rapidoid.annotation.Authors; | |||
import org.rapidoid.annotation.Since; | |||
import org.rapidoid.concurrent.Callback; | |||
import org.rapidoid.concurrent.Callbacks; | |||
import org.rapidoid.concurrent.Future; | |||
import org.rapidoid.concurrent.Futures; | |||
import org.rapidoid.jackson.JSON; | |||
import org.rapidoid.lambda.Mapper; | |||
import org.rapidoid.util.U; | |||
|
|||
@Authors("Nikolche Mihajlovski") | |||
@Since("4.1.0") | |||
public class Micro { | |||
|
|||
private static final Mapper<byte[], Object> JSON_BYTES_TO_OBJ = new Mapper<byte[], Object>() { | |||
@Override | |||
public Object map(byte[] src) throws Exception { | |||
return src != null ? JSON.parse(src, Object.class) : null; | |||
} | |||
}; | |||
|
|||
public static <T> Future<T> get(final String uri, final Callback<T> callback) { | |||
Mapper<byte[], T> mapper = U.cast(JSON_BYTES_TO_OBJ); | |||
Callback<byte[]> cb = Callbacks.mapping(callback, mapper); | |||
return Futures.mapping(HTTP.get(uri, cb), mapper); | |||
} | |||
|
|||
@SuppressWarnings("unchecked") | |||
public static <T> T get(final String uri) { | |||
return (T) get(uri, null).get(); | |||
} | |||
|
|||
public static <T> Future<T> post(final String uri, final Callback<T> callback) { | |||
Mapper<byte[], T> mapper = U.cast(JSON_BYTES_TO_OBJ); | |||
Callback<byte[]> cb = Callbacks.mapping(callback, mapper); | |||
return Futures.mapping(HTTP.post(uri, null, null, null, cb), mapper); | |||
} | |||
|
|||
@SuppressWarnings("unchecked") | |||
public static <T> T post(final String uri) { | |||
return (T) post(uri, null).get(); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
rapidoid-integration-tests/src/test/java/org/rapidoid/test/MicroServicesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,102 @@ | |||
package org.rapidoid.test; | |||
|
|||
/* | |||
* #%L | |||
* rapidoid-integration-tests | |||
* %% | |||
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors | |||
* %% | |||
* 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. | |||
* #L% | |||
*/ | |||
|
|||
import java.util.concurrent.CountDownLatch; | |||
|
|||
import org.junit.Test; | |||
import org.rapidoid.activity.RapidoidThread; | |||
import org.rapidoid.annotation.Authors; | |||
import org.rapidoid.annotation.Since; | |||
import org.rapidoid.concurrent.Callback; | |||
import org.rapidoid.http.HTTP; | |||
import org.rapidoid.http.HTTPServer; | |||
import org.rapidoid.http.Handler; | |||
import org.rapidoid.http.HttpExchange; | |||
import org.rapidoid.http.Micro; | |||
import org.rapidoid.util.U; | |||
import org.rapidoid.util.UTILS; | |||
import org.rapidoid.webapp.WebApp; | |||
import org.rapidoid.webapp.WebAppGroup; | |||
|
|||
@Authors("Nikolche Mihajlovski") | |||
@Since("4.1.0") | |||
public class MicroServicesTest extends IntegrationTestCommons { | |||
|
|||
@Test | |||
public void testMicroserviceCommunication() { | |||
WebApp app = WebAppGroup.openRootContext(); | |||
|
|||
app.getRouter().generic(new Handler() { | |||
@Override | |||
public Object handle(HttpExchange x) throws Exception { | |||
// return x.async(); | |||
return U.num(x.param("n")) + 1; | |||
} | |||
}); | |||
HTTPServer server = HTTP.server().applications(WebAppGroup.main()).build().start(); | |||
|
|||
// a blocking call | |||
eq(Micro.get("http://localhost:8080/?n=7"), 8); | |||
eq(Micro.post("http://localhost:8080/?n=7"), 8); | |||
|
|||
int count = 10000; | |||
final CountDownLatch latch = new CountDownLatch(count); | |||
UTILS.startMeasure(); | |||
|
|||
RapidoidThread loop = UTILS.loop(new Runnable() { | |||
@Override | |||
public void run() { | |||
System.out.println(latch); | |||
U.sleep(1000); | |||
} | |||
}); | |||
|
|||
for (int i = 0; i < count; i++) { | |||
final int expected = i + 1; | |||
|
|||
Callback<Integer> callback = new Callback<Integer>() { | |||
@Override | |||
public void onDone(Integer result, Throwable error) throws Exception { | |||
if (result != null) { | |||
eq(result.intValue(), expected); | |||
} else { | |||
System.out.println(error); | |||
} | |||
latch.countDown(); | |||
} | |||
}; | |||
|
|||
if (i % 2 == 0) { | |||
Micro.get("http://localhost:8080/?n=" + i, callback); | |||
} else { | |||
Micro.post("http://localhost:8080/?n=" + i, callback); | |||
} | |||
} | |||
|
|||
U.wait(latch); | |||
UTILS.endMeasure(count, "calls"); | |||
|
|||
loop.interrupt(); | |||
server.shutdown(); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters