-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
2,679 additions
and
160 deletions.
There are no files selected for viewing
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
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
9 changes: 9 additions & 0 deletions
9
actors/core/src/main/java/org/sunbird/actor/core/service/InterServiceCommunication.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,9 @@ | ||
package org.sunbird.actor.core.service; | ||
|
||
import org.sunbird.common.request.Request; | ||
|
||
/** Created by arvind on 24/4/18. */ | ||
public interface InterServiceCommunication { | ||
|
||
public Object getResponse(Request request, String operation); | ||
} |
47 changes: 47 additions & 0 deletions
47
...s/core/src/main/java/org/sunbird/actor/core/service/InterServiceCommunicationFactory.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,47 @@ | ||
package org.sunbird.actor.core.service; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.sunbird.actor.core.service.impl.InterServiceCommunicationImpl; | ||
|
||
/** Created by arvind on 24/4/18. */ | ||
public class InterServiceCommunicationFactory { | ||
|
||
private static InterServiceCommunicationFactory factory; | ||
private static Map<String, InterServiceCommunication> modes = new HashMap<>(); | ||
|
||
private InterServiceCommunicationFactory() {} | ||
|
||
public static InterServiceCommunicationFactory getInstance() { | ||
if (null == factory) { | ||
synchronized (InterServiceCommunicationFactory.class) { | ||
if (null == factory) { | ||
factory = new InterServiceCommunicationFactory(); | ||
} | ||
} | ||
} | ||
return factory; | ||
} | ||
|
||
public InterServiceCommunication getCommunicationPath(String mode) { | ||
if ("actorCommunication".equalsIgnoreCase(mode)) { | ||
return getActorCommunicationMode(); | ||
} | ||
return null; | ||
} | ||
|
||
public InterServiceCommunication getActorCommunicationMode() { | ||
|
||
if (modes.get("actorCommunication") != null) { | ||
return modes.get("actorCommunication"); | ||
} else { | ||
synchronized (InterServiceCommunicationFactory.class) { | ||
if (modes.get("actorCommunication") == null) { | ||
InterServiceCommunication communication = new InterServiceCommunicationImpl(); | ||
modes.put("actorCommunication", communication); | ||
} | ||
} | ||
} | ||
return modes.get("actorCommunication"); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...core/src/main/java/org/sunbird/actor/core/service/impl/InterServiceCommunicationImpl.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,59 @@ | ||
package org.sunbird.actor.core.service.impl; | ||
|
||
import static akka.pattern.PatternsCS.ask; | ||
|
||
import akka.actor.ActorRef; | ||
import akka.actor.ActorSelection; | ||
import akka.util.Timeout; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.TimeUnit; | ||
import org.sunbird.actor.core.service.InterServiceCommunication; | ||
import org.sunbird.actor.router.RequestRouter; | ||
import org.sunbird.actor.service.BaseMWService; | ||
import org.sunbird.common.models.util.LoggerEnum; | ||
import org.sunbird.common.models.util.ProjectLogger; | ||
import org.sunbird.common.request.Request; | ||
import scala.concurrent.duration.Duration; | ||
|
||
/** Created by arvind on 24/4/18. */ | ||
public class InterServiceCommunicationImpl extends BaseMWService | ||
implements InterServiceCommunication { | ||
|
||
private Integer WAIT_TIME = 10; | ||
|
||
@Override | ||
public Object getResponse(Request request, String operation) { | ||
ActorRef actor = RequestRouter.getActor(operation); | ||
Timeout t = new Timeout(Duration.create(WAIT_TIME, TimeUnit.SECONDS)); | ||
request.setOperation(operation); | ||
Object obj = null; | ||
if (null == actor) { | ||
ActorSelection select = getRemoteRouter(RequestRouter.class.getSimpleName()); | ||
CompletionStage<ActorRef> futureActor = | ||
select.resolveOneCS(Duration.create(WAIT_TIME, "seconds")); | ||
try { | ||
actor = futureActor.toCompletableFuture().get(); | ||
} catch (Exception e) { | ||
ProjectLogger.log( | ||
"InterServiceCommunicationImpl : getResponse - unable to get actorref from actorselection " | ||
+ e.getMessage(), | ||
e); | ||
} | ||
} | ||
if (null == actor) { | ||
ProjectLogger.log( | ||
"InterServiceCommunicationImpl : getResponse - actorRef is null ", LoggerEnum.INFO); | ||
return obj; | ||
} | ||
ProjectLogger.log( | ||
"Operation " + operation + " with request type " + (request instanceof Request)); | ||
CompletableFuture<Object> future = ask(actor, request, t).toCompletableFuture(); | ||
try { | ||
obj = future.get(WAIT_TIME + 2, TimeUnit.SECONDS); | ||
} catch (Exception e) { | ||
ProjectLogger.log("Interservice communication error " + e.getMessage(), e); | ||
} | ||
return obj; | ||
} | ||
} |
Oops, something went wrong.