forked from SilverThings/SilverWare
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SilverThings#9 sample invocation strategy is in place
- Loading branch information
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
microservices/src/main/java/org/silverware/microservices/annotations/InvocationPolicy.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 | 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.annotations; | ||
|
||
import org.silverware.microservices.silver.services.LookupStrategy; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author Martin Večeřa <[email protected]> | ||
*/ | ||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface InvocationPolicy { | ||
|
||
Class<LookupStrategy> lookupStrategy(); | ||
} |
63 changes: 63 additions & 0 deletions
63
...ces/src/main/java/org/silverware/microservices/silver/services/LookupStrategyFactory.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* -----------------------------------------------------------------------\ | ||
* 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; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.silverware.microservices.Context; | ||
import org.silverware.microservices.MicroserviceMetaData; | ||
import org.silverware.microservices.annotations.InvocationPolicy; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Constructor; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author Martin Večeřa <[email protected]> | ||
*/ | ||
public class LookupStrategyFactory { | ||
|
||
/** | ||
* Logger. | ||
*/ | ||
private static Logger log = LogManager.getLogger(LookupStrategyFactory.class); | ||
|
||
public static LookupStrategy getStrategy(final Context context, final MicroserviceMetaData metaData, final Set<Annotation> options) { | ||
LookupStrategy strategy = null; | ||
|
||
for (Annotation option: options) { | ||
if (option.annotationType().isAssignableFrom(InvocationPolicy.class)) { | ||
InvocationPolicy policy = (InvocationPolicy) option; | ||
Class<LookupStrategy> clazz = policy.lookupStrategy(); | ||
|
||
try { | ||
Constructor c = clazz.getConstructor(); | ||
strategy = (LookupStrategy) c.newInstance(); | ||
strategy.initialize(context, metaData, options); | ||
break; | ||
} catch (Exception e) { | ||
log.warn(String.format("Could not instantiate lookup strategy class %s:", clazz.getName()), e); | ||
} | ||
} | ||
} | ||
|
||
return strategy; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...main/java/org/silverware/microservices/silver/services/lookup/AbstractLookupStrategy.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* -----------------------------------------------------------------------\ | ||
* 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 org.silverware.microservices.Context; | ||
import org.silverware.microservices.MicroserviceMetaData; | ||
import org.silverware.microservices.silver.services.LookupStrategy; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author Martin Večeřa <[email protected]> | ||
*/ | ||
abstract public class AbstractLookupStrategy implements LookupStrategy { | ||
|
||
protected Context context; | ||
protected MicroserviceMetaData metaData; | ||
protected Set<Annotation> options; | ||
|
||
@Override | ||
public void initialize(final Context context, final MicroserviceMetaData metaData, final Set<Annotation> options) { | ||
this.context = context; | ||
this.metaData = metaData; | ||
this.options = options; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...n/java/org/silverware/microservices/silver/services/lookup/RandomRobinLookupStrategy.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* -----------------------------------------------------------------------\ | ||
* 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; | ||
|
||
/** | ||
* @author Martin Večeřa <[email protected]> | ||
*/ | ||
public class RandomRobinLookupStrategy extends AbstractLookupStrategy { | ||
|
||
private Random rnd = new Random(); | ||
|
||
@Override | ||
public Object getService() { | ||
final Object[] services = context.lookupMicroservice(metaData).toArray(); | ||
return services[rnd.nextInt(services.length)]; | ||
} | ||
} |