Skip to content

Commit

Permalink
SilverThings#4 getting ready for cluster invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
marvec authored and Jakub Cechacek committed Dec 9, 2015
1 parent b583459 commit ac55904
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -130,14 +132,14 @@ public void run() {
}

@Override
public Object lookupMicroservice(final MicroserviceMetaData metaData) {
public Set<Object> lookupMicroservice(final MicroserviceMetaData metaData) {
if (Route.class.isAssignableFrom(metaData.getType())) {
return camelContext.getRoute(metaData.getName());
return Collections.singleton(camelContext.getRoute(metaData.getName()));
} else if (Endpoint.class.isAssignableFrom(metaData.getType())) {
return camelContext.getEndpoint(metaData.getName());
return Collections.singleton(camelContext.getEndpoint(metaData.getName()));
}

return null;
return new HashSet<>();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void run() {
}
}

public Object lookupMicroservice(final MicroserviceMetaData microserviceMetaData) {
public Set<Object> lookupMicroservice(final MicroserviceMetaData microserviceMetaData) {
final String name = microserviceMetaData.getName();
final Class<?> type = microserviceMetaData.getType();
final Set<Annotation> qualifiers = microserviceMetaData.getQualifiers();
Expand All @@ -121,10 +121,10 @@ public Object lookupMicroservice(final MicroserviceMetaData microserviceMetaData
// or third, the annotation has a name defined and we asked for it
if (name == null || (microserviceAnnotation.value().isEmpty() && name.equals(theBean.getName())) ||
(!microserviceAnnotation.value().isEmpty() && name.equals(microserviceAnnotation.value()))) {
return beanManager.getReference(theBean, type, beanManager.createCreationalContext(theBean));
return Collections.singleton(beanManager.getReference(theBean, type, beanManager.createCreationalContext(theBean)));
} else if (type.isInterface() && !name.equals(theBean.getName())) {
if (qualifiers.stream().filter(q -> q.annotationType().getName().equals("Default")).count() == 0) { // we have a qualifier match
return beanManager.getReference(theBean, type, beanManager.createCreationalContext(theBean));
return Collections.singleton(beanManager.getReference(theBean, type, beanManager.createCreationalContext(theBean)));
}
}
}
Expand All @@ -134,7 +134,7 @@ public Object lookupMicroservice(final MicroserviceMetaData microserviceMetaData
}

@Override
public Object lookupLocalMicroservice(final MicroserviceMetaData metaData) {
public Set<Object> lookupLocalMicroservice(final MicroserviceMetaData metaData) {
return lookupMicroservice(metaData);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;
import org.silverware.microservices.Context;
import org.silverware.microservices.MicroserviceMetaData;
import org.silverware.microservices.providers.MicroserviceProvider;
import org.silverware.microservices.silver.ClusterSilverService;
import org.silverware.microservices.silver.cluster.ServiceHandle;
import org.silverware.microservices.util.Utils;

import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
* @author Martin Večeřa <[email protected]>
*/
Expand All @@ -40,6 +48,8 @@ public class ClusterMicroserviceProvider implements MicroserviceProvider, Cluste
private Context context;
private JChannel channel;
private ChannelReceiver receiver;
private Map<MicroserviceMetaData, Set<ServiceHandle>> outboundHandles = new ConcurrentHashMap<>();
private Queue<MicroserviceMetaData> toLookup = new ConcurrentLinkedQueue<>();

@Override
public void initialize(final Context context) {
Expand Down Expand Up @@ -82,6 +92,16 @@ public void run() {
}
}

@Override
public Set<Object> lookupMicroservice(final MicroserviceMetaData metaData) {
return null;
}

@Override
public Set<Object> lookupLocalMicroservice(final MicroserviceMetaData metaData) {
return null;
}

private static class ChannelReceiver extends ReceiverAdapter {
@Override
public void receive(final Message msg) {
Expand Down
4 changes: 4 additions & 0 deletions http-invoker-microservice-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>
<dependency>
<groupId>com.cedarsoftware</groupId>
<artifactId>json-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* -----------------------------------------------------------------------\
* SilverWare
*  
* Copyright (C) 2010 - 2013 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 org.silverware.microservices.providers.http.invoker.internal;

import org.silverware.microservices.Context;
import org.silverware.microservices.silver.cluster.ServiceHandle;

import java.lang.reflect.Method;

import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;

/**
* @author Martin Večeřa <[email protected]>
*/
public class HttpServiceProxy implements MethodHandler {

private final Context context;
private final ServiceHandle serviceHandle;

private HttpServiceProxy(final Context context, final ServiceHandle serviceHandle) {
this.context = context;
this.serviceHandle = serviceHandle;
}

@SuppressWarnings("unchecked")
public static <T> T getProxy(final Context context, final ServiceHandle serviceHandle) {
try {
ProxyFactory factory = new ProxyFactory();
if (serviceHandle.getQuery().getType().isInterface()) {
factory.setInterfaces(new Class[] { serviceHandle.getQuery().getType() });
} else {
factory.setSuperclass(serviceHandle.getQuery().getType());
}
return (T) factory.create(new Class[0], new Object[0], new HttpServiceProxy(context, serviceHandle));
} catch (Exception e) {
throw new IllegalStateException("Cannot create Http proxy for class " + serviceHandle.getQuery().getType().getName() + ": ", e);
}
}

@Override
public Object invoke(final Object o, final Method method, final Method method1, final Object[] objects) throws Throwable {
return serviceHandle.invoke(context, method.getName(), objects);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*
* @author Martin Večeřa <[email protected]>
*/
public interface ClusterSilverService extends SilverService {
public interface ClusterSilverService extends ProvidingSilverService {

/**
* Property with the cluster group name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.silverware.microservices.MicroserviceMetaData;

import java.util.Set;

/**
* SilverService that is able to provide Microservices.
*
Expand All @@ -34,7 +36,7 @@ public interface ProvidingSilverService extends SilverService {
* @param metaData Meta-data query.
* @return All available Microservices supported by this provider that meet the given query.
*/
Object lookupMicroservice(final MicroserviceMetaData metaData);
Set<Object> lookupMicroservice(final MicroserviceMetaData metaData);

/**
* Looks up all local Microservices supported by this provider.
Expand All @@ -43,7 +45,7 @@ public interface ProvidingSilverService extends SilverService {
* @param metaData Meta-data query.
* @return All local Microservices supported by this provider that meet the given query.
*/
default Object lookupLocalMicroservice(final MicroserviceMetaData metaData) {
default Set<Object> lookupLocalMicroservice(final MicroserviceMetaData metaData) {
return lookupMicroservice(metaData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,14 @@ public Object invoke(final Context context, final String method, final Class[] p

return response;
}

public Object invoke(final Context context, final String method, final Object[] params) throws Exception {
final Class[] paramTypes = new Class[params.length];
for (int i = 0; i < params.length; i++) {
paramTypes[i] = params.getClass();
}

return invoke(context, method, paramTypes, params);
}

}

0 comments on commit ac55904

Please sign in to comment.