Skip to content

Commit

Permalink
#6 using the strategies for real
Browse files Browse the repository at this point in the history
  • Loading branch information
marvec committed May 18, 2015
1 parent 4f9e558 commit 6b9c191
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.apache.logging.log4j.Logger;
import org.silverware.microservices.MicroserviceMetaData;
import org.silverware.microservices.annotations.MicroserviceReference;
import org.silverware.microservices.silver.services.LookupStrategy;
import org.silverware.microservices.silver.services.LookupStrategyFactory;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand All @@ -39,30 +41,27 @@ public class MicroserviceProxy implements MethodHandler {

private static final Logger log = LogManager.getLogger(MicroserviceProxy.class);

private Set<Object> service;

private MicroserviceProxyBean parentBean;

private LookupStrategy lookupStrategy;

private MicroserviceProxy(final MicroserviceProxyBean parentBean) throws Exception {
this.parentBean = parentBean;
}

private synchronized Object getService() {
if (service == null) {
Set<Annotation> qualifiers = parentBean.getQualifiers().stream().filter(qualifier -> !qualifier.annotationType().getName().equals(MicroserviceReference.class.getName())).collect(Collectors.toSet());
final Set<Annotation> qualifiers = parentBean.getQualifiers().stream().filter(qualifier -> !qualifier.annotationType().getName().equals(MicroserviceReference.class.getName())).collect(Collectors.toSet());
final MicroserviceMetaData metaData = new MicroserviceMetaData(parentBean.getMicroserviceName(), parentBean.getServiceInterface(), qualifiers);

service = parentBean.getContext().lookupMicroservice(new MicroserviceMetaData(parentBean.getMicroserviceName(), parentBean.getServiceInterface(), qualifiers));
this.lookupStrategy = LookupStrategyFactory.getStrategy(parentBean.getContext(), metaData, parentBean.getAnnotations());
}

if (log.isDebugEnabled()) {
log.info(String.format("Proxy %s matched with service implementation %s.", this.toString(), service));
}
private synchronized Object getService() {
final Object service = lookupStrategy.getService();

if (service == null || service.size() == 0) {
throw new IllegalStateException(String.format("Cannot lookup any implementation for microservice %s.", new MicroserviceMetaData(parentBean.getMicroserviceName(), parentBean.getServiceInterface(), qualifiers)));
}
if (log.isDebugEnabled()) {
log.info(String.format("Proxy %s matched with service implementation %s.", this.toString(), service));
}

return service.iterator().next();
return service;
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.silverware.microservices.Context;
import org.silverware.microservices.MicroserviceMetaData;
import org.silverware.microservices.annotations.InvocationPolicy;
import org.silverware.microservices.silver.services.lookup.LocalLookupStrategy;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -58,6 +59,11 @@ public static LookupStrategy getStrategy(final Context context, final Microservi
}
}

if (strategy == null) {
strategy = new LocalLookupStrategy();
strategy.initialize(context, metaData, options);
}

return strategy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* -----------------------------------------------------------------------\
* 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.silver.services.lookup;

import java.util.Random;

/**
* Lookup just local service implementations.
*
* @author Martin Večeřa <[email protected]>
*/
public class LocalLookupStrategy extends AbstractLookupStrategy {

private Random rnd = new Random();

@Override
public Object getService() {
final Object[] services = context.lookupLocalMicroservice(metaData).toArray();
return services[rnd.nextInt(services.length)];
}

}

0 comments on commit 6b9c191

Please sign in to comment.