Skip to content

Commit

Permalink
#5 service handle can be used to directly invoke the service
Browse files Browse the repository at this point in the history
  • Loading branch information
marvec committed May 6, 2015
1 parent 9d35461 commit 3286806
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<ServiceHandle> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions microservices/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<artifactId>commons-cli</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<groupId>com.cedarsoftware</groupId>
<artifactId>json-io</artifactId>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,7 +109,8 @@ public List<ServiceHandle> 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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Annotation> qualifiers) {
public MicroserviceMetaData(final String name, final Class type, final Set<Annotation> qualifiers) {
this.name = name;
this.type = type;
this.qualifiers = qualifiers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ public interface HttpInvokerSilverService extends SilverService {

String INVOKER_URL = "silverware.http-invoker.url";

String INVOKE_COMMAND = "invoke";
String QUERY_COMMAND = "query";

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 3286806

Please sign in to comment.