From 3286806f226f10603b30bd1dbe0c392f1c05be14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ve=C4=8De=C5=99a?= Date: Wed, 6 May 2015 23:55:35 +0200 Subject: [PATCH] #5 service handle can be used to directly invoke the service --- .../invoker/internal/HttpInvokerServlet.java | 5 ++-- .../HttpInvokerMicroserviceProviderTest.java | 4 +++ microservices/pom.xml | 4 +-- .../org/silverware/microservices/Context.java | 4 ++- .../microservices/MicroserviceMetaData.java | 4 +-- .../silver/HttpInvokerSilverService.java | 3 ++ .../silver/cluster/ServiceHandle.java | 29 +++++++++++++++++-- 7 files changed, 43 insertions(+), 10 deletions(-) diff --git a/http-invoker-microservice-provider/src/main/java/org/silverware/microservices/providers/http/invoker/internal/HttpInvokerServlet.java b/http-invoker-microservice-provider/src/main/java/org/silverware/microservices/providers/http/invoker/internal/HttpInvokerServlet.java index 2ca1fb3..55481f6 100644 --- a/http-invoker-microservice-provider/src/main/java/org/silverware/microservices/providers/http/invoker/internal/HttpInvokerServlet.java +++ b/http-invoker-microservice-provider/src/main/java/org/silverware/microservices/providers/http/invoker/internal/HttpInvokerServlet.java @@ -23,6 +23,7 @@ import com.cedarsoftware.util.io.JsonWriter; import org.silverware.microservices.Context; import org.silverware.microservices.MicroserviceMetaData; +import org.silverware.microservices.silver.HttpInvokerSilverService; import org.silverware.microservices.silver.cluster.Invocation; import org.silverware.microservices.silver.cluster.ServiceHandle; @@ -52,13 +53,13 @@ protected void doGet(final HttpServletRequest req, final HttpServletResponse res @Override protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { - if (req.getRequestURI().endsWith("query")) { + if (req.getRequestURI().endsWith(HttpInvokerSilverService.QUERY_COMMAND)) { final JsonReader jsonReader = new JsonReader(req.getInputStream()); final MicroserviceMetaData metaData = (MicroserviceMetaData) jsonReader.readObject(); final List handles = context.assureHandles(metaData); final JsonWriter jsonWriter = new JsonWriter(resp.getOutputStream()); jsonWriter.write(handles); - } else if (req.getRequestURI().endsWith("invoke")) { + } else if (req.getRequestURI().endsWith(HttpInvokerSilverService.INVOKE_COMMAND)) { final JsonReader jsonReader = new JsonReader(req.getInputStream()); final Invocation invocation = (Invocation) jsonReader.readObject(); try { diff --git a/http-invoker-microservice-provider/src/test/java/org/silverware/microservices/providers/http/invoker/HttpInvokerMicroserviceProviderTest.java b/http-invoker-microservice-provider/src/test/java/org/silverware/microservices/providers/http/invoker/HttpInvokerMicroserviceProviderTest.java index b412f52..6656b3d 100644 --- a/http-invoker-microservice-provider/src/test/java/org/silverware/microservices/providers/http/invoker/HttpInvokerMicroserviceProviderTest.java +++ b/http-invoker-microservice-provider/src/test/java/org/silverware/microservices/providers/http/invoker/HttpInvokerMicroserviceProviderTest.java @@ -73,6 +73,10 @@ public void testHttpInvoker() throws Exception { con.disconnect(); + final ServiceHandle handle = handles.get(0); + long l = (Long) handle.invoke(bootUtil.getContext(), "sum", new Class[] { short.class, int.class }, new Object[] { (short) 3, 4 }); + Assert.assertEquals(l, 7L); + con = (HttpURLConnection) new URL(urlBase + "invoke").openConnection(); con.setRequestMethod("POST"); con.setDoInput(true); diff --git a/microservices/pom.xml b/microservices/pom.xml index a70fc53..6f3dcbf 100644 --- a/microservices/pom.xml +++ b/microservices/pom.xml @@ -19,8 +19,8 @@ commons-cli - com.fasterxml.jackson.core - jackson-annotations + com.cedarsoftware + json-io diff --git a/microservices/src/main/java/org/silverware/microservices/Context.java b/microservices/src/main/java/org/silverware/microservices/Context.java index 1f18c0a..2c64d99 100644 --- a/microservices/src/main/java/org/silverware/microservices/Context.java +++ b/microservices/src/main/java/org/silverware/microservices/Context.java @@ -22,6 +22,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.silverware.microservices.providers.MicroserviceProvider; +import org.silverware.microservices.silver.HttpInvokerSilverService; import org.silverware.microservices.silver.HttpServerSilverService; import org.silverware.microservices.silver.ProvidingSilverService; import org.silverware.microservices.silver.SilverService; @@ -108,7 +109,8 @@ public List assureHandles(final MicroserviceMetaData metaData) { microservices.removeAll(haveHandles); microservices.forEach(microservice -> { - final ServiceHandle handle = new ServiceHandle((String) properties.get(HttpServerSilverService.HTTP_SERVER_ADDRESS), metaData, microservice); + final String host = properties.get(HttpServerSilverService.HTTP_SERVER_ADDRESS) + ":" + properties.get(HttpServerSilverService.HTTP_SERVER_PORT); + final ServiceHandle handle = new ServiceHandle(host, metaData, microservice); result.add(handle); inboundHandles.add(handle); }); diff --git a/microservices/src/main/java/org/silverware/microservices/MicroserviceMetaData.java b/microservices/src/main/java/org/silverware/microservices/MicroserviceMetaData.java index f70315b..c2cbb33 100644 --- a/microservices/src/main/java/org/silverware/microservices/MicroserviceMetaData.java +++ b/microservices/src/main/java/org/silverware/microservices/MicroserviceMetaData.java @@ -19,8 +19,6 @@ */ package org.silverware.microservices; -import com.fasterxml.jackson.annotation.JsonProperty; - import java.lang.annotation.Annotation; import java.util.Arrays; import java.util.Set; @@ -57,7 +55,7 @@ public class MicroserviceMetaData { * @param qualifiers * The qualifiers of the discovered Microservice. */ - public MicroserviceMetaData(@JsonProperty("name") final String name, @JsonProperty("type") final Class type, @JsonProperty("qualifiers") final Set qualifiers) { + public MicroserviceMetaData(final String name, final Class type, final Set qualifiers) { this.name = name; this.type = type; this.qualifiers = qualifiers; diff --git a/microservices/src/main/java/org/silverware/microservices/silver/HttpInvokerSilverService.java b/microservices/src/main/java/org/silverware/microservices/silver/HttpInvokerSilverService.java index d78e4ac..22b4d16 100644 --- a/microservices/src/main/java/org/silverware/microservices/silver/HttpInvokerSilverService.java +++ b/microservices/src/main/java/org/silverware/microservices/silver/HttpInvokerSilverService.java @@ -26,4 +26,7 @@ public interface HttpInvokerSilverService extends SilverService { String INVOKER_URL = "silverware.http-invoker.url"; + String INVOKE_COMMAND = "invoke"; + String QUERY_COMMAND = "query"; + } diff --git a/microservices/src/main/java/org/silverware/microservices/silver/cluster/ServiceHandle.java b/microservices/src/main/java/org/silverware/microservices/silver/cluster/ServiceHandle.java index 92ee6b9..e56a7fe 100644 --- a/microservices/src/main/java/org/silverware/microservices/silver/cluster/ServiceHandle.java +++ b/microservices/src/main/java/org/silverware/microservices/silver/cluster/ServiceHandle.java @@ -19,10 +19,15 @@ */ package org.silverware.microservices.silver.cluster; -import com.fasterxml.jackson.annotation.JsonProperty; +import com.cedarsoftware.util.io.JsonReader; +import com.cedarsoftware.util.io.JsonWriter; +import org.silverware.microservices.Context; import org.silverware.microservices.MicroserviceMetaData; +import org.silverware.microservices.silver.HttpInvokerSilverService; import java.io.Serializable; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.concurrent.atomic.AtomicInteger; /** @@ -40,7 +45,7 @@ public class ServiceHandle implements Serializable { final private transient Object service; - public ServiceHandle(@JsonProperty("host") final String host, @JsonProperty("query") final MicroserviceMetaData query, @JsonProperty("service") final Object service) { + public ServiceHandle(final String host, final MicroserviceMetaData query, final Object service) { this.handle = handleSource.getAndIncrement(); this.host = host; this.query = query; @@ -105,4 +110,24 @@ public String toString() { ", service=" + service + '}'; } + + public Object invoke(final Context context, final String method, final Class[] paramTypes, final Object[] params) throws Exception { + String urlBase = "http://" + host + "/" + context.getProperties().get(HttpInvokerSilverService.INVOKER_URL) + "/invoke"; + + HttpURLConnection con = (HttpURLConnection) new URL(urlBase).openConnection(); + con.setRequestMethod("POST"); + con.setDoInput(true); + con.setDoOutput(true); + con.connect(); + + Invocation invocation = new Invocation(handle, method, paramTypes, params); + JsonWriter jsonWriter = new JsonWriter(con.getOutputStream()); + jsonWriter.write(invocation); + JsonReader jsonReader = new JsonReader(con.getInputStream()); + Object response = jsonReader.readObject(); + + con.disconnect(); + + return response; + } }